You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO: Make the total count better so it will show the actually registered commands with JDA instead of the count
/* * Copyright (c) 2021 - 2022. Greazi - All rights reservered * * Unauthorized copying of this file, via any medium is strictly prohibited * Proprietary and confidential */packagecom.greazi.discordbotfoundation.handlers.commands;
importcom.greazi.discordbotfoundation.Common;
importcom.greazi.discordbotfoundation.SimpleBot;
importcom.greazi.discordbotfoundation.debug.Debugger;
importcom.greazi.discordbotfoundation.utils.SimpleEmbedBuilder;
importcom.greazi.discordbotfoundation.utils.color.ConsoleColor;
importnet.dv8tion.jda.api.entities.Guild;
importnet.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
importnet.dv8tion.jda.api.hooks.ListenerAdapter;
importnet.dv8tion.jda.api.hooks.SubscribeEvent;
importnet.dv8tion.jda.api.interactions.commands.build.*;
importorg.jetbrains.annotations.NotNull;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
/** * The slash command handler that handles the whole slash command event. * Uses the information of {@link SimpleSlashCommand} */publicclassSlashCommandHandlerextendsListenerAdapter {
// The hashmap and list of tall the slash commandsprivatefinalHashMap<String, SimpleSlashCommand> cmdList = newHashMap<>();
privatefinalList<SlashCommandData> publicSlashCommands = newArrayList<>();
privatefinalList<SlashCommandData> mainGuildSlashCommands = newArrayList<>();
/** * The main slash command handler */publicSlashCommandHandler() {
Common.log("Initializing slash command handler");
SimpleBot.getJDA().addEventListener(this);
}
/** * Add a slash command to the SlashCommand list * * @param module The SlashCommand module * @return this */publicSlashCommandHandleraddCommand(finalSimpleSlashCommandmodule) {
// Retrieve the slash command datafinalSlashCommandDatacommand = Commands.slash(module.getCommand(), module.getDescription());
// add sub commandsfinalList<SubcommandData> moduleSubcommands = module.getSubCommands();
for (finalSubcommandDatavar : moduleSubcommands) {
command.addSubcommands(var);
}
// Add sub command groupsfinalList<SubcommandGroupData> moduleSubcommandGroup = module.getSubcommandGroup();
for (finalSubcommandGroupDatavar : moduleSubcommandGroup) {
command.addSubcommandGroups(var);
}
// Get options if sub commands are emptyif (module.getSubCommands().isEmpty() && module.getSubcommandGroup().isEmpty() && !module.getOptions().isEmpty()) {
finalList<OptionData> moduleOptions = module.getOptions();
for (finalOptionDatavar : moduleOptions) {
command.addOptions(var);
}
}
// Set descriptioncommand.setDescription(module.getDescription());
// TODO: When api has an update for the slash command system update it to that system// Check it out here: https://github.com/DV8FromTheWorld/JDA/pull/2113// Add the slash commandif (module.getGuildOnly()) {
mainGuildSlashCommands.add(command);
Debugger.debug("SlashCommandRegistration", "Guild only for: " + command.getName());
} else {
publicSlashCommands.add(command);
Debugger.debug("SlashCommandRegistration", "Public command for: " + command.getName());
}
// Add it to our internal listcmdList.put(module.getCommand(), module);
returnthis;
}
/** * Register all slash commands to JDA */publicvoidregisterCommands() {
// for(Command command : SimpleBot.getJDA().retrieveCommands().complete()) {// SimpleBot.getJDA().deleteCommandById(command.getIdLong());// }// Check if the slash commands isn't emptyif (mainGuildSlashCommands.isEmpty()) Common.warning("Be aware no main guild slash commands can be found!");
if (publicSlashCommands.isEmpty()) Common.warning("Be aware no public slash commands can be found!");
Debugger.debug("SlashCommand", "Registering slash commands");
// Add all slash commands to the main guildSimpleBot.getMainGuild().updateCommands()
.addCommands(mainGuildSlashCommands)
.queue();
// Add the commands to all the guilds PUBLIC!!SimpleBot.getJDA().updateCommands()
.addCommands(publicSlashCommands)
.queue();
}
/** * The main event listener for the slash command interaction event * * @param event SlashCommandInteractionEvent */@Override@SubscribeEventpublicvoidonSlashCommandInteraction(@NotNullfinalSlashCommandInteractionEventevent) {
Debugger.debug("SlashCommand", "A Slash Command has been used");
// Log who used a slash commandCommon.log("User, " + ConsoleColor.CYAN + event.getMember().getEffectiveName() + ConsoleColor.RESET + " used Slash Command: " + ConsoleColor.CYAN + event.getCommandString() + ConsoleColor.RESET);
// Retrieve the command class from the command that has been runfinalSimpleSlashCommandmodule = cmdList.get(event.getName());
// If the module doesn't exist in the bot return an errorif (module == null) {
event.replyEmbeds(newSimpleEmbedBuilder("ERROR - command not found")
.text("The command that you have used does not exist or hasn't been activated!",
"Please contact an admin and report this error!")
.error()
.setFooter("")
.build()).setEphemeral(true).queue();
return;
}
// Get the guild of the button and the main guild of the botfinalGuildguild = event.getGuild();
finalGuildmainGuild = SimpleBot.getMainGuild();
assertguild != null : "Event guild is null!";
// Check if the button is for the main guild onlyif (!guild.getId().equals(mainGuild.getId()) && module.getGuildOnly()) {
event.replyEmbeds(newSimpleEmbedBuilder("ERROR - Button main guild only")
.text(
"The button you used is only usable in the main guild of this bot!",
"If you feel like this is a problem please contact a admin!"
)
.error()
.setFooter("")
.build()).setEphemeral(true).queue();
return;
}
// Run the command logicmodule.execute(event);
}
// TODO: Make the total count better so it will show the actually registered commands with JDA instead of the count// of the lists that are made/** * Get the total amount of slash commands registered * * @return Total amount of slash commands */publicintgetTotal() {
returncmdList.size();
}
/** * Get the total amount of public registered slash commands * * @return Total amount of public slash commands */publicintgetPublicTotal() {
returnpublicSlashCommands.size();
}
/** * Get the total amount of private registered slash commands * * @return Total amount of private slash commands */publicintgetGuildTotal() {
returnmainGuildSlashCommands.size();
}
}
The text was updated successfully, but these errors were encountered:
of the lists that are made
Discord_Bot_Foundation/src/main/java/com/greazi/discordbotfoundation/handlers/commands/SlashCommandHandler.java
Line 171 in 04d4da1
The text was updated successfully, but these errors were encountered: