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
packagecom.greazi.old.command;
importcom.greazi.old.SimpleBot;
importcom.greazi.old.Valid;
importcom.greazi.old.debug.Debugger;
importcom.greazi.old.utils.SimpleEmbedBuilder;
importcom.greazi.old.utils.expiringmap.ExpiringMap;
importlombok.Getter;
importnet.dv8tion.jda.api.JDA;
importnet.dv8tion.jda.api.Permission;
importnet.dv8tion.jda.api.entities.Guild;
importnet.dv8tion.jda.api.entities.Member;
importnet.dv8tion.jda.api.entities.Role;
importnet.dv8tion.jda.api.entities.User;
importnet.dv8tion.jda.api.entities.channel.concrete.TextChannel;
importnet.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
importnet.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
importnet.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
importnet.dv8tion.jda.api.hooks.ListenerAdapter;
importnet.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
importnet.dv8tion.jda.api.interactions.commands.build.*;
importjava.nio.channels.Channel;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.List;
importjava.util.concurrent.TimeUnit;
/** * A simple slash command class that can be used to create slash commands * <p> * Create a command by extending this class and overriding the onCommand method * to register the command use {@link #getCommandData()} * <p> * /{command} {sub command group} {sub command} {options...} * <p> * Example: /about or /info user Greazi */publicabstractclassSimpleCommandextendsListenerAdapter {
/** * The command name, eg. /about or /ping */privatefinalStringcommand;
/** * The command description, eg. "Get some info about the bot" */privateStringdescription;
/** * The permissions that are required to run this command */privateList<Permission> permissionList = newArrayList<>();
/** * A list containing all subcommand groups */privatefinalList<SubcommandGroupData> subcommandGroupDataList = newArrayList<>();
/** * A list containing all the subcommands */privatefinalList<SubcommandData> subcommandDataList = newArrayList<>();
/** * A list containing all the options */privatefinalList<OptionData> optionDataList = newArrayList<>();
/** * You can set the cooldown time before executing the command again. This map * stores the player uuid and his last execution of the command. */privatefinalExpiringMap<User, Long> cooldownMap = ExpiringMap.builder().expiration(30, TimeUnit.MINUTES).build();
/** * The command cooldown before we can run this command again */@GetterprivateintcooldownSeconds = 0;
/** * The users that are allowd to bypass this command's cooldown, if it is set */privateList<User> cooldownBypassUsers = null;
/** * The permissions that are allowd to bypass this command's cooldown, if it is set */privateList<Permission> cooldownBypassPermission = null;
/** * A custom message when the user attempts to run this command * within {@link #cooldownSeconds}. By default we use the line below * <p> * TIP: Use {duration} to replace the remaining time till next run */privateStringcooldownMessage = "The command you used is in cooldow you can use this command after {duration}";
/** * Send the cooldownMessage in a embed or as a normal message */privatebooleancooldownMessageAsEmbed = true;
/** * If the command can only be run in the main guild of the bot * <p> * The mainGuild is set inside {@link SimpleBot#setMainGuild(JDA)} */privatebooleanmainGuildOnly = false;
/** * Users that can't use this command */privateList<User> disabledUsers = null;
/** * Roles that can't use this command */privateList<Role> disabledRoles = null;
/** * Channels that aren't allowed to use this command */privateList<Channel> disabledChannels = null;
/** * Users that are allowed use this command */privateList<User> allowedUsers = null;
/** * Roles that are allowed use this command */privateList<Role> allowedRoles = null;
/** * Channels where this command is allowed */privateList<Channel> allowedChannels = null;
// ----------------------------------------------------------------------------------------// Temporary variables// ----------------------------------------------------------------------------------------protectedSlashCommandInteractionEventevent;
/** * The User that ran the command * <p> * This variable is set dynamically when the command is run with the * last known user */protectedUseruser;
/** * The Member that ran the command * <p> * This variable is set dynamically when the command is run with the * last known member */protectedMembermember;
/** * The Guild where the command was run * <p> * This variable is set dynamically when the command is run with the * last known guild */protectedGuildguild;
/** * The Channel where the command was run, only works if the channel is a TextChannel, * otherwise it will be null * <p> * This variable is set dynamically when the command is run with the * last known channel */protectedTextChannelchannel;
/** * The Channel where the command was run, only works if the channel is a ThreadChannel, * otherwise it will be null * <p> * This variable is set dynamically when the command is run with the * last known channel */protectedThreadChannelthreadChannel;
// ----------------------------------------------------------------------------------------/** * The main method to create the command */protectedSimpleCommand(finalStringcommand) {
this.command = command;
//this.description = description;
}
// ----------------------------------------------------------------------// Registration// ----------------------------------------------------------------------publicfinalCommandDatagetCommandData() {
Debugger.debug("SimpleCommand", "Registering command: " + this.getCommand() + " with description: " + this.getDescription());
finalSlashCommandDatacommandData = Commands.slash(this.getCommand(), this.getDescription());
// add sub command groupsfor (finalSubcommandGroupDatasubcommandGroup : getSubcommandGroup()) {
commandData.addSubcommandGroups(subcommandGroup);
}
// add sub commandsfor (finalSubcommandDatasubcommand : getSubcommandGroupDataList()) {
commandData.addSubcommands(subcommand);
}
if (getSubcommandGroupDataList().isEmpty() && getSubcommandGroup().isEmpty()) {
commandData.addOptions(getOptions());
}
finalDefaultMemberPermissionsdefaultMemberPermissions = DefaultMemberPermissions.enabledFor(getPermissions());
commandData.setDefaultPermissions(defaultMemberPermissions);
returncommandData;
}
// ----------------------------------------------------------------------------------------// Execution// ----------------------------------------------------------------------------------------publicfinalvoidexecute(finalSlashCommandInteractionEventevent) {
// Setting the temporary variablesthis.guild = event.getGuild();
if (this.guild == null || this.guild != SimpleBot.getMainGuild()) {
replyErrorEmbed("Command denied", "This command can only be used in the main guild");
return;
}
this.user = event.getUser();
this.member = event.getMember();
// Making sure that the channel is a text channelthis.channel = event.getChannel() instanceofTextChannel ? event.getChannel().asTextChannel() : null;
// Making sure that the channel is a thread channelthis.threadChannel = event.getChannel() instanceofThreadChannel ? event.getChannel().asThreadChannel() : null;
this.event = event;
// Runs the commandthis.onCommand();
}
/** * Executed when the command is run. You can get the variable sender and args directly, * and use convenience checks in the simple command class. */protectedabstractvoidonCommand();
// ----------------------------------------------------------------------// Temporary variables and safety// ----------------------------------------------------------------------publicStringgetCommand() {
returnthis.command;
}
publicStringgetDescription() {
returnthis.description;
}
publicvoidsetDescription(finalStringdescription) {
this.description = description;
}
publicvoidaddPermission(finalPermissionpermission) {
this.permissionList.add(permission);
}
publicvoidaddPermission(finalPermission... permissions) {
this.permissionList.addAll(Arrays.asList(permissions));
}
publicList<Permission> getPermissions() {
returnthis.permissionList;
}
publicvoidaddSubcommandGroup(finalSubcommandGroupDatasubcommandGroup) {
this.subcommandGroupDataList.add(subcommandGroup);
}
publicvoidaddSubCommandGroup(finalSubcommandGroupData... subcommandGroups) {
this.subcommandGroupDataList.addAll(Arrays.asList(subcommandGroups));
}
publicvoidaddSubcommand(finalSubcommandDatasubcommand) {
this.subcommandDataList.add(subcommand);
}
publicvoidaddSubCommand(finalSubcommandData... subcommands) {
this.subcommandDataList.addAll(Arrays.asList(subcommands));
}
publicvoidaddOption(finalOptionDataoption) {
this.optionDataList.add(option);
}
publicvoidaddOption(finalOptionData... options) {
this.optionDataList.addAll(Arrays.asList(options));
}
publicList<SubcommandGroupData> getSubcommandGroup() {
returnthis.subcommandGroupDataList;
}
publicList<SubcommandData> getSubcommandGroupDataList() {
returnthis.subcommandDataList;
}
publicList<OptionData> getOptions() {
returnthis.optionDataList;
}
publicUsergetUser() {
returnthis.user;
}
publicMembergetMember() {
returnthis.member;
}
publicGuildgetGuild() {
returnthis.guild;
}
publicSlashCommandInteractionEventgetEvent() {
returnthis.event;
}
publicGuildMessageChannelgetChannel() {
if (this.channel == null && this.threadChannel != null) {
returnthis.threadChannel;
} else {
returnthis.channel;
}
}
/** * Set the time before the same user can execute this command again * * @param cooldown * @param unit */protectedfinalvoidsetCooldown(finalintcooldown, finalTimeUnitunit) {
Valid.checkBoolean(cooldown >= 0, "Cooldown must be >= 0 for /" + this.getCommand());
this.cooldownSeconds = (int) unit.toSeconds(cooldown);
}
protectedfinalvoidsetCooldownMessage(finalStringcooldownMessage) {
this.cooldownMessage = cooldownMessage;
}
protectedfinalvoidsendCooldownMessageAsEmbed(finalbooleancooldownMessageAsEmbed) {
this.cooldownMessageAsEmbed = cooldownMessageAsEmbed;
}
protectedfinalvoidsetCooldownBypassUsers(finalList<User> cooldownBypassUsers) {
this.cooldownBypassUsers = cooldownBypassUsers;
}
protectedfinalvoidsetCooldownBypassPermission(finalList<Permission> cooldownBypassPermission) {
this.cooldownBypassPermission = cooldownBypassPermission;
}
protectedfinalvoidsetMainGuildOnly(finalbooleanmainGuildOnly) {
this.mainGuildOnly = mainGuildOnly;
}
protectedfinalbooleanisMainGuildOnly() {
// Make sure that the bot has and is in the main guildif (SimpleBot.getMainGuild() == null)
returnfalse;
returnthis.mainGuildOnly;
}
protectedfinalvoidsetDisabledUsers(finalList<User> disabledUsers) {
this.disabledUsers = disabledUsers;
}
protectedfinalList<User> getDisabledUsers() {
returnthis.disabledUsers;
}
protectedfinalvoidsetDisabledRoles(finalList<Role> disabledRoles) {
this.disabledRoles = disabledRoles;
}
protectedfinalList<Role> getDisabledRoles() {
returnthis.disabledRoles;
}
protectedfinalvoidsetDisabledChannels(finalList<Channel> disabledChannels) {
this.disabledChannels = disabledChannels;
}
protectedfinalList<Channel> getDisabledChannels() {
returnthis.disabledChannels;
}
protectedfinalvoidsetAllowedUsers(finalList<User> allowedUsers) {
this.allowedUsers = allowedUsers;
}
protectedfinalList<User> getAllowedUsers() {
returnthis.allowedUsers;
}
protectedfinalvoidsetAllowedRoles(finalList<Role> allowedRoles) {
this.allowedRoles = allowedRoles;
}
protectedfinalList<Role> getAllowedRoles() {
returnthis.allowedRoles;
}
protectedfinalvoidsetAllowedChannels(finalList<Channel> allowedChannels) {
this.allowedChannels = allowedChannels;
}
protectedfinalList<Channel> getAllowedChannels() {
returnthis.allowedChannels;
}
// ----------------------------------------------------------------------// Messaging// ----------------------------------------------------------------------/** * Sends reply message * * @param message String */protectedfinalvoidreply(finalStringmessage) {
this.reply(false, message);
}
/** * Sends a private reply message * * @param message String */protectedfinalvoidreplyPrivate(finalStringmessage) {
this.reply(true, message);
}
/** * Sends a multiline reply message * * @param messages String... */protectedfinalvoidreply(finalbooleanephemeral, String... messages) {
if (messages == null)
return;
try {
messages = this.replacePlaceholders(messages);
getEvent().reply(String.join("\n", messages)).setEphemeral(ephemeral).queue();
} finally {
getChannel().sendMessage(String.join("\n", messages)).queue();
}
}
// ----------------------------------------------------------------------/** * Reply with an success embed message * * @param title String * @param messages String... */protectedfinalvoidreplySuccessEmbed(finalStringtitle, finalString... messages) {
replyEmbed(newSimpleEmbedBuilder(this.replacePlaceholders(title)).text(this.replacePlaceholders(messages)).success());
}
/** * Reply with an error embed message * * @param title String * @param messages String... */protectedfinalvoidreplyErrorEmbed(finalStringtitle, finalString... messages) {
replyEmbed(newSimpleEmbedBuilder(this.replacePlaceholders(title)).text(this.replacePlaceholders(messages)).error());
}
/** * Reply with an embed message * * @param title String * @param messages String... */protectedfinalvoidreplyEmbed(finalStringtitle, finalString... messages) {
replyEmbed(newSimpleEmbedBuilder(this.replacePlaceholders(title)).text(this.replacePlaceholders(messages)));
}
/** * Reply with an embed message * * @param embed SimpleEmbedBuilder */protectedfinalvoidreplyEmbed(finalSimpleEmbedBuilderembed) {
replyEmbed(embed, false);
}
// ----------------------------------------------------------------------/** * Reply with a private success embed message * * @param title String * @param messages String... */protectedfinalvoidreplyPrivateSuccessEmbed(finalStringtitle, finalString... messages) {
replyPrivateEmbed(newSimpleEmbedBuilder(this.replacePlaceholders(title)).text(this.replacePlaceholders(messages)).success());
}
/** * Reply with an private error embed message * * @param title String * @param messages String... */protectedfinalvoidreplyPrivateErrorEmbed(finalStringtitle, finalString... messages) {
replyPrivateEmbed(newSimpleEmbedBuilder(this.replacePlaceholders(title)).text(this.replacePlaceholders(messages)).error());
}
/** * Reply with an private embed message * * @param title String * @param messages String... */protectedfinalvoidreplyPrivateEmbed(finalStringtitle, finalString... messages) {
replyPrivateEmbed(newSimpleEmbedBuilder(this.replacePlaceholders(title)).text(this.replacePlaceholders(messages)));
}
/** * Reply with an private embed message * * @param embed SimpleEmbedBuilder */protectedfinalvoidreplyPrivateEmbed(finalSimpleEmbedBuilderembed) {
replyEmbed(embed, true);
}
/** * Reply with an embed message * * @param embed SimpleEmbedBuilder * @param ephemeral boolean */protectedfinalvoidreplyEmbed(finalSimpleEmbedBuilderembed, finalbooleanephemeral) {
if (embed == null)
return;
try {
getEvent().replyEmbeds(embed.build()).setEphemeral(ephemeral).queue();
} finally {
getChannel().sendMessageEmbeds(embed.build()).queue();
}
}
// ----------------------------------------------------------------------// Placeholder// ----------------------------------------------------------------------/** * Replaces placeholders in all messages * To change them override {@link #replacePlaceholders(String)} * * @param messages * @return */protectedfinalString[] replacePlaceholders(finalString[] messages) {
for (inti = 0; i < messages.length; i++)
messages[i] = this.replacePlaceholders(messages[i]);
returnmessages;
}
/** * Replaces placeholders in the message * * @param message * @return */protectedStringreplacePlaceholders(Stringmessage) {
// Replace basic labelsmessage = this.replaceBasicPlaceholders0(message);
/*// Replace {X} with arguments for (int i = 0; i < this.args.length; i++) message = message.replace("{" + i + "}", Common.getOrEmpty(this.args[i]));*/returnmessage;
}
/** * Internal method for replacing {label} and {sublabel} * * @param message * @return */privateStringreplaceBasicPlaceholders0(Stringmessage) {
// First, replace label and sublabelmessage = message
.replace("{label}", this.getCommand());
// Replace hard variables// TODO: Add hard variables herereturnmessage;
}
}
The text was updated successfully, but these errors were encountered:
Discord_Bot_Foundation/src/main/java/com/greazi/old/command/SimpleCommand.java
Line 629 in 0b71ab4
The text was updated successfully, but these errors were encountered: