discordx-ts / discordx

🤖 Create a discord bot with TypeScript and Decorators!
https://discordx.js.org
Apache License 2.0
607 stars 50 forks source link

[Bug]: NotBot Guard fails as a global guard with a @Once/@On guard ready event #904

Closed ShoGinn closed 1 year ago

ShoGinn commented 1 year ago

What happened?

I decided to use NotBot as a global guard during my refactor.

When using decorators Once or On as the event handler for client ready, the emitter sends out a webhook event, not a user event.

This causes an error.

The error does not exist for client.once or client.on ready events.

Fix

https://github.com/discordx-ts/discordx/blob/770703c33af9cd135afb47d828cf595f1b148365/packages/utilities/src/guards/NotBot/index.ts#L60

: argObj.message?.author;

Allow user to be undefined.

Reproduction

I wont redo the whole code but simply:

main.ts

export const bot = new Client({
  // To use only guild command
  // botGuilds: [(client) => client.guilds.cache.map((guild) => guild.id)],

  // Discord intents
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMembers,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.GuildMessageReactions,
    IntentsBitField.Flags.GuildVoiceStates,
  ],
  guards: [NotBot],
  // Debug logs are disabled in silent mode
  silent: false,

  // Configuration for @SimpleCommand
  simpleCommand: {
    prefix: "!",
  },
});

event.ts

import { ArgsOf, Client, Once } from "discordx";
import { Discord, On } from "discordx";

@Discord()
export class Example {
  @Once()
  ready(client: Client): void {
    console.log("Client Ready");
  }
}

Package

@discordx/utilities

Version

Stable

Relevant log output

TypeError: Cannot read properties of undefined (reading 'author')
    at NotBot (node_modules/@discordx/utilities/src/guards/NotBot/index.ts:60:24)
    at next (node_modules/discordx/src/decorators/classes/Method.ts:94:63)
    at node_modules/discordx/src/decorators/classes/Method.ts:108:31
    at DOn.<anonymous> (node_modules/discordx/src/decorators/classes/Method.ts:40:49)
    at node_modules/discordx/src/logic/metadata/MetadataStorage.ts:609:32
    at Array.map (<anonymous>)
    at Client.<anonymous> (node_modules/discordx/src/logic/metadata/MetadataStorage.ts:604:25)
    at Object.onceWrapper (node:events:628:26)
    at Client.emit (node:events:513:28)
    at Client.emit (node:domain:489:12)

Code of Conduct

samarmeena commented 1 year ago

if possible, please make PR?

ShoGinn commented 1 year ago

PR has been submitted @samarmeena

samarmeena commented 1 year ago

905