|
| 1 | +package net.javadiscord.javabot.systems.staff_commands.role_emoji; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | + |
| 5 | +import com.dynxsty.dih4jda.interactions.commands.SlashCommand.Subcommand; |
| 6 | + |
| 7 | +import net.dv8tion.jda.api.entities.Message.Attachment; |
| 8 | +import net.dv8tion.jda.api.entities.Role; |
| 9 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 10 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 11 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 12 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 13 | +import net.javadiscord.javabot.data.config.BotConfig; |
| 14 | +import net.javadiscord.javabot.util.Checks; |
| 15 | +import net.javadiscord.javabot.util.Responses; |
| 16 | + |
| 17 | +/** |
| 18 | + * This class represents the /emoji-admin add subcommand. |
| 19 | + * This subcommand allows adding emojis which are usable only be members with certain roles. |
| 20 | + */ |
| 21 | +public class AddRoleEmojiSubcommand extends Subcommand { |
| 22 | + |
| 23 | + private final BotConfig botConfig; |
| 24 | + |
| 25 | + /** |
| 26 | + * The constructor of this class, which sets the corresponding {@link SubcommandData}s. |
| 27 | + * @param botConfig The main configuration of the bot |
| 28 | + */ |
| 29 | + public AddRoleEmojiSubcommand(BotConfig botConfig) { |
| 30 | + this.botConfig=botConfig; |
| 31 | + SubcommandData subCommandData = new SubcommandData("add", "Adds an emoji only usable only with certain roles") |
| 32 | + .addOption(OptionType.STRING, "name", "The name of the emoji", true) |
| 33 | + .addOption(OptionType.ATTACHMENT, "emoji", "the emoji", true); |
| 34 | + for (int i = 1; i <= 10; i++) { |
| 35 | + subCommandData.addOption(OptionType.ROLE, "role-"+i, "A role allowed to use the emoji", i==1); |
| 36 | + } |
| 37 | + setSubcommandData(subCommandData); |
| 38 | + requireUsers(botConfig.getSystems().getAdminConfig().getAdminUsers()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void execute(@NotNull SlashCommandInteractionEvent event) { |
| 43 | + if (!Checks.hasAdminRole(botConfig, event.getMember())) { |
| 44 | + Responses.replyAdminOnly(event, botConfig.get(event.getGuild())).queue(); |
| 45 | + return; |
| 46 | + } |
| 47 | + Attachment attachment = event.getOption("emoji",null, OptionMapping::getAsAttachment); |
| 48 | + Role[] roles = event |
| 49 | + .getOptions() |
| 50 | + .stream() |
| 51 | + .filter(option -> option.getType() == OptionType.ROLE) |
| 52 | + .map(option -> option.getAsRole()) |
| 53 | + .toArray(Role[]::new); |
| 54 | + event.deferReply().queue(); |
| 55 | + attachment.getProxy().downloadAsIcon().thenAccept(icon -> { |
| 56 | + event |
| 57 | + .getGuild() |
| 58 | + .createEmoji(event.getOption("name",attachment.getFileName(), OptionMapping::getAsString), icon, roles) |
| 59 | + .queue(emoji -> { |
| 60 | + event.getHook().sendMessage("Emoji "+emoji.getName()+" successfully created").queue(); |
| 61 | + }, e -> { |
| 62 | + event.getHook().sendMessage("Cannot create emoji because `" + e.getMessage() + "`").queue(); |
| 63 | + }); |
| 64 | + }).exceptionally(e -> { |
| 65 | + event.getHook().sendMessage("Cannot create emoji because `" + e.getMessage() + "`").queue(); |
| 66 | + return null; |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
0 commit comments