dressed
Version:
A sleek, serverless-ready Discord bot framework.
75 lines • 3.22 kB
JavaScript
import { ApplicationCommandType, ApplicationIntegrationType, InteractionContextType, InteractionType, PermissionFlagsBits, } from "discord-api-types/v10";
import { bulkOverwriteGlobalCommands, bulkOverwriteGuildCommands } from "../../resources/generated.resources.js";
import { logDefer, logSuccess, logWarn } from "../../utils/log.js";
import { createHandlerSetup } from "./index.js";
/**
* Installs commands to the Discord API
*/
export async function installCommands(commands) {
var _a, _b, _c, _d;
logDefer("Registering commands");
const scopes = new Map([
["global", []],
]);
for (const command of commands) {
if (command.exports === null)
return;
if ("config" in command.data) {
logWarn("In the next major version of Dressed, command config must be passed in using `command.exports` instead of `command.data`");
command.exports.config = command.data.config; // TODO Remove check before next major release
}
const config = (_a = command.exports.config) !== null && _a !== void 0 ? _a : {};
if (!config.type) {
config.type = "ChatInput";
}
if (config.type === "ChatInput") {
config.description = (_b = config.description) !== null && _b !== void 0 ? _b : "No description provided";
}
if (Array.isArray(config.default_member_permissions)) {
config.default_member_permissions = config.default_member_permissions
.reduce((p, k) => p | PermissionFlagsBits[k], BigInt(0))
.toString();
}
if (config.contexts) {
config.contexts = [...new Set(config.contexts.map((c) => InteractionContextType[c]))];
}
if (config.integration_type) {
config.integration_types = [ApplicationIntegrationType[`${config.integration_type}Install`]];
}
for (const guild of (_c = config.guilds) !== null && _c !== void 0 ? _c : ["global"]) {
scopes.set(guild, ((_d = scopes.get(guild)) !== null && _d !== void 0 ? _d : []).concat({
...config,
name: command.name,
type: ApplicationCommandType[config.type],
}));
}
}
for (const [scope, commands] of scopes) {
if (scope === "global") {
await bulkOverwriteGlobalCommands(commands);
}
else {
await bulkOverwriteGuildCommands(scope, commands);
}
}
logSuccess("Registered commands");
}
/**
* Creates the command handler
* @returns A function that runs a command
*/
export const setupCommands = createHandlerSetup({
itemMessages: (interaction) => ({
noItem: `No command handler for "${interaction.data.name}"`,
middlewareKey: "commands",
pending: (item) => `Running${interaction.type === InteractionType.ApplicationCommandAutocomplete ? " autocomplete for " : " "}command "${item.name}"`,
}),
findItem(interaction, items) {
const item = items.find((i) => i.name === interaction.data.name);
if (!item) {
return;
}
return [item, [interaction]];
},
});
//# sourceMappingURL=commands.js.map