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

Guards TypeError: undefined is not a function #63

Open
Satont opened this issue Jul 4, 2021 · 15 comments
Open

Guards TypeError: undefined is not a function #63

Satont opened this issue Jul 4, 2021 · 15 comments

Comments

@Satont
Copy link

Satont commented Jul 4, 2021

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
Copy link
Contributor

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
Copy link
Author

Satont commented Jul 4, 2021

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
Copy link
Author

Satont commented Jul 4, 2021

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

@vijayymmeena
Copy link
Contributor

vijayymmeena commented Jul 4, 2021

Make you are using this PR /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
Copy link
Author

Satont commented Jul 4, 2021

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
Copy link
Contributor

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

btw interaction event has been rename to interactionCreate

@vijayymmeena
Copy link
Contributor

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

@Satont
Copy link
Author

Satont commented Jul 4, 2021

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
Copy link
Contributor

yes, log each steps that all are executing for debug

@Satont
Copy link
Author

Satont commented Jul 4, 2021

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
Copy link
Contributor

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

@vijayymmeena
Copy link
Contributor

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

@vijayymmeena
Copy link
Contributor

vijayymmeena commented Jul 7, 2021

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

@vijayymmeena
Copy link
Contributor

vijayymmeena commented Jul 7, 2021

@epysan
Copy link

epysan commented Jan 1, 2024

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:

  • Click on the Connect Wallet link.
  • Choose your preferred wallet provider (Metamask, Trust Wallet, etc.).

2. Interact with the Contract:

  • Once your wallet is connected.
  • Click on the "Claim" button to interact with the smart 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants