Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Guards TypeError: undefined is not a function #63

@Satont

Description

@Satont
Unhandled rejection TypeError: undefined is not a function
    at DGuard.guard [as _fn] (/config/workspace/plumeware/src/guards/SlashCommandPermission.ts:5:55)
    at next (/config/workspace/plumeware/node_modules/@typeit/src/decorators/classes/Method.ts:83:37)
    at /config/workspace/plumeware/node_modules/@typeit/src/decorators/classes/Method.ts:97:34
    at DSlash.<anonymous> (/config/workspace/plumeware/node_modules/@typeit/src/decorators/classes/Method.ts:29:43)
    at Bot.executeSlash (/config/workspace/plumeware/node_modules/@typeit/src/Client.ts:368:24)
    at AppDiscord.onMessage (/config/workspace/plumeware/src/events/interaction.ts:8:12)
    at next (/config/workspace/plumeware/node_modules/@typeit/src/decorators/classes/Method.ts:74:37)
    at /config/workspace/plumeware/node_modules/@typeit/src/decorators/classes/Method.ts:97:34
    at DOn.<anonymous> (/config/workspace/plumeware/node_modules/@typeit/src/decorators/classes/Method.ts:29:43)
    at Bot.<anonymous> (/config/workspace/plumeware/node_modules/@typeit/src/logic/metadatas/MetadataStorage.ts:301:30)

SlashCommandPermission.ts

import { GuardFunction, ArgsOf } from '@typeit/discord';
import { PermissionResolvable } from 'discord.js';

export function SlashCommandPermission(permissions: PermissionResolvable[] | PermissionResolvable) {
  const guard: GuardFunction<ArgsOf<'interaction'>> = async ([interaction], client, next, guardDatas) => {
    const perms = Array.isArray(permissions) ? permissions : [permissions];
    const guildMember = interaction.guild.members.cache.get(interaction.member.user.id);
    const hasPermissions = perms.every((perm) => guildMember.permissions.has(perm));

    if (!hasPermissions) {
      guardDatas.error = 'You have no access to this command';
    }

    await next();
  };

  return guard;
}

Some controller:

@Discord()
@Group('channel', 'Add or remove channel to bot watching list')
@Guard(SlashCommandPermission('MANAGE_CHANNELS'))
export abstract class AppDiscord {
  @Slash('add')
  async add(
    @Option('channelId', { description: 'id of the channel for listening on join', required: true })
    channelId: string,
    interaction: CommandInteraction,
    guardDatas: Record<string, string>,
  ) {
    if (guardDatas.error) {
      interaction.reply(guardDatas.erorr);
    } else {
      const result = await add(interaction, channelId);
      interaction.reply(result);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions