Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

feat: slash command registration #29

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export class RootModule extends BaseModule {
this.config = botConfig.modules[this.name];
this.enabled = this.config.enabled;
} else {
this.config = {enabled: false};
logEvent(
EventCategory.Error,
'core',
Expand Down
4 changes: 3 additions & 1 deletion src/core/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ function checkContextBlockForInteraction(
}

/** Convert an {@link ManagementPermission} to be of type {@link PermissionsBitFlag} */
function permissionConfigToBitFlag(permission: ManagementPermission | string) {
export function permissionConfigToBitFlag(
permission: ManagementPermission | string
) {
// have fun!
switch (permission) {
case 'timeout':
Expand Down
20 changes: 19 additions & 1 deletion src/core/slash_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
ModuleInputOption,
ModuleOptionType,
} from './modules.js';
import {updateShorthandPropertyAssignment} from 'typescript';

Check warning on line 47 in src/core/slash_commands.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'updateShorthandPropertyAssignment' is defined but never used
import {permissionConfigToBitFlag} from './permissions.js';

type SlashCommandOption =
| SlashCommandAttachmentOption
Expand Down Expand Up @@ -96,10 +98,26 @@
export async function generateSlashCommandForModule(
module: RootModule
): Promise<SlashCommandBuilder> {
// calculate the permissions configured for the slash command, so that it's hidden for those who don't have
// execution permission
// https://discord.com/developers/docs/topics/permissions
// 1 << 31 is the flag for "using bot commands"
// i would have used zero, but the command is entirely disabled if 0
let permissionBitFlags = BigInt('0x0000000080000000');
const requiredPermissions =
(module.config.permissions ?? {requiredPermissions: []})
.requiredPermissions ?? [];
for (const permission of requiredPermissions) {
// perform a binary OR operation to combine the bitflags together
// https://discordjs.guide/slash-commands/permissions.html#member-permissions
permissionBitFlags |= permissionConfigToBitFlag(permission);
}

// translate the module to slash command form
const slashCommand = new SlashCommandBuilder()
.setName(module.name)
.setDescription(module.description);
.setDescription(module.description)
.setDefaultMemberPermissions(permissionBitFlags);

// if the module has submodules, than register those as subcommands
// https://discord.com/developers/docs/interactions/application-commands#subcommands-and-subcommand-groups
Expand Down
Loading