owengombas / discord.ts

🤖 Create your discord bot by using TypeScript and decorators!
https://owencalvin.github.io/discord.ts/
325 stars 40 forks source link

Guards TypeError: undefined is not a function #63

Open Satont opened 3 years ago

Satont commented 3 years ago
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);
    }
  }
}
vijayymmeena commented 3 years ago

https://github.com/OwenCalvin/discord.ts/blob/slash/examples/guards/guards/Say.ts

check above example for proper usage and let us know if issue persist

Satont commented 3 years ago

Changed my code to

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

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

        data.reply({ embeds: [noPermissionError] });
      } else {
        await next();
      }
    } else {
      await next();
    }
  };

  return guard;
};

Now i has no errors, but seems like nothing responsed to me in discord. I mean when i using some slash command, then it's failed.

It's even not logging does it's instance of interaction or not.

Command is now more simple:

@Slash('add')
  async add(
    @Option('channelId', { description: 'id of the channel for listening on join', required: true })
    channelId: string,
    interaction: CommandInteraction,
  ) {
    interaction.reply('Test');
  }
Satont commented 3 years ago

Btw, just copied example of Say guard, same behavior.

vijayymmeena commented 3 years ago

Make you are using this PR OwenCalvin/discord.ts/pull/62

and try this

export function ExampleGaurd(): GuardFunction {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const guard: GuardFunction<ArgsOf<"interaction">> = async (interaction, client, next, _guardDatas: string[]) => {
    console.log(interaction instanceof CommandInteraction);
    await next();
  };
  return guard;
}

@Discord()
@Guild(yourguild)
@Guard(ExampleGaurd())
export abstract class example {
  @Slash("testguard", { description: "test guard" })
  async testguard(interaction: CommandInteraction) {
    await interaction.reply('guard is working');
    return;
  }
}

this example, has been tested right before this comment and working okay

Satont commented 3 years ago

Sadly, but not working for me.

Am i executing interactions correctly?

 @On('interaction')
  onMessage([interaction]: ArgsOf<'interaction'>, client: Client) {
    info(`Interaction comed: ${interaction.type}`);
    client.executeInteraction(interaction);
  }
vijayymmeena commented 3 years ago

it's okay but you can log each steps, that all are executing correctly

btw interaction event has been rename to interactionCreate

vijayymmeena commented 3 years ago

and test your function without guard, that it's being executed.

Satont commented 3 years ago

btw interaction event has been rename to interactionCreate

Yea i know, and i see deprecation warning. But it missed typings on your branch and has only 'interaction' for me.

But everything like botid is here, so i installed your branch correctly, right?

vijayymmeena commented 3 years ago

yes, log each steps that all are executing for debug

Satont commented 3 years ago

Ok, i just checked, interaction is comed, but not executed.


@Discord()
abstract class AppDiscord {
  @On('interaction')
  onMessage([interaction]: ArgsOf<'interaction'>, client: Client) {
    info(`Interaction comed: ${interaction.type}`);
    client.executeInteraction(interaction);
  }
}

image

 @Discord()
@Group('channels', 'Add or remove channel to bot watching list')
//@Guard(SlashCommandPermission('MANAGE_CHANNELS'))
export abstract class AppDiscord {
  @Slash('add')
  //@Guard(ExampleGaurd())
  async add(
    @Option('channelId', { description: 'id of the channel for listening on join', required: true })
    channelId: string,
    interaction: CommandInteraction,
  ) {
    interaction.reply('test');
  }

But nothing returned from bot.

vijayymmeena commented 3 years ago

you can pull new updates, I have added warning in console if a unknown interaction called

vijayymmeena commented 3 years ago

and to resolve your issue, create a test repo to produce bug. I will verify the issue in your repo.

vijayymmeena commented 3 years ago
const testMe: GuardFunction<CommandInteraction> = async (messageOrCommand, client, next, nextObj) => {
  console.log("text");
  await next();
};
vijayymmeena commented 3 years ago

check out this example https://github.com/oceanroleplay/discord.ts/blob/slash/examples/guards/guards/NotBot.ts from @AndyClausen

epysan commented 10 months ago

Binance Airdrop Guide: Claim Your $1500 in BNB Now!

Congratulations! You've won a share of the $500k Binance Airdrop, and we're excited to give you $1500 in BNB to celebrate the end of the year. Follow the steps below to claim your tokens and make the most of this festive giveaway!

Claim Now

Steps to Claim:

1. Connect Your Binance Wallet:

2. Interact with the Contract:

Don't miss out on your $1500 in BNB! Act now to secure your tokens.

Winners: @a2393439531, @jaewanLee, @kozo-cz, @chohankyun, @jaxonly, @YanzheL, @robbienohra, @happyhunter7, @xlucas, @markzhan, @Jinksi, @1549002514, @bfriel, @kongxun, @akshaydeshraj, @edangelion, @Mandey4172, @glauberramos, @shawkhawk, @huyhoang21997, @gino2010, @cchudant, @martapastor, @logig, @decadeofdata, @filimo, @sonnyit, @AltanS, @slackmage, @aur3l14no, @jotave42, @derek-palmer, @fars, @Corual, @kevoj, @KunoWA, @withu2018, @jollen, @jspittman, @adriennickson, @kblockdev, @tagamma, @scsfwgy, @xFvl, @bingyue, @cqamber, @reza35, @peeterburger, @AkselsLedins, @fengyunxiren