|
33 | 33 | import net.dv8tion.jda.api.events.ReadyEvent; |
34 | 34 | import net.dv8tion.jda.api.exceptions.ErrorHandler; |
35 | 35 | import net.dv8tion.jda.api.hooks.EventListener; |
36 | | -import net.dv8tion.jda.api.interactions.commands.Command; |
37 | 36 | import net.dv8tion.jda.api.requests.ErrorResponse; |
38 | | -import net.dv8tion.jda.api.requests.RestAction; |
39 | 37 | import org.jetbrains.annotations.Nullable; |
40 | 38 | import org.slf4j.Logger; |
41 | 39 | import org.slf4j.LoggerFactory; |
42 | 40 |
|
43 | 41 | import java.util.Collection; |
44 | 42 | import java.util.List; |
45 | 43 | import java.util.Objects; |
46 | | -import java.util.stream.Collectors; |
47 | | -import java.util.stream.LongStream; |
| 44 | +import java.util.stream.Stream; |
48 | 45 |
|
49 | 46 | /** |
50 | 47 | * Utility class for upserting commands. <br> |
@@ -110,68 +107,34 @@ public void upsertCommands(@NonNull final JDA jda) { |
110 | 107 | if (forceGuild) { |
111 | 108 | final var guild = Objects.requireNonNull(guildId).resolve(jda::getGuildById); |
112 | 109 | if (guild == null) throw new NullPointerException("Unknown guild with ID: " + guildId); |
113 | | - guild.retrieveCommands().queue(commands -> { |
114 | | - // Delete old commands. |
115 | | - final var toRemove = getCommandsToRemove(commands); |
116 | | - if (toRemove.length > 0) { |
117 | | - RestAction.allOf(LongStream.of(toRemove).mapToObj(guild::deleteCommandById).toList()).queue(); |
118 | | - } |
119 | | - |
120 | | - // Upsert new ones |
121 | | - RestAction.allOf(client.getSlashCommands().stream() |
122 | | - .map(SlashCommand::buildCommandData) |
123 | | - .map(guild::upsertCommand) |
124 | | - .collect(Collectors.toSet())) |
125 | | - .queue(cmds -> LOG.info("Registered {} commands to guild '{}' ({})", cmds.size(), guild.getName(), guild.getId()), createErrorHandler(guild)); |
126 | 110 |
|
127 | | - if (!client.getContextMenus().isEmpty()) { |
128 | | - // Upsert menus |
129 | | - RestAction.allOf(client.getContextMenus().stream() |
130 | | - .map(ContextMenu::buildCommandData) |
131 | | - .map(guild::upsertCommand) |
132 | | - .collect(Collectors.toSet())) |
133 | | - .queue(cmds -> LOG.info("Registered {} context menus to guild '{}' ({})", cmds.size(), guild.getName(), guild.getId()), createErrorHandler(guild)); |
134 | | - } |
135 | | - }); |
| 111 | + // Update the guild commands |
| 112 | + guild.updateCommands() |
| 113 | + .addCommands(Stream.concat( |
| 114 | + client.getSlashCommands().stream().map(SlashCommand::buildCommandData), |
| 115 | + client.getContextMenus().stream().map(ContextMenu::buildCommandData) |
| 116 | + ).toList()) |
| 117 | + .queue(it -> LOG.info("Registered {} commands to guild '{}' ({}).", it.size(), guild.getName(), guild.getId()), createErrorHandler(guild)); |
136 | 118 | } else { |
137 | 119 | if (guildId != null) { |
138 | 120 | final var guild = guildId.resolve(jda::getGuildById); |
139 | 121 | if (guild != null) { |
140 | 122 | // Guild still specified? Then remove guild commands |
141 | 123 | guild.retrieveCommands().queue(commands -> { |
142 | 124 | if (!commands.isEmpty()) { |
143 | | - RestAction.allOf(commands.stream() |
144 | | - .map(Command::getIdLong) |
145 | | - .map(guild::deleteCommandById) |
146 | | - .toList()).queue(); |
| 125 | + guild.updateCommands().queue(); |
147 | 126 | } |
148 | 127 | }); |
149 | 128 | } |
150 | 129 | } |
151 | | - jda.retrieveCommands().queue(commands -> { |
152 | | - // Delete old commands. |
153 | | - final var toRemove = getCommandsToRemove(commands); |
154 | | - if (toRemove.length > 1) { |
155 | | - RestAction.allOf(LongStream.of(toRemove).mapToObj(jda::deleteCommandById).toList()).queue(); |
156 | | - } |
157 | | - |
158 | | - // Upsert new ones |
159 | | - RestAction.allOf(client.getSlashCommands() |
160 | | - .stream() |
161 | | - .map(SlashCommand::buildCommandData) |
162 | | - .map(jda::upsertCommand) |
163 | | - .collect(Collectors.toSet())) |
164 | | - .queue(cmds -> LOG.info("Registered {} global commands.", cmds.size())); |
165 | 130 |
|
166 | | - if (!client.getContextMenus().isEmpty()) { |
167 | | - // Upsert menus |
168 | | - RestAction.allOf(client.getContextMenus().stream() |
169 | | - .map(ContextMenu::buildCommandData) |
170 | | - .map(jda::upsertCommand) |
171 | | - .collect(Collectors.toSet())) |
172 | | - .queue(cmds -> LOG.info("Registered {} context menus.", cmds.size())); |
173 | | - } |
174 | | - }); |
| 131 | + // Update the global commands |
| 132 | + jda.updateCommands() |
| 133 | + .addCommands(Stream.concat( |
| 134 | + client.getSlashCommands().stream().map(SlashCommand::buildCommandData), |
| 135 | + client.getContextMenus().stream().map(ContextMenu::buildCommandData) |
| 136 | + ).toList()) |
| 137 | + .queue(it -> LOG.info("Registered {} global commands.", it.size())); |
175 | 138 | } |
176 | 139 | } |
177 | 140 |
|
@@ -201,21 +164,4 @@ public ErrorHandler createErrorHandler(final Guild guild) { |
201 | 164 | }); |
202 | 165 | } |
203 | 166 |
|
204 | | - private long[] getCommandsToRemove(final List<Command> existingCommands) { |
205 | | - record ExistingCommand(String name, long id) { |
206 | | - } |
207 | | - final var ext = existingCommands.stream() |
208 | | - .filter(c -> c.getType() == Command.Type.SLASH) |
209 | | - .map(c -> new ExistingCommand(c.getName(), c.getIdLong())) |
210 | | - .collect(Collectors.toSet()); |
211 | | - final var clientCommandNames = client.getSlashCommands().stream().map(SlashCommand::getName).collect(Collectors.toSet()); |
212 | | - ext.removeIf(p -> { |
213 | | - final var contains = clientCommandNames.contains(p.name()); |
214 | | - if (contains) { |
215 | | - clientCommandNames.remove(p.name()); |
216 | | - } |
217 | | - return contains; |
218 | | - }); |
219 | | - return ext.stream().mapToLong(ExistingCommand::id).toArray(); |
220 | | - } |
221 | 167 | } |
0 commit comments