refactor!: move dirs outside of src/ (#2032)

This commit is contained in:
Skillz4Killz
2022-02-11 04:49:53 -05:00
committed by GitHub
parent 471ef5cb6c
commit 8aaea9f339
594 changed files with 84 additions and 66 deletions
@@ -0,0 +1,19 @@
import type { Bot } from "../../../bot.ts";
import type { ApplicationCommandPermissions } from "../../../types/interactions/commands/applicationCommandPermissions.ts";
import { GuildApplicationCommandPermissions } from "../../../types/interactions/commands/guildApplicationCommandPermissions.ts";
/** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */
export async function batchEditApplicationCommandPermissions(
bot: Bot,
guildId: bigint,
options: { id: string; permissions: ApplicationCommandPermissions[] }[],
) {
const result = await bot.rest.runMethod<GuildApplicationCommandPermissions[]>(
bot.rest,
"put",
bot.constants.endpoints.COMMANDS_PERMISSIONS(bot.applicationId, guildId),
options,
);
return result.map((res) => bot.transformers.applicationCommandPermission(bot, res));
}
@@ -0,0 +1,49 @@
import type { ApplicationCommand } from "../../../types/interactions/commands/applicationCommand.ts";
import type { CreateApplicationCommand } from "../../../types/interactions/commands/createGlobalApplicationCommand.ts";
import type { Bot } from "../../../bot.ts";
import { ApplicationCommandOption } from "../../../types/interactions/commands/applicationCommandOption.ts";
/**
* There are two kinds of Application 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 createApplicationCommand(bot: Bot, options: CreateApplicationCommand, guildId?: bigint) {
const result = await bot.rest.runMethod<ApplicationCommand>(
bot.rest,
"post",
guildId
? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.endpoints.COMMANDS(bot.applicationId),
{
name: options.name,
description: options.description,
type: options.type,
options: options.options ? makeOptionsForCommand(options.options) : undefined,
},
);
return bot.transformers.applicationCommand(bot, result);
}
// @ts-ignore TODO: see if we can make this not circular
export function makeOptionsForCommand(options: ApplicationCommandOption[]) {
return options.map((option) => ({
type: option.type,
name: option.name,
description: option.description,
required: option.required,
choices: option.choices,
options: option.options ? makeOptionsForCommand(option.options) : undefined,
channel_types: option.channelTypes,
autocomplete: option.autocomplete,
min_value: option.minValue,
max_value: option.maxValue,
}));
}
@@ -0,0 +1,12 @@
import type { Bot } from "../../../bot.ts";
/** Deletes a application command. */
export async function deleteApplicationCommand(bot: Bot, id: bigint, guildId?: bigint) {
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
guildId
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, id)
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, id),
);
}
@@ -0,0 +1,12 @@
import type { Bot } from "../../../bot.ts";
/** To delete your response to a application command. If a message id is not provided, it will default to deleting the original response. */
export async function deleteInteractionResponse(bot: Bot, token: string, messageId?: bigint) {
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
messageId
? bot.constants.endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(bot.applicationId, token, messageId)
: bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
);
}
@@ -0,0 +1,22 @@
import type { ApplicationCommandPermissions } from "../../../types/interactions/commands/applicationCommandPermissions.ts";
import type { Bot } from "../../../bot.ts";
import { GuildApplicationCommandPermissions } from "../../../types/interactions/commands/guildApplicationCommandPermissions.ts";
/** Edits command permissions for a specific command for your application in a guild. */
export async function editApplicationCommandPermissions(
bot: Bot,
guildId: bigint,
commandId: bigint,
options: ApplicationCommandPermissions[],
) {
const result = await bot.rest.runMethod<GuildApplicationCommandPermissions>(
bot.rest,
"put",
bot.constants.endpoints.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId),
{
permissions: options,
},
);
return bot.transformers.applicationCommandPermission(bot, result);
}
@@ -0,0 +1,99 @@
import type { DiscordenoEditWebhookMessage } from "../../../types/discordeno/editWebhookMessage.ts";
import type { Bot } from "../../../bot.ts";
import { MessageComponentTypes } from "../../../types/messages/components/messageComponentTypes.ts";
/** To edit your response to a application command. If a messageId is not provided it will default to editing the original response. */
export async function editInteractionResponse(bot: Bot, token: string, options: DiscordenoEditWebhookMessage) {
const result = await bot.rest.runMethod(
bot.rest,
"patch",
options.messageId
? bot.constants.endpoints.WEBHOOK_MESSAGE(bot.applicationId, token, options.messageId)
: bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
{
content: options.content,
embeds: options.embeds,
file: options.file,
allowed_mentions: options.allowedMentions
? {
parse: options.allowedMentions.parse,
roles: options.allowedMentions.roles?.map((id) => id.toString()),
users: options.allowedMentions.users?.map((id) => id.toString()),
replied_user: options.allowedMentions.repliedUser,
}
: undefined,
attachments: options.attachments?.map((attachment) => ({
id: attachment.id.toString(),
filename: attachment.filename,
content_type: attachment.contentType,
size: attachment.size,
url: attachment.url,
proxy_url: attachment.proxyUrl,
height: attachment.height,
width: attachment.width,
ephemeral: attachment.ephemeral,
})),
components: options.components?.map((component) => ({
type: component.type,
components: component.components.map((subcomponent) => {
if (subcomponent.type === MessageComponentTypes.InputText) {
return {
type: subcomponent.type,
style: subcomponent.style,
custom_id: subcomponent.customId,
label: subcomponent.label,
placeholder: subcomponent.placeholder,
min_length: subcomponent.minLength ?? subcomponent.required === false ? 0 : subcomponent.minLength,
max_length: subcomponent.maxLength,
};
}
if (subcomponent.type === MessageComponentTypes.SelectMenu) {
return {
type: subcomponent.type,
custom_id: subcomponent.customId,
placeholder: subcomponent.placeholder,
min_values: subcomponent.minValues,
max_values: subcomponent.maxValues,
options: subcomponent.options.map((option) => ({
label: option.label,
value: option.value,
description: option.description,
emoji: option.emoji
? {
id: option.emoji.id?.toString(),
name: option.emoji.name,
animated: option.emoji.animated,
}
: undefined,
default: option.default,
})),
};
}
return {
type: subcomponent.type,
custom_id: subcomponent.customId,
label: subcomponent.label,
style: subcomponent.style,
emoji: "emoji" in subcomponent && subcomponent.emoji
? {
id: subcomponent.emoji.id?.toString(),
name: subcomponent.emoji.name,
animated: subcomponent.emoji.animated,
}
: undefined,
url: "url" in subcomponent ? subcomponent.url : undefined,
disabled: "disabled" in subcomponent ? subcomponent.disabled : undefined,
};
}),
})),
message_id: options.messageId?.toString(),
},
);
// If the original message was edited, this will not return a message
if (!options.messageId) return result as undefined;
return bot.transformers.message(bot, result);
}
@@ -0,0 +1,15 @@
import type { ApplicationCommand } from "../../../types/interactions/commands/applicationCommand.ts";
import type { Bot } from "../../../bot.ts";
/** Fetches the global command for the given Id. If a guildId is provided, the guild command will be fetched. */
export async function getApplicationCommand(bot: Bot, commandId: bigint, guildId?: bigint) {
const result = await bot.rest.runMethod<ApplicationCommand>(
bot.rest,
"get",
guildId
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, commandId)
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, commandId),
);
return bot.transformers.applicationCommand(bot, result);
}
@@ -0,0 +1,13 @@
import type { GuildApplicationCommandPermissions } from "../../../types/interactions/commands/guildApplicationCommandPermissions.ts";
import type { Bot } from "../../../bot.ts";
/** Fetches command permissions for a specific command for your application in a guild. Returns a GuildApplicationCommandPermissions object. */
export async function getApplicationCommandPermission(bot: Bot, guildId: bigint, commandId: bigint) {
const result = await bot.rest.runMethod<GuildApplicationCommandPermissions>(
bot.rest,
"get",
bot.constants.endpoints.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId),
);
return bot.transformers.applicationCommandPermission(bot, result);
}
@@ -0,0 +1,19 @@
import type { Bot } from "../../../bot.ts";
import type { GuildApplicationCommandPermissions } from "../../../types/interactions/commands/guildApplicationCommandPermissions.ts";
import { Collection } from "../../../util/collection.ts";
/** Fetches command permissions for all commands for your application in a guild. Returns an array of GuildApplicationCommandPermissions objects. */
export async function getApplicationCommandPermissions(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<GuildApplicationCommandPermissions[]>(
bot.rest,
"get",
bot.constants.endpoints.COMMANDS_PERMISSIONS(bot.applicationId, guildId),
);
return new Collection(
result.map((res) => {
const perms = bot.transformers.applicationCommandPermission(bot, res);
return [perms.id, perms];
}),
);
}
@@ -0,0 +1,21 @@
import type { ApplicationCommand } from "../../../types/interactions/commands/applicationCommand.ts";
import { Collection } from "../../../util/collection.ts";
import type { Bot } from "../../../bot.ts";
/** Fetch all the commands for your application. If a guild id is not provided, it will fetch global commands. */
export async function getApplicationCommands(bot: Bot, guildId?: bigint) {
const result = await bot.rest.runMethod<ApplicationCommand[]>(
bot.rest,
"get",
guildId
? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.endpoints.COMMANDS(bot.applicationId),
);
return new Collection(
result.map((res) => {
const command = bot.transformers.applicationCommand(bot, res);
return [command.id, command];
}),
);
}
@@ -0,0 +1,30 @@
import type { ApplicationCommand } from "../../../types/interactions/commands/applicationCommand.ts";
import type { EditGlobalApplicationCommand } from "../../../types/interactions/commands/editGlobalApplicationCommand.ts";
import type { Bot } from "../../../bot.ts";
import { makeOptionsForCommand } from "./createApplicationCommand.ts";
/**
* Edit an existing application command. If this command did not exist, it will create it.
*/
export async function upsertApplicationCommand(
bot: Bot,
commandId: bigint,
options: EditGlobalApplicationCommand,
guildId?: bigint,
) {
const result = await bot.rest.runMethod<ApplicationCommand>(
bot.rest,
"patch",
guildId
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, commandId)
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, commandId),
{
name: options.name,
description: options.description,
type: options.type,
options: options.options ? makeOptionsForCommand(options.options) : undefined,
},
);
return bot.transformers.applicationCommand(bot, result);
}
@@ -0,0 +1,39 @@
import type { ApplicationCommand } from "../../../types/interactions/commands/applicationCommand.ts";
import type { EditGlobalApplicationCommand } from "../../../types/interactions/commands/editGlobalApplicationCommand.ts";
import type { MakeRequired } from "../../../types/util.ts";
import type { Bot } from "../../../bot.ts";
import { Collection } from "../../../util/collection.ts";
import { makeOptionsForCommand } from "./createApplicationCommand.ts";
/**
* Bulk edit existing application commands. If a command does not exist, it will create it.
*
* **NOTE:** Any application 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 upsertApplicationCommands(
bot: Bot,
options: MakeRequired<EditGlobalApplicationCommand, "name">[],
guildId?: bigint,
) {
const result = await bot.rest.runMethod<ApplicationCommand[]>(
bot.rest,
"put",
guildId
? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.endpoints.COMMANDS(bot.applicationId),
options.map((option) => ({
name: option.name,
description: option.description,
type: option.type,
options: option.options ? makeOptionsForCommand(option.options) : undefined,
default_permission: option.defaultPermission,
})),
);
return new Collection(
result.map((res) => {
const command = bot.transformers.applicationCommand(bot, res);
return [command.id, command];
}),
);
}