fix: use applicationID for slash commands instead of botID (#437)

* add(bot): ApplicationID let & setApplicationID function

* fix(controllers): set application id when bot is ready

* refactor(util): rename botID argument to applicationID

* fix(handlers): use applicationID
This commit is contained in:
ITOH
2021-01-23 20:19:12 +01:00
committed by GitHub
parent 55235e5ea0
commit c281665961
4 changed files with 39 additions and 24 deletions
+2 -1
View File
@@ -1,4 +1,4 @@
import { eventHandlers, setBotID } from "../../bot.ts"; import { eventHandlers, setApplicationID, setBotID } from "../../bot.ts";
import { import {
DiscordPayload, DiscordPayload,
PresenceUpdatePayload, PresenceUpdatePayload,
@@ -24,6 +24,7 @@ export async function handleInternalReady(
const payload = data.d as ReadyPayload; const payload = data.d as ReadyPayload;
setBotID(payload.user.id); setBotID(payload.user.id);
setApplicationID(payload.application.id);
// Triggered on each shard // Triggered on each shard
eventHandlers.shardReady?.(shardID); eventHandlers.shardReady?.(shardID);
+19 -15
View File
@@ -1,4 +1,4 @@
import { botID } from "../../bot.ts"; import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts"; import { RequestManager } from "../../rest/request_manager.ts";
import { import {
CreateSlashCommandOptions, CreateSlashCommandOptions,
@@ -206,8 +206,8 @@ export function createSlashCommand(options: CreateSlashCommandOptions) {
return RequestManager.post( return RequestManager.post(
options.guildID options.guildID
? endpoints.COMMANDS_GUILD(botID, options.guildID) ? endpoints.COMMANDS_GUILD(applicationID, options.guildID)
: endpoints.COMMANDS(botID), : endpoints.COMMANDS(applicationID),
{ {
...options, ...options,
}, },
@@ -219,8 +219,8 @@ export function getSlashCommands(guildID?: string) {
// TODO: Should this be a returned as a collection? // TODO: Should this be a returned as a collection?
return RequestManager.get( return RequestManager.get(
guildID guildID
? endpoints.COMMANDS_GUILD(botID, guildID) ? endpoints.COMMANDS_GUILD(applicationID, guildID)
: endpoints.COMMANDS(botID), : endpoints.COMMANDS(applicationID),
); );
} }
@@ -230,8 +230,8 @@ export function getSlashCommands(guildID?: string) {
export function upsertSlashCommand(options: UpsertSlashCommandOptions) { export function upsertSlashCommand(options: UpsertSlashCommandOptions) {
return RequestManager.post( return RequestManager.post(
options.guildID options.guildID
? endpoints.COMMANDS_GUILD_ID(botID, options.id, options.guildID) ? endpoints.COMMANDS_GUILD_ID(applicationID, options.id, options.guildID)
: endpoints.COMMANDS_ID(botID, options.id), : endpoints.COMMANDS_ID(applicationID, options.id),
{ {
...options, ...options,
}, },
@@ -242,8 +242,8 @@ export function upsertSlashCommand(options: UpsertSlashCommandOptions) {
export function editSlashCommand(options: EditSlashCommandOptions) { export function editSlashCommand(options: EditSlashCommandOptions) {
return RequestManager.patch( return RequestManager.patch(
options.guildID options.guildID
? endpoints.COMMANDS_GUILD_ID(botID, options.id, options.guildID) ? endpoints.COMMANDS_GUILD_ID(applicationID, options.id, options.guildID)
: endpoints.COMMANDS_ID(botID, options.id), : endpoints.COMMANDS_ID(applicationID, options.id),
{ {
...options, ...options,
}, },
@@ -252,8 +252,12 @@ export function editSlashCommand(options: EditSlashCommandOptions) {
/** Deletes a slash command. */ /** Deletes a slash command. */
export function deleteSlashCommand(id: string, guildID?: string) { export function deleteSlashCommand(id: string, guildID?: string) {
if (!guildID) return RequestManager.delete(endpoints.COMMANDS_ID(botID, id)); if (!guildID) {
return RequestManager.delete(endpoints.COMMANDS_GUILD_ID(botID, id, guildID)); return RequestManager.delete(endpoints.COMMANDS_ID(applicationID, id));
}
return RequestManager.delete(
endpoints.COMMANDS_GUILD_ID(applicationID, id, guildID),
);
} }
/** /**
@@ -269,7 +273,7 @@ export function executeSlashCommand(
) { ) {
// If its already been executed, we need to send a followup response // If its already been executed, we need to send a followup response
if (cache.executedSlashCommands.has(token)) { if (cache.executedSlashCommands.has(token)) {
return RequestManager.post(endpoints.WEBHOOK(botID, token), { return RequestManager.post(endpoints.WEBHOOK(applicationID, token), {
...options, ...options,
}); });
} }
@@ -298,11 +302,11 @@ export function deleteSlashResponse(
) { ) {
if (!messageID) { if (!messageID) {
return RequestManager.delete( return RequestManager.delete(
endpoints.INTERACTION_ORIGINAL_ID_TOKEN(botID, token), endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationID, token),
); );
} }
return RequestManager.delete( return RequestManager.delete(
endpoints.INTERACTION_ID_TOKEN_MESSAGEID(botID, token, messageID), endpoints.INTERACTION_ID_TOKEN_MESSAGEID(applicationID, token, messageID),
); );
} }
@@ -312,7 +316,7 @@ export function editSlashResponse(
options: EditSlashResponseOptions, options: EditSlashResponseOptions,
) { ) {
return RequestManager.patch( return RequestManager.patch(
endpoints.INTERACTION_ORIGINAL_ID_TOKEN(botID, token), endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationID, token),
options, options,
); );
} }
+6
View File
@@ -10,6 +10,7 @@ import { spawnShards } from "./ws/shard_manager.ts";
export let authorization = ""; export let authorization = "";
export let botID = ""; export let botID = "";
export let applicationID = "";
export let eventHandlers: EventHandlers = {}; export let eventHandlers: EventHandlers = {};
@@ -74,6 +75,11 @@ export function setBotID(id: string) {
if (botID !== id) botID = id; if (botID !== id) botID = id;
} }
/** INTERNAL LIB function used to set the application ID once the READY event is sent by Discord. */
export function setApplicationID(id: string) {
if (applicationID !== id) applicationID = id;
}
// BIG BRAIN BOT STUFF ONLY BELOW THIS // BIG BRAIN BOT STUFF ONLY BELOW THIS
/** /**
+12 -8
View File
@@ -135,14 +135,18 @@ export const endpoints = {
`${baseEndpoints.BASE_URL}/webhooks/${webhookID}/${token}/github`, `${baseEndpoints.BASE_URL}/webhooks/${webhookID}/${token}/github`,
// Application Endpoints // Application Endpoints
COMMANDS: (botID: string) => COMMANDS: (applicationID: string) =>
`${baseEndpoints.BASE_URL}/applications/${botID}/commands`, `${baseEndpoints.BASE_URL}/applications/${applicationID}/commands`,
COMMANDS_GUILD: (botID: string, guildID: string) => COMMANDS_GUILD: (applicationID: string, guildID: string) =>
`${baseEndpoints.BASE_URL}/applications/${botID}/guilds/${guildID}/commands`, `${baseEndpoints.BASE_URL}/applications/${applicationID}/guilds/${guildID}/commands`,
COMMANDS_ID: (botID: string, commandID: string) => COMMANDS_ID: (applicationID: string, commandID: string) =>
`${baseEndpoints.BASE_URL}/applications/${botID}/commands/${commandID}`, `${baseEndpoints.BASE_URL}/applications/${applicationID}/commands/${commandID}`,
COMMANDS_GUILD_ID: (botID: string, commandID: string, guildID: string) => COMMANDS_GUILD_ID: (
`${baseEndpoints.BASE_URL}/applications/${botID}/guilds/${guildID}/commands/${commandID}`, applicationID: string,
commandID: string,
guildID: string,
) =>
`${baseEndpoints.BASE_URL}/applications/${applicationID}/guilds/${guildID}/commands/${commandID}`,
// Interaction Endpoints // Interaction Endpoints
INTERACTION_ID_TOKEN: (interactionID: string, token: string) => INTERACTION_ID_TOKEN: (interactionID: string, token: string) =>