refactor(helpers): separate functions into files (#667)

* refactor(helpers): separate functions into files

* idk

* idk
This commit is contained in:
ayntee
2021-03-13 08:10:31 -05:00
committed by GitHub
parent 88ce4da555
commit e9cbbbff7c
143 changed files with 3362 additions and 2915 deletions
@@ -0,0 +1,31 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { CreateSlashCommandOptions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
/**
* 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 async function createSlashCommand(options: CreateSlashCommandOptions) {
validateSlashCommands([options], true);
const result = await RequestManager.post(
options.guildID
? endpoints.COMMANDS_GUILD(applicationID, options.guildID)
: endpoints.COMMANDS(applicationID),
{
...options,
},
);
return result;
}
@@ -0,0 +1,13 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
/** Deletes a slash command. */
export function deleteSlashCommand(id: string, guildID?: string) {
if (!guildID) {
return RequestManager.delete(endpoints.COMMANDS_ID(applicationID, id));
}
return RequestManager.delete(
endpoints.COMMANDS_GUILD_ID(applicationID, guildID, id),
);
}
@@ -0,0 +1,18 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
/** To delete your response to a slash command. If a message id is not provided, it will default to deleting the original response. */
export async function deleteSlashResponse(token: string, messageID?: string) {
const result = await RequestManager.delete(
messageID
? endpoints.INTERACTION_ID_TOKEN_MESSAGEID(
applicationID,
token,
messageID,
)
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationID, token),
);
return result;
}
@@ -0,0 +1,70 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { structures } from "../../structures/mod.ts";
import {
EditSlashResponseOptions,
Errors,
MessageCreateOptions,
} from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
/** To edit your response to a slash command. If a messageID is not provided it will default to editing the original response. */
export async function editSlashResponse(
token: string,
options: EditSlashResponseOptions,
) {
if (options.content && options.content.length > 2000) {
throw Error(Errors.MESSAGE_MAX_LENGTH);
}
if (options.embeds && options.embeds.length > 10) {
options.embeds.splice(10);
}
if (options.allowed_mentions) {
if (options.allowed_mentions.users?.length) {
if (options.allowed_mentions.parse.includes("users")) {
options.allowed_mentions.parse = options.allowed_mentions.parse.filter(
(p) => p !== "users",
);
}
if (options.allowed_mentions.users.length > 100) {
options.allowed_mentions.users = options.allowed_mentions.users.slice(
0,
100,
);
}
}
if (options.allowed_mentions.roles?.length) {
if (options.allowed_mentions.parse.includes("roles")) {
options.allowed_mentions.parse = options.allowed_mentions.parse.filter(
(p) => p !== "roles",
);
}
if (options.allowed_mentions.roles.length > 100) {
options.allowed_mentions.roles = options.allowed_mentions.roles.slice(
0,
100,
);
}
}
}
const result = await RequestManager.patch(
options.messageID
? endpoints.WEBHOOK_MESSAGE(applicationID, token, options.messageID)
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationID, token),
options,
);
// If the original message was edited, this will not return a message
if (!options.messageID) return result;
const message = await structures.createMessageStruct(
result as MessageCreateOptions,
);
return message;
}
@@ -0,0 +1,48 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { SlashCommandResponseOptions } from "../../types/mod.ts";
import { cache } from "../../util/cache.ts";
import { endpoints } from "../../util/constants.ts";
/**
* 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 async function executeSlashCommand(
id: string,
token: string,
options: SlashCommandResponseOptions,
) {
// If its already been executed, we need to send a followup response
if (cache.executedSlashCommands.has(token)) {
return RequestManager.post(endpoints.WEBHOOK(applicationID, token), {
...options,
});
}
// Expire in 15 minutes
cache.executedSlashCommands.set(token, id);
setTimeout(
() => cache.executedSlashCommands.delete(token),
900000,
);
// If the user wants this as a private message mark it ephemeral
if (options.private) {
options.data.flags = 64;
}
// If no mentions are provided, force disable mentions
if (!options.data.allowed_mentions) {
options.data.allowed_mentions = { parse: [] };
}
const result = await RequestManager.post(
endpoints.INTERACTION_ID_TOKEN(id, token),
options,
);
return result;
}
+15
View File
@@ -0,0 +1,15 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { SlashCommand } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
/** Fetchs the global command for the given ID. If a guildID is provided, the guild command will be fetched. */
export async function getSlashCommand(commandID: string, guildID?: string) {
const result = await RequestManager.get(
guildID
? endpoints.COMMANDS_GUILD_ID(applicationID, guildID, commandID)
: endpoints.COMMANDS_ID(applicationID, commandID),
);
return result as SlashCommand;
}
@@ -0,0 +1,16 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { SlashCommand } from "../../types/mod.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/** Fetch all of the global commands for your application. */
export async function getSlashCommands(guildID?: string) {
const result = (await RequestManager.get(
guildID
? endpoints.COMMANDS_GUILD(applicationID, guildID)
: endpoints.COMMANDS(applicationID),
)) as SlashCommand[];
return new Collection(result.map((command) => [command.name, command]));
}
@@ -0,0 +1,25 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { UpsertSlashCommandOptions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
/**
* Edit an existing slash command. If this command did not exist, it will create it.
*/
export async function upsertSlashCommand(
commandID: string,
options: UpsertSlashCommandOptions,
guildID?: string,
) {
validateSlashCommands([options]);
const result = await RequestManager.patch(
guildID
? endpoints.COMMANDS_GUILD_ID(applicationID, guildID, commandID)
: endpoints.COMMANDS_ID(applicationID, commandID),
options,
);
return result;
}
@@ -0,0 +1,26 @@
import { applicationID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { UpsertSlashCommandsOptions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
/**
* Bulk edit existing slash commands. If a command does not exist, it will create it.
*
* **NOTE:** Any slash commands that are not specified in this function will be **deleted**. If you don't provide the commandID and rename your command, the command gets a new ID.
*/
export async function upsertSlashCommands(
options: UpsertSlashCommandsOptions[],
guildID?: string,
) {
validateSlashCommands(options);
const result = await RequestManager.put(
guildID
? endpoints.COMMANDS_GUILD(applicationID, guildID)
: endpoints.COMMANDS(applicationID),
options,
);
return result;
}