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.handlers.buttons.SimpleButton;
importcom.greazi.discordbotfoundation.settings.SimpleSettings;
importcom.greazi.discordbotfoundation.utils.SimpleEmbedBuilder;
importcom.greazi.discordbotfoundation.utils.color.ConsoleColor;
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.Command;
importnet.dv8tion.jda.api.interactions.commands.build.*;
importnet.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege;
importorg.jetbrains.annotations.NotNull;
importjava.awt.*;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Objects;
/** * 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(){
Debugger.debug("SlashCommand", "Slash Command main method");
SimpleBot.getJDA().addEventListener(this);
}
/** * Add a slash command to the SlashCommand list * @param module The SlashCommand module * @return this */publicSlashCommandHandleraddCommand(SimpleSlashCommandmodule) {
// Retrieve the slash command dataSlashCommandDatacommand = Commands.slash(module.getCommand(), module.getDescription());
Debugger.debug("SlashCommand", "Adding new slash command: " + command.getName());
// add sub commandsList<SubcommandData> moduleSubcommands = module.getSubCommands();
for (SubcommandDatavar : moduleSubcommands) {
command.addSubcommands(var);
}
// Add sub command groupsList<SubcommandGroupData> moduleSubcommandGroup = module.getSubcommandGroup();
for (SubcommandGroupDatavar : moduleSubcommandGroup) {
command.addSubcommandGroups(var);
}
// Get options if sub commands are emptyif (module.getSubCommands().isEmpty() && module.getSubcommandGroup().isEmpty() && !module.getOptions().isEmpty()){
List<OptionData> moduleOptions = module.getOptions();
for (OptionDatavar : moduleOptions) {
command.addOptions(var);
}
}
// Set default enabledcommand.setDefaultEnabled(module.getDefaultEnabled());
// Set descriptioncommand.setDescription(module.getDescription());
Debugger.debug("SlashCommand", "Added slash command: " + command.getName());
// 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("SlashCommand", "Guild only for: " + command.getName());
} else {
publicSlashCommands.add(command);
Debugger.debug("SlashCommand", "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");
// A for loop to loop through all commands to really see what commands get registeredfor(SlashCommandDatacommandData : mainGuildSlashCommands){
Debugger.debug("UpdateCommands", "Register command " + commandData.getName() + " on main guild");
}
for(SlashCommandDatacommandData : publicSlashCommands){
Debugger.debug("UpdateCommands", "Register command " + commandData.getName() + " publicly");
}
// Add all slash commands to the main guildSimpleBot.getGuild().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(@NotNullSlashCommandInteractionEventevent) {
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 runSimpleSlashCommandmodule = 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;
}
// Check if main guild is enabledif (!Objects.requireNonNull(event.getGuild()).getId().equals(SimpleSettings.Bot.MainGuild()) && module.getGuildOnly()){
return;
}
// Check if NSFW channel is enabledif (!event.getTextChannel().isNSFW() && module.getNsfwOnly()){
return;
}
Debugger.debug("SlashCommand", "Executing command logic");
// 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 180 in 335b0ff
The text was updated successfully, but these errors were encountered: