Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
push progress to gh 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ggvbo committed Jan 24, 2023
1 parent 4535478 commit e6d8e4d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/utils/command.ts
Original file line number Diff line number Diff line change
@@ -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 `</${name}:${Pogbot.instance.commands.get(name.split(' ')[0])?._id}>`;
}

/**
* 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);
}

0 comments on commit e6d8e4d

Please sign in to comment.