Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.mcmoddev.mmdbot.MMDBot;
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdAbout;
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdHelp;
Expand All @@ -41,16 +42,20 @@
import com.mcmoddev.mmdbot.modules.commands.server.CmdRoles;
import com.mcmoddev.mmdbot.modules.commands.server.CmdToggleEventPings;
import com.mcmoddev.mmdbot.modules.commands.server.CmdToggleMcServerPings;
import com.mcmoddev.mmdbot.modules.commands.server.DeletableCommand;
import com.mcmoddev.mmdbot.modules.commands.server.moderation.CmdCommunityChannel;
import com.mcmoddev.mmdbot.modules.commands.server.moderation.CmdMute;
import com.mcmoddev.mmdbot.modules.commands.server.moderation.CmdOldChannels;
import com.mcmoddev.mmdbot.modules.commands.server.moderation.CmdUnmute;
import com.mcmoddev.mmdbot.modules.commands.server.moderation.CmdUser;
import com.mcmoddev.mmdbot.modules.commands.server.quotes.CmdQuote;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdAddTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdEditTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdListTricks;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRemoveTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRunTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRunTrickExplicitly;
import com.mcmoddev.mmdbot.utilities.tricks.Tricks;
import me.shedaniel.linkie.Namespaces;

/**
Expand Down Expand Up @@ -111,9 +116,11 @@ public static void setupCommandModule() {
.addSlashCommand(new CmdUptime())
//TODO Setup DB storage for tricks and polish them off/add permission restrictions for when needed.
.addSlashCommand(new CmdAddTrick())
.addSlashCommand(new CmdEditTrick())
.addSlashCommand(new CmdListTricks())
.addSlashCommand(new CmdRemoveTrick())
.addSlashCommand(new CmdRunTrick())
.addSlashCommand(new CmdRunTrickExplicitly())
.addSlashCommands(Tricks.getTricks().stream().map(CmdRunTrick::new).toArray(SlashCommand[]::new))
.addSlashCommand(new CmdShutdown())
.addSlashCommands(CmdMappings.createCommands()) // TODO: This is broken beyond belief. Consider moving away from linkie. - Curle
.addSlashCommands(CmdTranslateMappings.createCommands())
Expand All @@ -133,4 +140,55 @@ public static void setupCommandModule() {
MMDBot.LOGGER.warn("Command module disabled via config, commands will not work at this time!");
}
}

/**
* Removes a slash command. If the command is a {@link DeletableCommand}, this also marks it as deleted so that if
* somehow it is run (which should be impossible) nothing will happen.
*
* @param name the name of the command to remove
*/
public static void removeCommand(final String name) {
var guild = MMDBot.getInstance().getGuildById(MMDBot.getConfig().getGuildID());
if (guild == null) {
throw new NullPointerException("No Guild found!");
}

commandClient.getSlashCommands().stream()
.filter(cmd -> cmd.getName().equals(name))
.filter(cmd -> cmd instanceof DeletableCommand)
.map(cmd -> (DeletableCommand) cmd)
.forEach(DeletableCommand::delete);

guild.retrieveCommands()
.flatMap(list -> list.stream().filter(cmd -> cmd.getName().equals(name)).findAny().map(cmd -> guild.deleteCommandById(cmd.getId())).orElseThrow())
.queue();
}

/**
* Adds and upserts a slash command.
*
* @param cmd the command
*/
public static void addSlashCommand(final SlashCommand cmd) {
commandClient.addSlashCommand(cmd);
upsertCommand(cmd);
}

/**
* Upserts a slash command.
*
* @param cmd the command
*/
public static void upsertCommand(final SlashCommand cmd) {
if (cmd.isGuildOnly()) {
var guild = MMDBot.getInstance().getGuildById(MMDBot.getConfig().getGuildID());
if (guild == null) {
throw new NullPointerException("No Guild found!");
}

guild.upsertCommand(cmd.buildCommandData()).queue(cmd1 -> cmd1.updatePrivileges(guild, cmd.buildPrivileges(commandClient)).queue());
} else {
MMDBot.getInstance().upsertCommand(cmd.buildCommandData()).queue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
* Copyright (C) 2016-2021 <MMD - MinecraftModDevelopment>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
*/
package com.mcmoddev.mmdbot.modules.commands.server;

/**
* A command which can be disabled on delete.
*/
public interface DeletableCommand {
/**
* Mark this command as deleted.
*/
void delete();

/**
* Unmark this command as deleted.
*/
void restore();

/**
* Gets whether this command is marked as deleted or not.
*
* @return whether the command is marked as deleted
*/
boolean isDeleted();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
import com.jagrosh.jdautilities.command.SlashCommand;
import com.mcmoddev.mmdbot.MMDBot;
import com.mcmoddev.mmdbot.utilities.Utils;
import com.mcmoddev.mmdbot.utilities.tricks.Trick;
import com.mcmoddev.mmdbot.utilities.tricks.Tricks;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

import java.util.List;

/**
* Adds a trick to the list.
Expand All @@ -39,7 +36,7 @@
* - embed;
* - Takes three parameters; name, description and color. All used for constructing the embed.
*
* @author williambl
* @author Will BL
* @author Curle
*/
public final class CmdAddTrick extends SlashCommand {
Expand All @@ -55,101 +52,33 @@ public CmdAddTrick() {
arguments = "(<string> <trick content body> (or) <embed> <title> "
+ "<description> <colour-as-hex-code>";
aliases = new String[]{"add-trick"};
requiredRole = "bot maintainer";
enabledRoles = new String[]{Long.toString(MMDBot.getConfig().getRole("bot_maintainer"))};
guildOnly = true;
// we need to use this unfortunately :( can't create more than one commandclient
//noinspection deprecation
guildId = Long.toString(MMDBot.getConfig().getGuildID());

children = new SlashCommand[] {
new AddStringTrick("string", "Create a string-type Trick.", true,
new OptionData(OptionType.STRING, "names", "Name(s) for the trick. Separate with spaces.").setRequired(true),
new OptionData(OptionType.STRING, "content", "The content of the string-type Trick.").setRequired(true)),
new AddEmbedTrick("embed", "Create an embed-type Trick.", true,
new OptionData(OptionType.STRING, "names", "Name(s) for the trick. Separate with spaces.").setRequired(true),
new OptionData(OptionType.STRING, "title", "Title of the embed.").setRequired(true),
new OptionData(OptionType.STRING, "description", "Description of the embed.").setRequired(true),
new OptionData(OptionType.STRING, "color", "Hex color string in #AABBCC format, used for the embed.").setRequired(true))
};
children = Tricks.getTrickTypes().entrySet().stream().map(entry -> new SubCommand(entry.getKey(), entry.getValue())).toArray(SlashCommand[]::new);
}

/**
* Execute.
*
* @param event the event
*/
@Override
protected void execute(final SlashCommandEvent event) {
if (!Utils.checkCommand(this, event)) {
return;
}

String command = event.getSubcommandName();
String arguments = command.equals("string") ?
event.getOption("names").getAsString() + " | " + event.getOption("content").getAsString() :
event.getOption("names").getAsString() + " | " + event.getOption("title").getAsString() + " | " + event.getOption("description").getAsString() + " | " + event.getOption("color").getAsString();


try {
Tricks.addTrick(Tricks.getTrickType(command).createFromArgs(arguments));
event.reply("Added trick!").mentionRepliedUser(false).setEphemeral(true).queue();
} catch (IllegalArgumentException e) {
event.reply("A command with that name already exists!").mentionRepliedUser(false).setEphemeral(true).queue();
MMDBot.LOGGER.warn("Failure adding trick: {}", e.getMessage());
}
}
protected void execute(final SlashCommandEvent event) {}

/**
* A child command of AddTrick.
* Handles adding string tricks.
*
* Takes the form:
* /addtrick string name1 name2 test something
* /addtrick string [name <name...> ] [content]
* A child command of AddTrick, handles adding a particular type of trick.
*
* @author Curle
* @author Will BL
*/
private class AddStringTrick extends SlashCommand {
public AddStringTrick(String name, String help, boolean guildOnly, OptionData... options) {
this.name = name;
this.help = help;
this.guildOnly = guildOnly;
this.options = List.of(options);
}

@Override
protected void execute(final SlashCommandEvent event) {
if (!Utils.checkCommand(this, event)) {
return;
}

String arguments = event.getOption("names").getAsString() + " | " + event.getOption("content").getAsString();

try {
Tricks.addTrick(Tricks.getTrickType("string").createFromArgs(arguments));
event.reply("Added trick!").mentionRepliedUser(false).setEphemeral(true).queue();
} catch (IllegalArgumentException e) {
event.reply("A command with that name already exists!").mentionRepliedUser(false).setEphemeral(true).queue();
MMDBot.LOGGER.warn("Failure adding trick: {}", e.getMessage());
}
}
}
private static class SubCommand extends SlashCommand {
private final Trick.TrickType<?> trickType;

/**
* A child command of AddTrick.
* Handles adding embed tricks.
*
* Takes the form:
* /addtrick embed name1 name2 test something #AABBCC
* /addtrick embed [name <name...> ] [title] [description] [color]
*
* TODO: Fields.
*
* @author Curle
*/
private class AddEmbedTrick extends SlashCommand {
public AddEmbedTrick(String name, String help, boolean guildOnly, OptionData... options) {
public SubCommand(String name, Trick.TrickType<?> trickType) {
this.trickType = trickType;
this.name = name;
this.help = help;
this.guildOnly = guildOnly;
this.options = List.of(options);
this.help = "Create a "+name+"-type trick.";
this.guildOnly = true;
this.options = trickType.getArgs();
}

@Override
Expand All @@ -158,10 +87,8 @@ protected void execute(final SlashCommandEvent event) {
return;
}

String arguments = event.getOption("names").getAsString() + " | " + event.getOption("title").getAsString() + " | " + event.getOption("description").getAsString() + " | " + event.getOption("color").getAsString();

try {
Tricks.addTrick(Tricks.getTrickType("embed").createFromArgs(arguments));
Tricks.addTrick(trickType.createFromCommand(event));
event.reply("Added trick!").mentionRepliedUser(false).setEphemeral(true).queue();
} catch (IllegalArgumentException e) {
event.reply("A command with that name already exists!").mentionRepliedUser(false).setEphemeral(true).queue();
Expand Down
Loading