|
1 | 1 | package com.javadiscord.javabot.commands.reaction_roles; |
2 | 2 |
|
3 | | -import com.google.gson.JsonObject; |
4 | | -import com.google.gson.JsonParser; |
| 3 | +import com.javadiscord.javabot.commands.DelegatingCommandHandler; |
5 | 4 | import com.javadiscord.javabot.commands.Responses; |
6 | | -import com.javadiscord.javabot.commands.SlashCommandHandler; |
7 | | -import com.javadiscord.javabot.other.Constants; |
8 | | -import com.javadiscord.javabot.other.Misc; |
9 | | -import com.mongodb.BasicDBObject; |
10 | | -import com.mongodb.client.MongoCollection; |
11 | | -import com.mongodb.client.MongoCursor; |
12 | | -import com.mongodb.client.MongoDatabase; |
13 | | -import net.dv8tion.jda.api.EmbedBuilder; |
14 | | -import net.dv8tion.jda.api.entities.Emoji; |
15 | | -import net.dv8tion.jda.api.entities.Message; |
16 | | -import net.dv8tion.jda.api.entities.MessageChannel; |
17 | | -import net.dv8tion.jda.api.entities.Role; |
| 5 | +import com.javadiscord.javabot.commands.reaction_roles.subcommands.CreateReactionRole; |
| 6 | +import com.javadiscord.javabot.commands.reaction_roles.subcommands.DeleteReactionRole; |
| 7 | +import com.javadiscord.javabot.commands.reaction_roles.subcommands.ListReactionRoles; |
18 | 8 | import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
19 | | -import net.dv8tion.jda.api.interactions.components.Button; |
20 | | -import net.dv8tion.jda.api.interactions.components.ButtonStyle; |
21 | 9 | import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction; |
22 | | -import org.bson.Document; |
23 | 10 |
|
24 | | -import java.util.ArrayList; |
25 | | -import java.util.Date; |
26 | | -import java.util.List; |
| 11 | +public class ReactionRoles extends DelegatingCommandHandler { |
27 | 12 |
|
28 | | -import static com.javadiscord.javabot.events.Startup.mongoClient; |
29 | | -import static com.mongodb.client.model.Filters.eq; |
| 13 | + public ReactionRoles() { |
| 14 | + addSubcommand("create", new CreateReactionRole()); |
| 15 | + addSubcommand("delete", new DeleteReactionRole()); |
| 16 | + } |
30 | 17 |
|
31 | | -// TODO: Redo implementation of this to be much cleaner. |
32 | | -@Deprecated(forRemoval = true) |
33 | | -public class ReactionRoles implements SlashCommandHandler { |
34 | 18 | @Override |
35 | 19 | public ReplyAction handle(SlashCommandEvent event) { |
36 | | - return switch (event.getSubcommandName()) { |
37 | | - case "create" -> create(event, |
38 | | - event.getOption("channel").getAsMessageChannel(), |
39 | | - event.getOption("messageid").getAsString(), |
40 | | - event.getOption("emote").getAsString(), |
41 | | - event.getOption("button-label").getAsString(), |
42 | | - event.getOption("role").getAsRole()); |
43 | | - case "delete" -> delete(event, |
44 | | - event.getOption("messageid").getAsString(), |
45 | | - event.getOption("button-label").getAsString(), |
46 | | - event.getOption("emote").getAsString()); |
47 | | - default -> Responses.warning(event, "Unknown subcommand."); |
48 | | - }; |
49 | | - } |
50 | | - |
51 | | - private ReplyAction create(SlashCommandEvent event, MessageChannel channel, String mID, String emote, String buttonLabel, Role role) { |
52 | | - |
53 | | - MongoDatabase database = mongoClient.getDatabase("other"); |
54 | | - MongoCollection<Document> collection = database.getCollection("reactionroles"); |
55 | | - |
56 | | - BasicDBObject criteria = new BasicDBObject() |
57 | | - .append("guild_id", event.getGuild().getId()) |
58 | | - .append("channel_id", channel.getId()) |
59 | | - .append("message_id", mID) |
60 | | - .append("button_label", buttonLabel) |
61 | | - .append("emote", emote); |
62 | | - |
63 | | - if (collection.find(criteria).first() == null) { |
64 | | - |
65 | | - Document doc = new Document() |
66 | | - .append("guild_id", event.getGuild().getId()) |
67 | | - .append("channel_id", channel.getId()) |
68 | | - .append("message_id", mID) |
69 | | - .append("role_id", role.getId()) |
70 | | - .append("button_label", buttonLabel) |
71 | | - .append("emote", emote); |
72 | | - |
73 | | - collection.insertOne(doc); |
74 | | - var e = new EmbedBuilder() |
75 | | - .setTitle("Reaction Role created") |
76 | | - .addField("Channel", "<#" + channel.getId() + ">", true) |
77 | | - .addField("Role", role.getAsMention(), true) |
78 | | - .addField("MessageID", "```" + mID + "```", false) |
79 | | - .addField("Emote", "```" + emote + "```", true) |
80 | | - .addField("Button Label", "```" + buttonLabel + "```", true) |
81 | | - .setColor(Constants.GRAY) |
82 | | - .setFooter(event.getUser().getAsTag(), event.getUser().getEffectiveAvatarUrl()) |
83 | | - .setTimestamp(new Date().toInstant()) |
84 | | - .build(); |
85 | 20 |
|
86 | | - |
87 | | - Misc.sendToLog(event.getGuild(), e); |
88 | | - |
89 | | - channel.retrieveMessageById(mID).queue(this::updateMessageComponents); |
90 | | - return event.replyEmbeds(e).setEphemeral(true); |
91 | | - } else return Responses.error(event, |
92 | | - "A Reaction Role on message `" + mID + "` with emote `" + emote + "` and Button Label `" + buttonLabel + "` already exists."); |
93 | | - } |
94 | | - |
95 | | - private ReplyAction delete(SlashCommandEvent event, String mID, String buttonLabel, String emote) { |
96 | | - |
97 | | - MongoDatabase database = mongoClient.getDatabase("other"); |
98 | | - MongoCollection<Document> collection = database.getCollection("reactionroles"); |
99 | | - |
100 | | - BasicDBObject criteria = new BasicDBObject() |
101 | | - .append("guild_id", event.getGuild().getId()) |
102 | | - .append("message_id", mID) |
103 | | - .append("emote", emote) |
104 | | - .append("button_label", buttonLabel); |
105 | | - |
106 | | - try { |
107 | | - collection.find(criteria).first().toJson(); |
108 | | - } catch (NullPointerException e) { |
109 | | - return Responses.error(event, |
110 | | - "A Reaction Role on message `" + mID + "` with emote `" + emote + "` and Button Label `" + buttonLabel + "` does not exist."); |
111 | | - } |
112 | | - |
113 | | - collection.deleteOne(criteria); |
114 | | - |
115 | | - var e = new EmbedBuilder() |
116 | | - .setTitle("Reaction Role removed") |
117 | | - .addField("MessageID", "```" + mID + "```", false) |
118 | | - .addField("Emote", "```" + emote + "```", true) |
119 | | - .addField("Button Label", "```" + buttonLabel + "```", true) |
120 | | - .setColor(Constants.GRAY) |
121 | | - .setFooter(event.getUser().getAsTag(), event.getUser().getEffectiveAvatarUrl()) |
122 | | - .setTimestamp(new Date().toInstant()) |
123 | | - .build(); |
124 | | - |
125 | | - |
126 | | - Misc.sendToLog(event.getGuild(), e); |
127 | | - |
128 | | - event.getChannel().retrieveMessageById(mID).queue(this::updateMessageComponents); |
129 | | - return event.replyEmbeds(e).setEphemeral(true); |
130 | | - } |
131 | | - |
132 | | - void updateMessageComponents (Message message) { |
133 | | - |
134 | | - MongoDatabase database = mongoClient.getDatabase("other"); |
135 | | - MongoCollection<Document> collection = database.getCollection("reactionroles"); |
136 | | - |
137 | | - List<Button> buttons = new ArrayList<>(); |
138 | | - MongoCursor<Document> it = collection.find(eq("message_id", message.getId())).iterator(); |
139 | | - |
140 | | - while (it.hasNext()) { |
141 | | - |
142 | | - JsonObject root = JsonParser.parseString(it.next().toJson()).getAsJsonObject(); |
143 | | - String label = root.get("button_label").getAsString(); |
144 | | - String emote = root.get("emote").getAsString(); |
145 | | - Emoji emoji = Emoji.fromMarkdown(emote); |
146 | | - |
147 | | - buttons.add(Button.of(ButtonStyle.SECONDARY, "reactionroles:" + message.getId() + ":" + label + ":" + emoji.getId(), label, emoji)); |
148 | | - } |
149 | | - |
150 | | - message.editMessageEmbeds(message.getEmbeds().get(0)).setActionRow(buttons.toArray(new Button[0])).queue(); |
| 21 | + try { return super.handle(event); |
| 22 | + } catch (Exception e) { return Responses.error(event, "```" + e.getMessage() + "```"); } |
151 | 23 | } |
152 | 24 | } |
| 25 | + |
0 commit comments