Implement basic slash commands support
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
add9dc0632
commit
fb69dcd863
@ -7,7 +7,9 @@ import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
||||
import wtf.beatrice.hidekobot.listeners.MessageLogger;
|
||||
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
||||
import wtf.beatrice.hidekobot.utils.Logger;
|
||||
import wtf.beatrice.hidekobot.utils.SlashCommandsUtil;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.util.ArrayList;
|
||||
@ -18,7 +20,7 @@ public class HidekoBot
|
||||
{
|
||||
private static String botToken;
|
||||
private static String standardInviteLink =
|
||||
"https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot&permissions=8";
|
||||
"https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8";
|
||||
private static String botUserId;
|
||||
private static final String version = "0.0.1"; // we should probably find a way to make this consistent with Maven
|
||||
|
||||
@ -66,6 +68,10 @@ public class HidekoBot
|
||||
|
||||
// register listeners
|
||||
jda.addEventListener(new MessageListener());
|
||||
jda.addEventListener(new SlashCommandListener());
|
||||
|
||||
// update slash commands
|
||||
SlashCommandsUtil.updateSlashCommands(jda);
|
||||
|
||||
// set the bot's status
|
||||
jda.getPresence().setStatus(OnlineStatus.ONLINE);
|
||||
|
@ -0,0 +1,19 @@
|
||||
package wtf.beatrice.hidekobot.listeners;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SlashCommandListener extends ListenerAdapter
|
||||
{
|
||||
|
||||
@Override
|
||||
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event)
|
||||
{
|
||||
if(event.getName().equals("ping"))
|
||||
{
|
||||
event.reply("Pong!").queue();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package wtf.beatrice.hidekobot.utils;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SlashCommandsUtil
|
||||
{
|
||||
|
||||
private static final Logger logger = new Logger(MessageListener.class);
|
||||
|
||||
static List<CommandData> allCommands = new ArrayList<>()
|
||||
{{
|
||||
add(Commands.slash("ping", "Test if the bot is responsive."));
|
||||
}};
|
||||
|
||||
public static void updateSlashCommands(JDA jdaInstance)
|
||||
{
|
||||
List<CommandData> toAdd = new ArrayList<>();
|
||||
List<Command> toDelete = new ArrayList<>();
|
||||
|
||||
List<Command> registeredCommands = jdaInstance.retrieveCommands().complete();
|
||||
|
||||
// for each command that we have already registered...
|
||||
for(Command currRegCmd : registeredCommands)
|
||||
{
|
||||
// queue it for removal.
|
||||
boolean toRemove = true;
|
||||
|
||||
// iterate through all "recognized" commands
|
||||
for(CommandData cmdData : allCommands)
|
||||
{
|
||||
// if we find the same command...
|
||||
if(cmdData.getName().equals(currRegCmd.getName()))
|
||||
{
|
||||
// then don't remove it
|
||||
toRemove = false;
|
||||
// and quit the loop since we found it.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if no match was found, queue this command for removal.
|
||||
if(toRemove) toDelete.add(currRegCmd);
|
||||
|
||||
}
|
||||
|
||||
// for each "recognized" valid command
|
||||
for(CommandData currCmdData : allCommands)
|
||||
{
|
||||
// queue it for registration.
|
||||
boolean toRegister = true;
|
||||
|
||||
// iterate through all already registered commands.
|
||||
for(Command cmd : registeredCommands)
|
||||
{
|
||||
// if this command was already registered...
|
||||
if(cmd.getName().equals(currCmdData.getName()))
|
||||
{
|
||||
// flag that we don't need to register it
|
||||
toRegister = false;
|
||||
// and quit the loop since we found a match.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if no match was found, queue this command for registration.
|
||||
if(toRegister) toAdd.add(currCmdData);
|
||||
}
|
||||
|
||||
logger.log("Found " + registeredCommands.size() + " commands.");
|
||||
|
||||
// remove all commands queued for removal.
|
||||
for(Command cmd : toDelete)
|
||||
{
|
||||
jdaInstance.deleteCommandById(cmd.getId()).queue();
|
||||
}
|
||||
|
||||
logger.log("Deleted " + toDelete.size() + " commands.");
|
||||
|
||||
// register all new commands.
|
||||
jdaInstance.updateCommands().addCommands(toAdd).queue();
|
||||
logger.log("Registered " + toAdd.size() + " new commands.");
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user