From e6d8e4d4a8c67f7fe362e17ace6f60c34b5f9c50 Mon Sep 17 00:00:00 2001 From: gnosii <65878291+gnosii@users.noreply.github.com> Date: Mon, 23 Jan 2023 23:31:05 -0300 Subject: [PATCH] push progress to gh 3 --- src/utils/command.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/utils/command.ts diff --git a/src/utils/command.ts b/src/utils/command.ts new file mode 100644 index 0000000..d9b4fd0 --- /dev/null +++ b/src/utils/command.ts @@ -0,0 +1,51 @@ +import { GuildMember, Permissions } from 'discord.js'; + +import { Command, FinalizedCommand } from '../object/command/Command' +import { CommandFinalizationError } from '../errors.js'; +import { Pogbot } from '../Pogbot.js'; + +/** + * Finalize a command by adding permissions and restrictions. + * Creates an object that can be accepted by Discord + */ +export function finalize(initial: Command): FinalizedCommand { + const cmd: FinalizedCommand = initial as FinalizedCommand; + + const { isAdminOnly, isGuildOnly } = cmd.restrictions; + const basicInfo = { + name: cmd.name, + description: cmd.description + }; + + cmd._json = { ...basicInfo, ...cmd.additionalData } + + cmd._json.dmPermission = !isGuildOnly ?? true + + const permissions: bigint[] = [Permissions.FLAGS.USE_APPLICATION_COMMANDS]; + + if (isAdminOnly) { + if (!isGuildOnly) { + throw new CommandFinalizationError(cmd, 'Cannot require admin if not limited to guilds'); + } + permissions.push(Permissions.FLAGS.ADMINISTRATOR, Permissions.FLAGS.MANAGE_GUILD); + } + + cmd._json.defaultMemberPermissions = permissions; + + return cmd; +} + +/** + * Create a slash mention from a command's name and Discord assigned ID. + */ +export function mention(name: string): string { + // Splitting the command name helps support subcommands without having to add more code. + return ``; +} + +/** + * Check if a member has permission to manage the guild or has the administrator permission. + */ +export function canManage(member: GuildMember): boolean { + return member.permissions.has(Permissions.FLAGS.MANAGE_GUILD, true); +}