Skip to content
This repository was archived by the owner on Jun 9, 2024. It is now read-only.

Commit 1a27110

Browse files
committed
feature: Support context menu commands (closes #584)
1 parent 523d10e commit 1a27110

File tree

2 files changed

+98
-38
lines changed

2 files changed

+98
-38
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2021 amy, All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright notice,
10+
* this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software without
14+
* specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.mewna.catnip.entity.interaction.command;
29+
30+
import lombok.Getter;
31+
import lombok.experimental.Accessors;
32+
33+
/**
34+
* @author amy
35+
* @since 10/10/21.
36+
*/
37+
@Accessors(fluent = true)
38+
public enum ApplicationCommandType {
39+
CHAT_INPUT(1),
40+
USER(2),
41+
MESSAGE(3),
42+
;
43+
44+
@Getter
45+
private final int id;
46+
47+
ApplicationCommandType(final int id) {
48+
this.id = id;
49+
}
50+
}

src/main/java/com/mewna/catnip/rest/handler/RestInteraction.java

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929

3030
import com.grack.nanojson.JsonArray;
3131
import com.grack.nanojson.JsonObject;
32+
import com.mewna.catnip.entity.interaction.InteractionResponseType;
3233
import com.mewna.catnip.entity.interaction.command.ApplicationCommand;
3334
import com.mewna.catnip.entity.interaction.command.ApplicationCommandOption;
34-
import com.mewna.catnip.entity.interaction.InteractionResponseType;
35+
import com.mewna.catnip.entity.interaction.command.ApplicationCommandType;
3536
import com.mewna.catnip.entity.message.MentionParseFlag;
3637
import com.mewna.catnip.entity.message.Message;
3738
import com.mewna.catnip.entity.message.MessageFlag;
@@ -67,34 +68,34 @@ public RestInteraction(final CatnipImpl catnip) {
6768
// Initial response
6869

6970
public Completable createInteractionInitialResponse(@Nonnull final InteractionResponseType type,
70-
@Nonnull final String interactionId,
71-
@Nonnull final String interactionToken,
72-
@Nonnull final MessageOptions options) {
71+
@Nonnull final String interactionId,
72+
@Nonnull final String interactionToken,
73+
@Nonnull final MessageOptions options) {
7374
return createInteractionInitialResponse(type, interactionId, interactionToken, null, null, options);
7475
}
7576

7677
public Completable createInteractionInitialResponse(@Nonnull final InteractionResponseType type,
77-
@Nonnull final String interactionId,
78-
@Nonnull final String interactionToken,
79-
@Nullable final String username,
80-
@Nonnull final MessageOptions options) {
78+
@Nonnull final String interactionId,
79+
@Nonnull final String interactionToken,
80+
@Nullable final String username,
81+
@Nonnull final MessageOptions options) {
8182
return createInteractionInitialResponse(type, interactionId, interactionToken, username, null, options);
8283
}
8384

8485
public Completable createInteractionInitialResponse(@Nonnull final InteractionResponseType type,
85-
@Nonnull final String interactionId,
86-
@Nonnull final String interactionToken,
87-
@Nullable final String username, @Nullable final String avatarUrl,
88-
@Nonnull final MessageOptions options) {
86+
@Nonnull final String interactionId,
87+
@Nonnull final String interactionToken,
88+
@Nullable final String username, @Nullable final String avatarUrl,
89+
@Nonnull final MessageOptions options) {
8990
return createInteractionInitialResponseRaw(type, interactionId, interactionToken, username, avatarUrl, options);
9091
}
9192

9293
public Completable createInteractionInitialResponseRaw(@Nonnull final InteractionResponseType type,
93-
@Nonnull final String interactionId,
94-
@Nonnull final String interactionToken,
95-
@Nullable final String username,
96-
@Nullable final String avatarUrl,
97-
@Nonnull final MessageOptions options) {
94+
@Nonnull final String interactionId,
95+
@Nonnull final String interactionToken,
96+
@Nullable final String username,
97+
@Nullable final String avatarUrl,
98+
@Nonnull final MessageOptions options) {
9899
final var body = createSendBody(type, username, avatarUrl, options);
99100
return Completable.fromObservable(catnip().requester().
100101
queue(new OutboundRequest(Routes.CREATE_INTERACTION_INITIAL_RESPONSE.withMajorParam(interactionId)
@@ -231,34 +232,38 @@ public Observable<ApplicationCommand> getGlobalApplicationCommands() {
231232

232233
public Observable<JsonArray> getGlobalApplicationCommandsRaw() {
233234
return catnip().requester().queue(new OutboundRequest(Routes.GET_GLOBAL_APPLICATION_COMMANDS.withMajorParam(catnip().clientId()),
234-
Map.of()))
235+
Map.of()))
235236
.map(ResponsePayload::array);
236237
}
237238

238-
public Single<ApplicationCommand> createGlobalApplicationCommand(@Nonnull final String name, @Nonnull final String description,
239+
public Single<ApplicationCommand> createGlobalApplicationCommand(@Nonnull final ApplicationCommandType type,
240+
@Nonnull final String name, @Nonnull final String description,
239241
@Nonnull final Collection<ApplicationCommandOption> options) {
240-
return Single.fromObservable(createGlobalApplicationCommandRaw(name, description, options)
242+
return Single.fromObservable(createGlobalApplicationCommandRaw(type, name, description, options)
241243
.map(entityBuilder()::createApplicationCommand));
242244
}
243245

244-
public Observable<JsonObject> createGlobalApplicationCommandRaw(@Nonnull final String name, @Nonnull final String description,
246+
public Observable<JsonObject> createGlobalApplicationCommandRaw(@Nonnull final ApplicationCommandType type,
247+
@Nonnull final String name, @Nonnull final String description,
245248
@Nonnull final Collection<ApplicationCommandOption> options) {
246-
final JsonObject body = createCommandBody(name, description, options);
249+
final JsonObject body = createCommandBody(type, name, description, options);
247250
return catnip().requester().queue(new OutboundRequest(Routes.CREATE_GLOBAL_APPLICATION_COMMAND
248251
.withMajorParam(catnip().clientId()), Map.of()).object(body)).map(ResponsePayload::object);
249252
}
250253

251-
public Single<ApplicationCommand> editGlobalApplicationCommand(@Nonnull final String name, @Nonnull final String description,
254+
public Single<ApplicationCommand> editGlobalApplicationCommand(@Nonnull final ApplicationCommandType type,
255+
@Nonnull final String name, @Nonnull final String description,
252256
@Nonnull final String commandId,
253257
@Nonnull final Collection<ApplicationCommandOption> options) {
254-
return Single.fromObservable(editGlobalApplicationCommandRaw(name, description, commandId, options)
258+
return Single.fromObservable(editGlobalApplicationCommandRaw(type, name, description, commandId, options)
255259
.map(entityBuilder()::createApplicationCommand));
256260
}
257261

258-
public Observable<JsonObject> editGlobalApplicationCommandRaw(@Nonnull final String name, @Nonnull final String description,
262+
public Observable<JsonObject> editGlobalApplicationCommandRaw(@Nonnull final ApplicationCommandType type,
263+
@Nonnull final String name, @Nonnull final String description,
259264
@Nonnull final String commandId,
260265
@Nonnull final Collection<ApplicationCommandOption> options) {
261-
final var body = createCommandBody(name, description, options);
266+
final var body = createCommandBody(type, name, description, options);
262267
return catnip().requester().queue(new OutboundRequest(Routes.EDIT_GLOBAL_APPLICATION_COMMAND
263268
.withMajorParam(catnip().clientId()), Map.of("command", commandId)).object(body)).map(ResponsePayload::object);
264269
}
@@ -278,40 +283,44 @@ public Observable<ApplicationCommand> getGuildApplicationCommands(@Nonnull final
278283

279284
public Observable<JsonArray> getGuildApplicationCommandsRaw(@Nonnull final String guildId) {
280285
return catnip().requester().queue(new OutboundRequest(Routes.GET_GUILD_APPLICATION_COMMANDS.withMajorParam(catnip().clientId()),
281-
Map.of("guild", guildId)))
286+
Map.of("guild", guildId)))
282287
.map(ResponsePayload::array);
283288
}
284289

285-
public Single<ApplicationCommand> createGuildApplicationCommand(@Nonnull final String guildId, @Nonnull final String name,
290+
public Single<ApplicationCommand> createGuildApplicationCommand(@Nonnull final ApplicationCommandType type,
291+
@Nonnull final String guildId, @Nonnull final String name,
286292
@Nonnull final String description,
287293
@Nonnull final Collection<ApplicationCommandOption> options) {
288-
return Single.fromObservable(createGuildApplicationCommandRaw(guildId, name, description, options)
294+
return Single.fromObservable(createGuildApplicationCommandRaw(type, guildId, name, description, options)
289295
.map(entityBuilder()::createApplicationCommand));
290296
}
291297

292-
public Observable<JsonObject> createGuildApplicationCommandRaw(@Nonnull final String guildId, @Nonnull final String name,
298+
public Observable<JsonObject> createGuildApplicationCommandRaw(@Nonnull final ApplicationCommandType type,
299+
@Nonnull final String guildId, @Nonnull final String name,
293300
@Nonnull final String description,
294301
@Nonnull final Collection<ApplicationCommandOption> options) {
295-
final var body = createCommandBody(name, description, options);
302+
final var body = createCommandBody(type, name, description, options);
296303
return catnip().requester().queue(new OutboundRequest(Routes.CREATE_GUILD_APPLICATION_COMMAND
297304
.withMajorParam(catnip().clientId()), Map.of("guild", guildId)).object(body)).map(ResponsePayload::object);
298305
}
299306

300-
public Single<ApplicationCommand> editGuildApplicationCommand(@Nonnull final String guildId, @Nonnull final String name,
307+
public Single<ApplicationCommand> editGuildApplicationCommand(@Nonnull final ApplicationCommandType type,
308+
@Nonnull final String guildId, @Nonnull final String name,
301309
@Nonnull final String description,
302310
@Nonnull final String commandId,
303311
@Nonnull final Collection<ApplicationCommandOption> options) {
304-
return Single.fromObservable(editGuildApplicationCommandRaw(guildId, name, description, commandId, options)
312+
return Single.fromObservable(editGuildApplicationCommandRaw(type, guildId, name, description, commandId, options)
305313
.map(entityBuilder()::createApplicationCommand));
306314
}
307315

308-
public Observable<JsonObject> editGuildApplicationCommandRaw(@Nonnull final String guildId, @Nonnull final String name,
316+
public Observable<JsonObject> editGuildApplicationCommandRaw(@Nonnull final ApplicationCommandType type,
317+
@Nonnull final String guildId, @Nonnull final String name,
309318
@Nonnull final String description,
310319
@Nonnull final String commandId,
311320
@Nonnull final Collection<ApplicationCommandOption> options) {
312-
final var body = createCommandBody(name, description, options);
321+
final var body = createCommandBody(type, name, description, options);
313322
return catnip().requester().queue(new OutboundRequest(Routes.EDIT_GUILD_APPLICATION_COMMAND
314-
.withMajorParam(catnip().clientId()), Map.of("guild", guildId, "command", commandId)).object(body))
323+
.withMajorParam(catnip().clientId()), Map.of("guild", guildId, "command", commandId)).object(body))
315324
.map(ResponsePayload::object);
316325
}
317326

@@ -393,11 +402,12 @@ private JsonObject createSendBody(@Nullable final InteractionResponseType type,
393402
return builder.done();
394403
}
395404

396-
private JsonObject createCommandBody(@Nonnull final String name, @Nonnull final String description,
397-
@Nonnull final Collection<ApplicationCommandOption> options) {
405+
private JsonObject createCommandBody(@Nonnull final ApplicationCommandType type, @Nonnull final String name,
406+
@Nonnull final String description, @Nonnull final Collection<ApplicationCommandOption> options) {
398407
final var builder = JsonObject.builder();
399408
builder.value("name", name);
400409
builder.value("description", description);
410+
builder.value("type", type.id());
401411
if(!options.isEmpty()) {
402412
final List<JsonObject> optionJson = options.stream()
403413
.map(ApplicationCommandOption::toJson)

0 commit comments

Comments
 (0)