Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config system and customize command handling #1

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {
}

group 'io.codemc'
version '2.0.0'
version '2.1.0'

compileJava.options.encoding('UTF-8')

Expand All @@ -42,6 +42,7 @@ dependencies {
}
implementation group: 'pw.chew', name: 'jda-chewtils-commons', version: '2.0-SNAPSHOT'
implementation group: 'pw.chew', name: 'jda-chewtils-command', version: '2.0-SNAPSHOT'
implementation group: 'org.spongepowered', name: 'configurate-gson', version: '4.1.2'
}

artifacts {
Expand Down
106 changes: 73 additions & 33 deletions src/main/java/io/codemc/bot/CodeMCBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@

package io.codemc.bot;

import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import io.codemc.bot.commands.CmdApplication;
import io.codemc.bot.commands.CmdDisable;
import io.codemc.bot.commands.CmdMsg;
import io.codemc.bot.commands.CmdSubmit;
import io.codemc.bot.commands.*;
import io.codemc.bot.config.ConfigHandler;
import io.codemc.bot.listeners.ModalListener;
import io.codemc.bot.menu.ApplicationMenu;
import io.codemc.bot.utils.Constants;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
Expand All @@ -35,42 +31,82 @@
import org.slf4j.LoggerFactory;

import javax.security.auth.login.LoginException;
import java.util.List;

public class CodeMCBot{

private final Logger LOG = LoggerFactory.getLogger(CodeMCBot.class);
private final Logger logger = LoggerFactory.getLogger(CodeMCBot.class);
private final ConfigHandler configHandler = new ConfigHandler();

public static void main(String[] args){
try{
new CodeMCBot().start(args[0]);
new CodeMCBot().start();
}catch(LoginException ex){
new CodeMCBot().LOG.error("Unable to login to Discord!", ex);
new CodeMCBot().logger.error("Unable to login to Discord!", ex);
}
}

private void start(String token) throws LoginException{
CommandClient commandClient = new CommandClientBuilder()
.setOwnerId(
"204232208049766400" // Andre_601#0601
)
.setCoOwnerIds(
"143088571656437760", // sgdc3#0001
"282975975954710528" // tr7zw#4005
)
.setActivity(null)
.addSlashCommands(
new CmdApplication(),
new CmdDisable(),
new CmdMsg(),
new CmdSubmit()
)
.addContextMenus(
new ApplicationMenu.Accept(),
new ApplicationMenu.Deny()
)
.forceGuildOnly(Constants.SERVER)
.build();
private void start() throws LoginException{
if(!configHandler.loadConfig()){
logger.warn("Unable to load config.json! See previous logs for any errors.");
System.exit(1);
return;
}

String token = configHandler.getString("bot_token");
if(token == null || token.isEmpty()){
logger.warn("Received invalid Bot Token!");
System.exit(1);
return;
}

long owner = configHandler.getLong("users", "owner");
if(owner == -1L){
logger.warn("Unable to retrieve Owner ID. This value is required!");
System.exit(1);
return;
}

long guildId = configHandler.getLong("server");
if(guildId == -1L){
logger.warn("Unable to retrieve Server ID. This value is required!");
System.exit(1);
return;
}

CommandClientBuilder clientBuilder = new CommandClientBuilder().setActivity(null).forceGuildOnly(guildId);

clientBuilder.setOwnerId(owner);

List<Long> coOwners = configHandler.getLongList("users", "co_owners");

if(coOwners != null && !coOwners.isEmpty()){
logger.info("Adding {} Co-Owner(s) to the bot.", coOwners.size());
// Annoying, but setCoOwnerIds has no overload with a Collection<Long>...
long[] coOwnerIds = new long[coOwners.size()];
for(int i = 0; i < coOwnerIds.length; i++){
coOwnerIds[i] = coOwners.get(i);
}

clientBuilder.setCoOwnerIds(coOwnerIds);
}

logger.info("Adding commands...");
clientBuilder.addSlashCommands(
new CmdApplication(this),
new CmdDisable(this),
new CmdMsg(this),
new CmdReload(this),
new CmdSubmit()
);

logger.info("Adding Context Menus...");
clientBuilder.addContextMenus(
new ApplicationMenu.Accept(this),
new ApplicationMenu.Deny(this)
);

logger.info("Starting bot...");
JDABuilder.createDefault(token)
.enableIntents(
GatewayIntent.GUILD_MEMBERS,
Expand All @@ -83,9 +119,13 @@ private void start(String token) throws LoginException{
"Applications"
))
.addEventListeners(
commandClient,
new ModalListener()
clientBuilder.build(),
new ModalListener(this)
)
.build();
}

public ConfigHandler getConfigHandler(){
return configHandler;
}
}
1 change: 0 additions & 1 deletion src/main/java/io/codemc/bot/commands/BotCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public abstract class BotCommand extends SlashCommand{

@Override
public void execute(SlashCommandEvent event){
System.out.println("Custom BotCommand called!");
Guild guild = event.getGuild();
if(guild == null){
CommandUtil.EmbedReply.fromCommandEvent(event)
Expand Down
49 changes: 26 additions & 23 deletions src/main/java/io/codemc/bot/commands/CmdApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import io.codemc.bot.CodeMCBot;
import io.codemc.bot.utils.CommandUtil;
import io.codemc.bot.utils.Constants;
import net.dv8tion.jda.api.EmbedBuilder;
Expand All @@ -44,18 +45,15 @@

public class CmdApplication extends BotCommand{

public CmdApplication(){
public CmdApplication(CodeMCBot bot){
this.name = "application";
this.help = "Accept or deny applications.";

this.allowedRoles = Arrays.asList(
Constants.ROLE_ADMINISTRATOR,
Constants.ROLE_MODERATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "application");

this.children = new SlashCommand[]{
new Accept(),
new Deny()
new Accept(bot),
new Deny(bot)
};
}

Expand All @@ -65,8 +63,8 @@ public void withModalReply(SlashCommandEvent event){}
@Override
public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild guild, Member member){}

public static void handle(InteractionHook hook, Guild guild, long messageId, String str, boolean accepted){
TextChannel requestChannel = guild.getTextChannelById(Constants.REQUEST_ACCESS);
public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long messageId, String str, boolean accepted){
TextChannel requestChannel = guild.getTextChannelById(bot.getConfigHandler().getLong("channel", "request_access"));
if(requestChannel == null){
CommandUtil.EmbedReply.fromHook(hook).withError("Unable to retrieve `request-access` channel.").send();
return;
Expand Down Expand Up @@ -112,7 +110,10 @@ public static void handle(InteractionHook hook, Guild guild, long messageId, Str
return;
}

TextChannel channel = guild.getTextChannelById(accepted ? Constants.ACCEPTED_REQUESTS : Constants.REJECTED_REQUESTS);
TextChannel channel = guild.getTextChannelById(accepted
? bot.getConfigHandler().getLong("channels", "accepted_requests")
: bot.getConfigHandler().getLong("channels", "rejected_requests")
);
if(channel == null){
CommandUtil.EmbedReply.fromHook(hook)
.withError("Unable to retrieve `" + (accepted ? "accepted" : "rejected") + "-requests` channel.")
Expand Down Expand Up @@ -140,7 +141,7 @@ public static void handle(InteractionHook hook, Guild guild, long messageId, Str
return;
}

Role authorRole = guild.getRoleById(Constants.ROLE_AUTHOR);
Role authorRole = guild.getRoleById(bot.getConfigHandler().getLong("author_role"));
if(authorRole == null){
CommandUtil.EmbedReply.fromHook(hook)
.withError("Unable to retrieve Author Role!")
Expand Down Expand Up @@ -193,14 +194,15 @@ private static class Accept extends BotCommand{

private final Pattern projectUrlPattern = Pattern.compile("^https://ci\\.codemc\\.io/job/[a-zA-Z0-9-]+/job/[a-zA-Z0-9-_.]+/?$");

public Accept(){
private final CodeMCBot bot;

public Accept(CodeMCBot bot){
this.bot = bot;

this.name = "accept";
this.help = "Accept an application";

this.allowedRoles = Arrays.asList(
Constants.ROLE_ADMINISTRATOR,
Constants.ROLE_MODERATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "application");

this.options = Arrays.asList(
new OptionData(OptionType.STRING, "id", "The message id of the application.").setRequired(true),
Expand Down Expand Up @@ -236,20 +238,21 @@ public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild g
return;
}

handle(hook, guild, messageId, projectUrl, true);
handle(bot, hook, guild, messageId, projectUrl, true);
}
}

private static class Deny extends BotCommand{

public Deny(){
private final CodeMCBot bot;

public Deny(CodeMCBot bot){
this.bot = bot;

this.name = "deny";
this.help = "Deny an application";

this.allowedRoles = Arrays.asList(
Constants.ROLE_ADMINISTRATOR,
Constants.ROLE_MODERATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "application");

this.options = Arrays.asList(
new OptionData(OptionType.STRING, "id", "The message id of the application.").setRequired(true),
Expand Down Expand Up @@ -278,7 +281,7 @@ public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild g
return;
}

handle(hook, guild, messageId, reason, false);
handle(bot, hook, guild, messageId, reason, false);
}
}
}
10 changes: 3 additions & 7 deletions src/main/java/io/codemc/bot/commands/CmdDisable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,23 @@
package io.codemc.bot.commands;

import com.jagrosh.jdautilities.command.SlashCommandEvent;
import io.codemc.bot.CodeMCBot;
import io.codemc.bot.utils.CommandUtil;
import io.codemc.bot.utils.Constants;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.interactions.InteractionHook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

public class CmdDisable extends BotCommand{

private final Logger logger = LoggerFactory.getLogger("Shutdown");

public CmdDisable(){
public CmdDisable(CodeMCBot bot){
this.name = "disable";
this.help = "Disables the bot.";

this.allowedRoles = Collections.singletonList(
Constants.ROLE_ADMINISTRATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "disable");
}

@Override
Expand Down
25 changes: 9 additions & 16 deletions src/main/java/io/codemc/bot/commands/CmdMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import io.codemc.bot.CodeMCBot;
import io.codemc.bot.utils.CommandUtil;
import io.codemc.bot.utils.Constants;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
Expand All @@ -38,21 +38,18 @@
import net.dv8tion.jda.api.interactions.modals.Modal;

import java.util.Arrays;
import java.util.Collections;

public class CmdMsg extends BotCommand{

public CmdMsg(){
public CmdMsg(CodeMCBot bot){
this.name = "msg";
this.help = "Sends a message in a specified channel or edits one.";

this.allowedRoles = Collections.singletonList(
Constants.ROLE_ADMINISTRATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "msg");

this.children = new SlashCommand[]{
new Post(),
new Edit()
new Post(bot),
new Edit(bot)
};
}

Expand All @@ -64,13 +61,11 @@ public void withModalReply(SlashCommandEvent event){}

private static class Post extends BotCommand{

public Post(){
public Post(CodeMCBot bot){
this.name = "send";
this.help = "Sends a message as the Bot.";

this.allowedRoles = Collections.singletonList(
Constants.ROLE_ADMINISTRATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "msg");
this.hasModalReply = true;

this.options = Arrays.asList(
Expand Down Expand Up @@ -113,13 +108,11 @@ public void withModalReply(SlashCommandEvent event){

private static class Edit extends BotCommand{

public Edit(){
public Edit(CodeMCBot bot){
this.name = "edit";
this.help = "Edit an existing message of the bot.";

this.allowedRoles = Collections.singletonList(
Constants.ROLE_ADMINISTRATOR
);
this.allowedRoles = bot.getConfigHandler().getLongList("allowed_roles", "msg");
this.hasModalReply = true;

this.options = Arrays.asList(
Expand Down
Loading