This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |