3 Commits

Author SHA1 Message Date
813107a2f9 Attempt to fix commands getting unregistered
All checks were successful
continuous-integration/drone/push Build is passing
2022-11-20 03:49:53 +01:00
bfb4aae2aa Register clear command 2022-11-20 03:42:59 +01:00
f2dc70569d Implement basic clear-chat slash command 2022-11-20 03:41:51 +01:00
2 changed files with 108 additions and 1 deletions

View File

@@ -1,10 +1,17 @@
package wtf.beatrice.hidekobot.listeners;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageHistory;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.utils.RandomUtil;
import java.util.ArrayList;
import java.util.List;
public class SlashCommandListener extends ListenerAdapter
{
@@ -14,7 +21,9 @@ public class SlashCommandListener extends ListenerAdapter
if (event.getName().equals("ping"))
{
event.reply("Pong!").queue();
} else if (event.getName().equals("coinflip"))
}
else if (event.getName().equals("coinflip"))
{
int rand = RandomUtil.getRandomNumber(0, 1);
String msg;
@@ -29,5 +38,34 @@ public class SlashCommandListener extends ListenerAdapter
event.reply(msg).queue();
}
else if (event.getName().equals("clear"))
{
MessageChannel channel = event.getChannel();
if(!(channel instanceof TextChannel))
{
event.reply("Sorry! I can't delete messages here.").queue();
return;
}
int deleteCount = event.getOption("amount").getAsInt();
if(deleteCount < 2 || deleteCount > 98)
{
event.reply("Sorry! I can't delete that amount of messages!").queue();
return;
}
event.reply("Clearing...").queue();
MessageHistory.MessageRetrieveAction action = channel.getHistoryBefore(event.getInteraction().getIdLong(), deleteCount);
List<Message> messagesUnmodifiable = action.complete().getRetrievedHistory();
List<Message> messages = new ArrayList<>(messagesUnmodifiable);
//more than 2 messages, less than 100 for this method
((TextChannel) channel).deleteMessages(messages).queue();
}
}
}

View File

@@ -1,7 +1,10 @@
package wtf.beatrice.hidekobot.utils;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import wtf.beatrice.hidekobot.HidekoBot;
@@ -19,12 +22,76 @@ public class SlashCommandsUtil
{{
add(Commands.slash("ping", "Test if the bot is responsive."));
add(Commands.slash("coinflip", "Flip a coin and get head or tails."));
add(Commands.slash("clear", "Clear the current channel's chat.")
.addOption(OptionType.INTEGER, "amount", "The amount of messages to delete.")
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MESSAGE_MANAGE)));
}};
public static void updateSlashCommands()
{
JDA jdaInstance = HidekoBot.getAPI();
// get all the already registered commands
List<Command> registeredCommands = jdaInstance.retrieveCommands().complete();
boolean update = false;
// for each command that we have already registered...
for(Command currRegCmd : registeredCommands)
{
// iterate through all "recognized" commands
for(CommandData cmdData : allCommands)
{
// if we find the same command...
if(cmdData.getName().equals(currRegCmd.getName()))
{
// quit the loop since we found it.
break;
}
}
// if no match was found, we need to send an updated command list because
// an old command was probably removed.
update = true;
}
// if an update is not already queued...
if(!update)
{
// for each "recognized" valid command
for(CommandData currCmdData : allCommands)
{
// iterate through all already registered commands.
for(Command cmd : registeredCommands)
{
// if this command was already registered...
if(cmd.getName().equals(currCmdData.getName()))
{
// quit the loop since we found a match.
break;
}
}
// if no match was found, we need to send an updated command list because
// a new command was probably added.
update = true;
}
}
logger.log("Found " + registeredCommands.size() + " commands.");
if(update)
{
// send updated command list.
jdaInstance.updateCommands().addCommands(allCommands).queue();
logger.log("Commands updated. New total: " + allCommands.size() + ".");
}
/*
List<CommandData> toAdd = new ArrayList<>();
List<Command> toDelete = new ArrayList<>();
@@ -91,5 +158,7 @@ public class SlashCommandsUtil
jdaInstance.updateCommands().addCommands(toAdd).queue();
logger.log("Registered " + toAdd.size() + " new commands.");
*/
}
}