Skip to content

Commit c25f285

Browse files
committed
role-specific emojis
1 parent b46db57 commit c25f285

File tree

1 file changed

+74
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)