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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,12 @@ fabric.properties
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# End of https://www.toptal.com/developers/gitignore/api/intellij,java
# End of https://www.toptal.com/developers/gitignore/api/intellij,java
/.idea/copyright/*.xml
/.idea/.gitignore
/.idea/compiler.xml
/.idea/gradle.xml
/.idea/jarRepositories.xml
/.idea/misc.xml
/.idea/uiDesigner.xml
/.idea/vcs.xml
18 changes: 9 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
*/
Andre601 marked this conversation as resolved.
Show resolved Hide resolved

plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '5.2.0'
id 'idea'
id "java"
id "application"
id "com.github.johnrengelman.shadow" version "8.1.1"
Andre601 marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

repositories {
mavenCentral()
maven { url = 'https://jitpack.io' }
maven { url = 'https://repo.codemc.io/repository/maven-public' }
maven { url = 'https://m2.chew.pro/snapshots' }
maven { url = 'https://m2.chew.pro/releases' }
}

dependencies {
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.4.14'
implementation(group: 'net.dv8tion', name: 'JDA', version:'5.0.0-beta.23'){
implementation(group: 'net.dv8tion', name: 'JDA', version:'5.0.0'){
exclude(module: 'opus-java')
}
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: 'pw.chew', name: 'jda-chewtils-commons', version: '2.0'
implementation group: 'pw.chew', name: 'jda-chewtils-command', version: '2.0'
implementation group: 'org.spongepowered', name: 'configurate-gson', version: '4.1.2'
}

artifacts {
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Sun Apr 28 16:10:49 CEST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
97 changes: 69 additions & 28 deletions src/main/java/io/codemc/bot/CodeMCBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +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.ButtonListener;
import io.codemc.bot.listeners.ModalListener;
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 @@ -34,37 +31,76 @@
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()
).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(this)
);

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

public ConfigHandler getConfigHandler(){
return configHandler;
}
}
85 changes: 85 additions & 0 deletions src/main/java/io/codemc/bot/commands/BotCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2024 CodeMC.io
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.codemc.bot.commands;

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import io.codemc.bot.CodeMCBot;
import io.codemc.bot.utils.CommandUtil;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.interactions.InteractionHook;

import java.util.ArrayList;
import java.util.List;

public abstract class BotCommand extends SlashCommand{

protected List<Long> allowedRoles = new ArrayList<>();
protected boolean hasModalReply = false;

public final CodeMCBot bot;

public BotCommand(CodeMCBot bot){
this.bot = bot;
}

@Override
public void execute(SlashCommandEvent event){
Guild guild = event.getGuild();
if(guild == null){
CommandUtil.EmbedReply.from(event)
.error("Command can only be executed in a Server!")
.send();
return;
}

if(guild.getIdLong() != bot.getConfigHandler().getLong("server")){
CommandUtil.EmbedReply.from(event)
.error("Unable to find CodeMC Server!")
.send();
return;
}

Member member = event.getMember();
if(member == null){
CommandUtil.EmbedReply.from(event)
.error("Unable to retrieve Member from Event!")
.send();
return;
}

if(!CommandUtil.hasRole(member, allowedRoles)){
CommandUtil.EmbedReply.from(event)
.error("You lack the permissions required to use this command!")
.send();
return;
}

if(hasModalReply){
withModalReply(event);
}else{
event.deferReply(true).queue(hook -> withHookReply(hook, event, guild, member));
}
}

public abstract void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild guild, Member member);

public abstract void withModalReply(SlashCommandEvent event);
}
Loading