owengombas / discord.ts

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

[Question/bug] The @CommandNotFound decorator seems to not function for me #37

Closed LiquidMonkey closed 3 years ago

LiquidMonkey commented 3 years ago

Thank you for this great framework :D its very pleasant to work with.

Unfortunately I can't seem to manage to get the CommandNotFound decorator to work. Can you help me?

I have the following code for my commandnotfound function.

import {
    CommandNotFound,
    ArgsOf,
    Guard,
  } from "@typeit/discord";

import {ShouldRespond} from "../Guard/ShouldRespond";

export abstract class CommandNotFoundEvent {
  @CommandNotFound()
  @Guard(ShouldRespond)
  async notFound([message]: ArgsOf<"commandMessage">) {
    // Do something when a command is not found
    message.reply("Command not found check `--list for commands`");
  }
}

the ShouldRespond guard looks like this:

import {
    Client,
    GuardFunction,
} from "@typeit/discord";

export const ShouldRespond: GuardFunction<"message"> = async (
    [message],
    client: Client,
    next,
    guardDatas
  ) => {
    if (message.content.startsWith(globalThis.settings.prefix) && message.author.bot === false) {
      await next();
    }
  }

With the above code I run into this issue where whenever this event would fire it gives me this error in the console.

(node:17460) TypeError: undefined is not a function at CommandNotFoundEvent.notFound (C:**\build\src\Event\CommandNotFound.js:8:19)

It throws this error 2 times with different errors: UnhandledPromiseRejectionWarning: TypeError

and also gives a DepreciationWarning: Unhandled promise rejections are deprecated. In the future, promis rejections that are not handled will terminate the node.js process

Any help would be highly appreciated.

SteeledSlagle13 commented 3 years ago

should be able to use

@CommandNotFound()
  async notFound(message: Message): any {
    console.error('command not found', message);
  }
samlythemanly commented 3 years ago

+1 this is definitely a bug somehow, as it's not working for me, too.

Not doing anything fancy with it like OP is, either.

@CommandNotFound()
async commandNotFound(message: CommandMessage): Promise<void> {
  await message.channel.send(
    "I'm sorry, I don't understand! Please type '!help' for a list of " +
      'commands I understand.'
  );
}
SteeledSlagle13 commented 3 years ago

+1 this is definitely a bug somehow, as it's not working for me, too.

Not doing anything fancy with it like OP is, either.

@CommandNotFound()
async commandNotFound(message: CommandMessage): Promise<void> {
  await message.channel.send(
    "I'm sorry, I don't understand! Please type '!help' for a list of " +
      'commands I understand.'
  );
}

whats your console output when this happens? image

I just used your code snippet and it worked @somarkoe see image above

SteeledSlagle13 commented 3 years ago

I'll be closing this issue soon the issue @LiquidMonkey has is they are using ArgsOf in the notFound method ArgsOf is for discord events and command message only image

and because the message is not a command the ArgsOf is not applicable here

@CommandNotFound()
  @Guard(ShouldRespond)
  async notFound(message: Message): Promise<void> {
    // Do something when a command is not found
    message.reply("Command not found check `--list for commands`");
  }

will fix their issue

samlythemanly commented 3 years ago

whats your console output when this happens?

There's absolutely no console output when I type a command that isn't registered with the bot :( It just ignores it entirely.

SteeledSlagle13 commented 3 years ago

@somarkoe What does your project set up look like? Is this in a class that has @Discord on it?

samlythemanly commented 3 years ago

Here's my setup:

// bot.ts

export class Bot {
  private _client: Client;

  constructor() {
    this._client = new Client({
      classes: [AdminCommands, GeneralCommands],
      silent: false,
      variablesChar: ':',
    });
  }

  ...
}
// admin_commands.ts

/**
 * Commands only admins have access to.
 */
@Discord('!')
export abstract class AdminCommands {
  ...
}
// general_commands.ts

/**
 * General commands available to everybody.
 */
@Discord('!')
export abstract class GeneralCommands {
  ...

  /**
   * Sends a message to the channel when a command is executed that isn't
   * defined.
   */
  @CommandNotFound()
  async commandNotFound(message: CommandMessage): Promise<void> {
    await message.channel.send(
      "I'm sorry, I don't understand! Please type '!help' for a list of " +
        'commands I understand.'
    );
  }
}

All commands defined within both GeneralCommands and AdminCommands work.

SteeledSlagle13 commented 3 years ago

curious to see if putting the path instead of the imported classes would do anything

kinda like here https://github.com/OwenCalvin/discord.ts/blob/master/starter-projects/simple-bot/SimpleBot/src/Main.ts thats how it is here too https://github.com/OwenCalvin/discord.ts

maybe?