|
3 | 3 | import com.google.gson.JsonObject; |
4 | 4 | import com.google.gson.JsonParser; |
5 | 5 | import com.javadiscord.javabot.commands.SlashCommandHandler; |
6 | | -import com.javadiscord.javabot.events.SubmissionListener; |
7 | 6 | import com.javadiscord.javabot.other.Constants; |
8 | 7 | import com.javadiscord.javabot.properties.command.CommandConfig; |
9 | 8 | import com.javadiscord.javabot.properties.command.CommandDataConfig; |
|
13 | 12 | import com.mongodb.client.MongoDatabase; |
14 | 13 | import net.dv8tion.jda.api.EmbedBuilder; |
15 | 14 | import net.dv8tion.jda.api.entities.Guild; |
16 | | -import net.dv8tion.jda.api.entities.Member; |
17 | | -import net.dv8tion.jda.api.entities.Role; |
18 | | -import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; |
19 | 15 | import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
20 | 16 | import net.dv8tion.jda.api.hooks.ListenerAdapter; |
21 | 17 | import net.dv8tion.jda.api.interactions.commands.build.CommandData; |
|
26 | 22 | import java.util.Map; |
27 | 23 |
|
28 | 24 | import static com.javadiscord.javabot.events.Startup.mongoClient; |
29 | | -import static com.javadiscord.javabot.events.Startup.preferredGuild; |
30 | 25 | import static com.mongodb.client.model.Filters.eq; |
31 | 26 |
|
| 27 | +/** |
| 28 | + * This listener is responsible for handling slash commands sent by users in |
| 29 | + * guilds where the bot is active, and responding to them by calling the |
| 30 | + * appropriate {@link SlashCommandHandler}. |
| 31 | + * <p> |
| 32 | + * The list of valid commands, and their associated handlers, are defined in |
| 33 | + * the commands.yaml file under the resources directory. |
| 34 | + * </p> |
| 35 | + */ |
32 | 36 | public class SlashCommands extends ListenerAdapter { |
33 | 37 | /** |
34 | 38 | * Maps every command name and alias to an instance of the command, for |
@@ -69,6 +73,16 @@ public void onSlashCommand(SlashCommandEvent event) { |
69 | 73 | } |
70 | 74 | } |
71 | 75 |
|
| 76 | + /** |
| 77 | + * Registers all slash commands defined in commands.yaml for the given guild |
| 78 | + * so that users can see the commands when they type a "/". |
| 79 | + * <p> |
| 80 | + * It does this by attempting to add an entry to {@link SlashCommands#commandsIndex} |
| 81 | + * whose key is the command name, and whose value is a new instance of |
| 82 | + * the handler class which the command has specified. |
| 83 | + * </p> |
| 84 | + * @param guild The guild to update commands for. |
| 85 | + */ |
72 | 86 | public void registerSlashCommands(Guild guild) { |
73 | 87 | CommandListUpdateAction commands = guild.updateCommands(); |
74 | 88 |
|
@@ -103,80 +117,4 @@ public void registerSlashCommands(Guild guild) { |
103 | 117 |
|
104 | 118 | commands.queue(); |
105 | 119 | } |
106 | | - |
107 | | - @Override |
108 | | - public void onButtonClick(ButtonClickEvent event) { |
109 | | - if (event.getUser().isBot()) return; |
110 | | - |
111 | | - String[] id = event.getComponentId().split(":"); |
112 | | - event.deferEdit().queue(); |
113 | | - |
114 | | - JsonObject root; |
115 | | - Document document; |
116 | | - |
117 | | - Guild guild = preferredGuild; |
118 | | - |
119 | | - MongoDatabase database = mongoClient.getDatabase("other"); |
120 | | - MongoCollection<Document> reactionroles = database.getCollection("reactionroles"); |
121 | | - MongoCollection<Document> openSubmissions = database.getCollection("open_submissions"); |
122 | | - MongoCollection<Document> submissionMessages = database.getCollection("submission_messages"); |
123 | | - |
124 | | - switch (id[0]) { |
125 | | - case "dm-submission": |
126 | | - |
127 | | - document = openSubmissions.find(eq("guild_id", guild.getId())).first(); |
128 | | - |
129 | | - root = JsonParser.parseString(document.toJson()).getAsJsonObject(); |
130 | | - String text = root.get("text").getAsString(); |
131 | | - |
132 | | - switch (id[1]) { |
133 | | - case "send": new SubmissionListener().dmSubmissionSend(event, text); break; |
134 | | - case "cancel": new SubmissionListener().dmSubmissionCancel(event); break; |
135 | | - } |
136 | | - openSubmissions.deleteOne(document); |
137 | | - break; |
138 | | - |
139 | | - case "submission": |
140 | | - |
141 | | - document = submissionMessages.find(eq("guild_id", guild.getId())).first(); |
142 | | - |
143 | | - root = JsonParser.parseString(document.toJson()).getAsJsonObject(); |
144 | | - String userID = root.get("user_id").getAsString(); |
145 | | - |
146 | | - switch (id[1]) { |
147 | | - case "approve": new SubmissionListener().submissionApprove(event, userID); break; |
148 | | - case "decline": new SubmissionListener().submissionDecline(event); break; |
149 | | - } |
150 | | - submissionMessages.deleteOne(document); |
151 | | - break; |
152 | | - |
153 | | - case "reactionroles": |
154 | | - |
155 | | - String messageID = id[1]; |
156 | | - String buttonLabel = id[2]; |
157 | | - |
158 | | - Member member = event.getGuild().retrieveMemberById(event.getUser().getId()).complete(); |
159 | | - |
160 | | - BasicDBObject criteria = new BasicDBObject() |
161 | | - .append("guild_id", event.getGuild().getId()) |
162 | | - .append("message_id", messageID) |
163 | | - .append("button_label", buttonLabel); |
164 | | - |
165 | | - String JSON = reactionroles.find(criteria).first().toJson(); |
166 | | - |
167 | | - JsonObject Root = JsonParser.parseString(JSON).getAsJsonObject(); |
168 | | - String roleID = Root.get("role_id").getAsString(); |
169 | | - |
170 | | - Role role = event.getGuild().getRoleById(roleID); |
171 | | - |
172 | | - if (member.getRoles().contains(role)) { |
173 | | - event.getGuild().removeRoleFromMember(member, role).queue(); |
174 | | - event.getHook().sendMessage("Removed Role: " + role.getAsMention()).setEphemeral(true).queue(); |
175 | | - } else { |
176 | | - event.getGuild().addRoleToMember(member, role).queue(); |
177 | | - event.getHook().sendMessage("Added Role: " + role.getAsMention()).setEphemeral(true).queue(); |
178 | | - } |
179 | | - break; |
180 | | - } |
181 | | - } |
182 | 120 | } |
0 commit comments