feat: slash commands and interactions (#248)

* start slash commands

* lil bit of this

* a little bit of that

* chore: add slash commands' types  (#249)

* Add slash Types

* style: format source files

* interaction create event

* make it a valid controller

* respond to a slash command

* interactions

* shtuff

* gotta get them all

* you gotta hit the bullseye

* you gotta survive

* most important thing i forgot

* please keep this

* more shuttttfgvuasdafwesdvjzdk

* more endpoints

* TYPO

* making that party jam

* this is my jam

* refactor: move slash commands types to webhook.ts (#250)

* Move Types

* Move to webhook.ts types file

* Update webhook.ts

* fix: update ExecuteSlashCommandOptions (#252)

* Move Types

* idk

* Remove Unnecessary Comment

* details

* http side of slash

* Format

* idk

* cleanup

* fmt

* fix imports

* tet

* remove log

* Update interactions.ts

* Update interactions.ts

* Update interactions.ts

* Use tweetnacl_deno for verification

* chore: move tweetnacl import to deps.ts

* Update deps.ts

* deps: use tweetnacl from GitHub

* fix: use serverOptions.slashHexKey

* fix: res -> req

* fix: use TextEncoder

* deps: add std@0.81.0/encoding/hex.ts

* chore: use encode() from std/encoding/hex.ts

* I am using the GitHub online editor pls help

* Update deps.ts

* Update interactions.ts

* Update interactions.ts

* Update interactions.ts

* Update interactions.ts

* fix: respond with 400 if timestamp and signature not present

* style: format files

* refactor!: merge createServer() into startServer()

* style: format files

Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com>
Co-authored-by: ayntee <ayyantee@gmail.com>
This commit is contained in:
Skillz4Killz
2020-12-22 12:39:01 -05:00
committed by GitHub
parent e49a23687e
commit 19f228c329
22 changed files with 882 additions and 1 deletions

View File

@@ -44,6 +44,10 @@ import {
handleInternalGuildRoleDelete,
handleInternalGuildRoleUpdate,
} from "./roles.ts";
import {
handleInternalInteractionsCommandCreate,
handleInternalInteractionsCreate,
} from "./interactions.ts";
export let controllers = {
READY: handleInternalReady,
@@ -63,6 +67,8 @@ export let controllers = {
GUILD_ROLE_CREATE: handleInternalGuildRoleCreate,
GUILD_ROLE_DELETE: handleInternalGuildRoleDelete,
GUILD_ROLE_UPDATE: handleInternalGuildRoleUpdate,
INTERACTION_CREATE: handleInternalInteractionsCreate,
APPLICATION_COMMAND_CREATE: handleInternalInteractionsCommandCreate,
MESSAGE_CREATE: handleInternalMessageCreate,
MESSAGE_DELETE: handleInternalMessageDelete,
MESSAGE_DELETE_BULK: handleInternalMessageDeleteBulk,

View File

@@ -1,16 +1,23 @@
import { RequestManager } from "../../rest/mod.ts";
import { structures } from "../structures/structures.ts";
import {
CreateSlashCommandOptions,
EditSlashCommandOptions,
EditSlashResponseOptions,
EditWebhookMessageOptions,
Errors,
ExecuteSlashCommandOptions,
ExecuteWebhookOptions,
MessageCreateOptions,
UpsertSlashCommandOptions,
WebhookCreateOptions,
WebhookPayload,
} from "../../types/types.ts";
import { endpoints } from "../../util/constants.ts";
import { botHasChannelPermissions } from "../../util/permissions.ts";
import { urlToBase64 } from "../../util/utils.ts";
import { botID } from "../../bot.ts";
import { cache } from "../../util/cache.ts";
/** Create a new webhook. Requires the MANAGE_WEBHOOKS permission. Returns a webhook object on success. Webhook names follow our naming restrictions that can be found in our Usernames and Nicknames documentation, with the following additional stipulations:
*
@@ -171,3 +178,139 @@ export function deleteWebhookMessage(
endpoints.WEBHOOK_DELETE(webhookID, webhookToken, messageID),
);
}
/**
* There are two kinds of Slash Commands: global commands and guild commands. Global commands are available for every guild that adds your app; guild commands are specific to the guild you specify when making them. Command names are unique per application within each scope (global and guild). That means:
*
* - Your app **cannot** have two global commands with the same name
* - Your app **cannot** have two guild commands within the same name **on the same guild**
* - Your app **can** have a global and guild command with the same name
* - Multiple apps **can** have commands with the same names
*
* Global commands are cached for **1 hour**. That means that new global commands will fan out slowly across all guilds, and will be guaranteed to be updated in an hour.
* Guild commands update **instantly**. We recommend you use guild commands for quick testing, and global commands when they're ready for public use.
*/
export function createSlashCommand(options: CreateSlashCommandOptions) {
// Use ... for content length due to unicode characters and js .length handling
if ([...options.name].length < 2 || [...options.name].length > 32) {
throw new Error(Errors.INVALID_SLASH_NAME);
}
if (
[...options.description].length < 1 || [...options.description].length > 100
) {
throw new Error(Errors.INVALID_SLASH_DESCRIPTION);
}
return RequestManager.post(
options.guildID
? endpoints.COMMANDS_GUILD(botID, options.guildID)
: endpoints.COMMANDS(botID),
{
...options,
},
);
}
/** Fetch all of the global commands for your application. */
export function getSlashCommands(guildID?: string) {
// TODO: Should this be a returned as a collection?
return RequestManager.get(
guildID
? endpoints.COMMANDS_GUILD(botID, guildID)
: endpoints.COMMANDS(botID),
);
}
/**
* Edit an existing slash command. If this command did not exist, it will create it.
*/
export function upsertSlashCommand(options: UpsertSlashCommandOptions) {
return RequestManager.post(
options.guildID
? endpoints.COMMANDS_GUILD_ID(botID, options.id, options.guildID)
: endpoints.COMMANDS_ID(botID, options.id),
{
...options,
},
);
}
/** Edit an existing slash command. */
export function editSlashCommand(options: EditSlashCommandOptions) {
return RequestManager.patch(
options.guildID
? endpoints.COMMANDS_GUILD_ID(botID, options.id, options.guildID)
: endpoints.COMMANDS_ID(botID, options.id),
{
...options,
},
);
}
/** Deletes a slash command. */
export function deleteSlashCommand(id: string, guildID?: string) {
if (!guildID) return RequestManager.delete(endpoints.COMMANDS_ID(botID, id));
return RequestManager.delete(endpoints.COMMANDS_GUILD_ID(botID, id, guildID));
}
/**
* Send a response to a users slash command. The command data will have the id and token necessary to respond.
* Interaction `tokens` are valid for **15 minutes** and can be used to send followup messages.
*
* NOTE: By default we will suppress mentions. To enable mentions, just pass any mentions object.
*/
export function executeSlashCommand(
id: string,
token: string,
options: ExecuteSlashCommandOptions,
) {
// If its already been executed, we need to send a followup response
if (cache.executedSlashCommands.has(token)) {
return RequestManager.post(endpoints.WEBHOOK(botID, token), {
...options,
});
}
// Expire in 15 minutes
cache.executedSlashCommands.set(token, id);
setTimeout(
() => cache.executedSlashCommands.delete(token),
Date.now() + 900000,
);
// IF NO MENTIONS ARE PROVIDED, FORCE DISABLE MENTIONS
if (!(options.data.allowed_mentions)) {
options.data.allowed_mentions = { parse: [] };
}
return RequestManager.post(endpoints.INTERACTION_ID_TOKEN(id, token), {
...options,
});
}
/** To delete your response to a slash command. If a message id is not provided, it will default to deleting the original response. */
export function deleteSlashResponse(
token: string,
messageID?: string,
) {
if (!messageID) {
return RequestManager.delete(
endpoints.INTERACTION_ORIGINAL_ID_TOKEN(botID, token),
);
}
return RequestManager.delete(
endpoints.INTERACTION_ID_TOKEN_MESSAGEID(botID, token, messageID),
);
}
/** To edit your response to a slash command. If a messageID is not provided it will default to editing the original response. */
export function editSlashResponse(
token: string,
options: EditSlashResponseOptions,
) {
return RequestManager.patch(
endpoints.INTERACTION_ORIGINAL_ID_TOKEN(botID, token),
options,
);
}