Skip to content

Commit

Permalink
chore: Command test bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Greazi-Times committed Apr 21, 2024
1 parent ad01fdf commit eb8d51d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 80 deletions.
26 changes: 13 additions & 13 deletions src/main/java/com/greazi/discordbotfoundation/SimpleBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.SelfUser;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.AnnotatedEventManager;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
Expand Down Expand Up @@ -171,25 +170,19 @@ public SimpleBot() {
// Call the onBotLoad method
onBotLoad();

// Register the ping command
SimpleCommand.register(new PingCommand());
registerCommands(
new PingCommand()
);

// Call the onBotStart method
onBotStart();

// Call the onReloadableStart method
onReloadableStart();

SimpleCommand.registerAll();
jda.addEventListener(new SlashCommandHandler());

getJda().addEventListener(new SlashCommandHandler() {
@Override
protected void execute(@NotNull final SlashCommandInteractionEvent event) {

}
});

getJda().addEventListener(new PingCommand());
SlashCommandHandler.registerCommands();

// Set the enabled to true
this.enabled = true;
Expand Down Expand Up @@ -221,6 +214,14 @@ private static void setMainGuild() {
}
}

private void registerCommands(@NotNull final SimpleCommand... simpleCommands) {
for (final SimpleCommand simpleCommand : simpleCommands) registerCommand(simpleCommand);
}

private void registerCommand(final SimpleCommand simpleCommand) {
SlashCommandHandler.register(simpleCommand);
}


// ----------------------------------------------------------------------------------------
// Logging
Expand Down Expand Up @@ -277,7 +278,6 @@ public boolean enforceNewLine() {
protected void onPreLoad() {
}


/**
* Called the moment JDA is registerd
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

import com.greazi.discordbotfoundation.Common;

/**
* A simple ping command to get the latency of the bot
* The message output can be changed by overriding this file!
*/

public class PingCommand extends SimpleCommand {

/**
Expand All @@ -32,4 +29,4 @@ protected void onCommand() {
"Response: " + event.getJDA().getResponseTotal()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* <p>
* Example: /about or /info user Greazi
*/
public abstract class SimpleCommand extends SlashCommandHandler {
public abstract class SimpleCommand {

/**
* The command name, eg. /about or /ping
Expand Down Expand Up @@ -215,21 +215,14 @@ protected SimpleCommand(final String command) {
}

// ----------------------------------------------------------------------
// Registration
// Command data
// ----------------------------------------------------------------------

public static void register(final SimpleCommand command) {
Debugger.debug("SimpleCommand", "Registering command: " + command.getCommand() + " with description: " + command.getDescription());
COMMANDS.add(command);
SimpleBot.getJda().addEventListener(command);
}

public static void registerAll() {
Debugger.debug("SimpleCommand", "Registering all commands");
// Register all commands
SimpleBot.getJda().updateCommands().addCommands(COMMANDS.stream().map(SimpleCommand::getCommandData).toList()).queue();
}

/**
* Set the command data required by Discord to register the command
*
* @return CommandData The command data for Discord
*/
public final CommandData getCommandData() {
Debugger.debug("SimpleCommand", "Creating command data for command: " + this.getCommand() + " with description: " + this.getDescription());
final SlashCommandData commandData = Commands.slash(this.getCommand(), this.getDescription());
Expand All @@ -244,141 +237,143 @@ public final CommandData getCommandData() {
commandData.addSubcommands(subcommand);
}

// Add options if there are no subcommands or subcommand groups
if (getSubcommandGroupDataList().isEmpty() && getSubcommandGroup().isEmpty()) {
commandData.addOptions(getOptions());
}

// Set default permissions
final DefaultMemberPermissions defaultMemberPermissions = DefaultMemberPermissions.enabledFor(getPermissions());
commandData.setDefaultPermissions(defaultMemberPermissions);

// Set guild-only status
final boolean isGuildOnly = isMainGuildOnly();
commandData.setGuildOnly(isGuildOnly);

return commandData;
}

// ----------------------------------------------------------------------------------------
// Execution
// ----------------------------------------------------------------------------------------

@Override
protected void execute(@NotNull final SlashCommandInteractionEvent event) {
Common.log("SimpleCommand", "Executing command: " + this.getCommand() + " User: " + event.getUser().getName());
protected void execute(@NotNull final SlashCommandInteractionEvent event, final SimpleCommand simpleCommand) {
Common.log("Received command: " + this.getCommand() + " User: " + event.getUser().getName());
// Set the event variable
this.event = event;

// Log the command
Debugger.debug("SimpleCommand", "Executing command: " + this.getCommand() + " User: " + this.event.getUser().getName());
simpleCommand.event = event;

// Check if the command is the same as the command name
if (!this.event.getCommandString().equalsIgnoreCase(this.getCommand())) {
if (!simpleCommand.event.getCommandString().equalsIgnoreCase(simpleCommand.getCommand())) {
// Return because the command is not the same
return;
}

// Set the guild where to command is used
this.guild = this.event.getGuild();
simpleCommand.guild = simpleCommand.event.getGuild();

// Check if the guild is set and if its set to mainguild only
if (this.guild == null || this.guild != SimpleBot.getMainGuild()) {
if (simpleCommand.guild == null || simpleCommand.guild != SimpleBot.getMainGuild()) {
// Return an error message
replyErrorEmbed("Command denied", "This command can only be used in the main guild");
return;
}

// Set the user and member
this.user = this.event.getUser();
this.member = this.event.getMember();
simpleCommand.user = simpleCommand.event.getUser();
simpleCommand.member = simpleCommand.event.getMember();

// Making sure that the channel is a text channel set to null if it's a thread channel
this.channel = this.event.getChannel() instanceof TextChannel ? this.event.getChannel().asTextChannel() : null;
simpleCommand.channel = simpleCommand.event.getChannel() instanceof TextChannel ? simpleCommand.event.getChannel().asTextChannel() : null;
// Making sure that the channel is a thread channel set to null if it's a text channel
this.threadChannel = this.event.getChannel() instanceof ThreadChannel ? this.event.getChannel().asThreadChannel() : null;
simpleCommand.threadChannel = simpleCommand.event.getChannel() instanceof ThreadChannel ? simpleCommand.event.getChannel().asThreadChannel() : null;

// Check if the user can use the command
boolean canExecute = true;

// Check if the user is in the disabled users list
if (this.disabledUsers != null && this.disabledUsers.contains(this.user)) {
if (simpleCommand.disabledUsers != null && simpleCommand.disabledUsers.contains(simpleCommand.user)) {
canExecute = false;
}

// Check if the user is in the disabled roles list
if (this.disabledRoles != null && this.member != null) {
for (final Role role : this.disabledRoles) {
if (this.member.getRoles().contains(role)) {
if (simpleCommand.disabledRoles != null && simpleCommand.member != null) {
for (final Role role : simpleCommand.disabledRoles) {
if (simpleCommand.member.getRoles().contains(role)) {
canExecute = false;
break;
}
}
}

// Check if the channel is in the disabled channels list
if (this.disabledChannels != null && this.channel != null) {
if (this.disabledChannels.contains(this.channel) || this.disabledChannels.contains(this.threadChannel)) {
if (simpleCommand.disabledChannels != null && simpleCommand.channel != null) {
if (simpleCommand.disabledChannels.contains(simpleCommand.channel) || simpleCommand.disabledChannels.contains(simpleCommand.threadChannel)) {
canExecute = false;
}
}

// Check if the user is in the allowed users list
if (this.allowedUsers != null && !this.allowedUsers.contains(this.user)) {
if (simpleCommand.allowedUsers != null && !simpleCommand.allowedUsers.contains(simpleCommand.user)) {
canExecute = false;
}

// Check if the user is in the allowed roles list
if (this.allowedRoles != null && this.member != null) {
for (final Role role : this.allowedRoles) {
if (this.member.getRoles().contains(role)) {
if (simpleCommand.allowedRoles != null && simpleCommand.member != null) {
for (final Role role : simpleCommand.allowedRoles) {
if (simpleCommand.member.getRoles().contains(role)) {
canExecute = true;
break;
}
}
}

// Check if the channel is in the allowed channels list
if (this.allowedChannels != null && this.channel != null) {
if (!this.allowedChannels.contains(this.channel) || !this.allowedChannels.contains(this.threadChannel)) {
if (simpleCommand.allowedChannels != null && simpleCommand.channel != null) {
if (!simpleCommand.allowedChannels.contains(simpleCommand.channel) || !simpleCommand.allowedChannels.contains(simpleCommand.threadChannel)) {
canExecute = false;
}
}

// Check if the user has the permission to bypass the cooldown
if (this.cooldownBypassPermission != null && this.member != null) {
if (simpleCommand.cooldownBypassPermission != null && simpleCommand.member != null) {
for (final Permission permission : this.cooldownBypassPermission) {
if (this.member.hasPermission(permission)) {
if (simpleCommand.member.hasPermission(permission)) {
canExecute = true;
break;
}
}
}

// Check if the user is in the cooldown bypass list
if (this.cooldownBypassUsers != null && this.cooldownBypassUsers.contains(this.user)) {
if (simpleCommand.cooldownBypassUsers != null && simpleCommand.cooldownBypassUsers.contains(simpleCommand.user)) {
canExecute = true;
}

// Check if the user is in the cooldown map
if (this.cooldownMap.containsKey(this.user)) {
if (simpleCommand.cooldownMap.containsKey(simpleCommand.user)) {
// Get the last execution time
final long lastExecution = this.cooldownMap.get(this.user);
final long lastExecution = simpleCommand.cooldownMap.get(simpleCommand.user);

// Get the current time
final long currentTime = System.currentTimeMillis();

// Get the remaining time
final long remainingTime = (lastExecution + (this.cooldownSeconds * 1000)) - currentTime;
final long remainingTime = (lastExecution + (simpleCommand.cooldownSeconds * 1000)) - currentTime;

// Check if the remaining time is greater than 0
if (remainingTime > 0) {
// Check if we should send the cooldown message
if (this.cooldownMessage != null && !this.cooldownMessage.isEmpty()) {
if (simpleCommand.cooldownMessage != null && !simpleCommand.cooldownMessage.isEmpty()) {
// Replace the duration placeholder
final String message = this.cooldownMessage.replace("{duration}", String.valueOf(remainingTime / 1000));
final String message = simpleCommand.cooldownMessage.replace("{duration}", String.valueOf(remainingTime / 1000));

// Check if we should send the message as an embed
if (this.cooldownMessageAsEmbed) {
if (simpleCommand.cooldownMessageAsEmbed) {
// Send the message as an embed
this.replyErrorEmbed("Command denied", message);
simpleCommand.replyErrorEmbed("Command denied", message);
} else {
// Send the message as a normal message
this.reply(message);
simpleCommand.reply(message);
}
}

Expand All @@ -399,28 +394,28 @@ protected void execute(@NotNull final SlashCommandInteractionEvent event) {
// Check if the command is set to main guild only
if (this.isMainGuildOnly()) {
// Check if the guild is set
if (this.guild == null) {
if (simpleCommand.guild == null) {
// Return an error message
replyErrorEmbed("Command denied", "This command can only be used in the main guild");
return;
}

// Check if the guild is the main guild
if (this.guild != SimpleBot.getMainGuild()) {
if (simpleCommand.guild != SimpleBot.getMainGuild()) {
// Return an error message
replyErrorEmbed("Command denied", "This command can only be used in the main guild");
return;
}
}

// Check if the command is in cooldown
if (this.cooldownSeconds > 0) {
if (simpleCommand.cooldownSeconds > 0) {
// Add the user to the cooldown map
this.cooldownMap.put(this.user, System.currentTimeMillis());
simpleCommand.cooldownMap.put(simpleCommand.user, System.currentTimeMillis());
}

// Runs the command
onCommand();
simpleCommand.onCommand();
}

/**
Expand Down
Loading

0 comments on commit eb8d51d

Please sign in to comment.