Skip to content

Commit aae8736

Browse files
Merge pull request #369 from danthe1st/custom-role-emojis
role-specific emojis
2 parents b46db57 + 288ab37 commit aae8736

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.javadiscord.javabot.systems.staff_commands.role_emoji;
2+
3+
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
4+
5+
import net.dv8tion.jda.api.Permission;
6+
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
7+
import net.dv8tion.jda.api.interactions.commands.build.Commands;
8+
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
9+
import net.javadiscord.javabot.data.config.BotConfig;
10+
11+
/**
12+
* This class represents the /emoji-admin command.
13+
* This command allows managing emojis which are usable only be members with certain roles.
14+
*/
15+
public class RoleEmojiCommand extends SlashCommand {
16+
17+
/**
18+
* The constructor of this class, which sets the corresponding {@link SlashCommandData}s.
19+
* @param botConfig The main configuration of the bot
20+
* @param addRoleEmojiSubcommand A subcommand allowing to add role-exclusive emojis
21+
*/
22+
public RoleEmojiCommand(BotConfig botConfig, AddRoleEmojiSubcommand addRoleEmojiSubcommand) {
23+
SlashCommandData slashCommandData = Commands.slash("emoji-admin", "Administrative command for managing guild emojis")
24+
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
25+
.setGuildOnly(true);
26+
setSlashCommandData(slashCommandData);
27+
addSubcommands(addRoleEmojiSubcommand);
28+
requireUsers(botConfig.getSystems().getAdminConfig().getAdminUsers());
29+
}
30+
}

0 commit comments

Comments
 (0)