NamVr / DiscordBot-Template

A boilerplate / template for discord.js v14 bots with 100% coverage of Discord API, command handler, error handler based on https://discordjs.guide/
https://djs.docs.namanvrati.me/
Apache License 2.0
316 stars 74 forks source link

[BUG] Issue utilising trigger based commands when multiple exist #29

Closed fakuzatsu closed 1 year ago

fakuzatsu commented 1 year ago

Issue When attempting to register multiple files within either commands or triggers/reactions (only tested these two), the bot is only regestering the first command it locates within the file strucure (typically alphabetically). The only slash command the bot is capable of executing is the help command for instance.

To reproduce I am using the repo exactly as is when cloned, except -of course- for adding the config.json file with my details. For testing purposes I provided my bot all bot permissions via the discord develeoper portal, as well as all privileged gateway intents. I figure the only difference between my codebase and yours is my node and npm versions (npm: 9.5.1, node: 18.16.0).

Expected behaviour I expect copying the hello.js file in triggers/reactions to the same directory and renaming to hello2.js with different names exported from the module.exports would allow each to function when called upon with it's name (I am using unique names). Or even that the provided commands in the /command folder would work. Rather than simply the help command.

Screenshots Please let me know if you need any!

Additional context There's a posibility I am doing something wrong here, but I've been bashing my head against a wall trying to sort it out for a few hours and have tried multiple configurations before eventually establishing that it wouldnt even work directly after a git clone.

fakuzatsu commented 1 year ago

I was able to get around the issue by altering the code within the triggerCreate.js event file. I removed the every function within the asynchronous callbacks, and replaced with a regular for loop.

The new code becomes:

triggerCreate.js


module.exports = {
name: "messageCreate",

async execute(message) { // Deconstructed client from message object. const args = message.content.split(/ +/);

// Checks if the trigger author is a bot. Comment this line if you want to reply to bots as well.
if (message.author.bot) return;

// Checking ALL triggers using a for...of loop and breaking out if a trigger was found.
let triggered = false;

for (const trigger of message.client.triggers.values()) {
  if (triggered) break;

  for (const name of trigger.data.name) {
    if (triggered) break;

    // If validated, it will try to execute the trigger.
    if (message.content.includes(name)) {
      try {
        await trigger.execute(message, args);
      } catch (error) {
        // If the trigger fails, reply back!
        console.error(error);
        message.reply({
          content: "There was an error trying to execute that trigger!",
        });
      }

      // Set the trigger to be true and break out of the loop.
      triggered = true;
      break;
    }
  }
}

}, };



I'm not 100% on whether this could break other aspects of the repo, as it stands. But I'll try to do the same for the other event files and verify that everything works.
NamVr commented 1 year ago

Hi, I'm not really sure what is wrong because this has been tested multiple times by multiple people. Let me ask, do you have problems with interaction based commands or legacy (message) based commands?

fakuzatsu commented 1 year ago

I actually thought I had issues with both, but I have since realised that my expectations of the interaction based commands were incorrect, and they are working fine in your repo. The real problem was with trigger commands, and seemed to be a consiquence of the .every function within the async function in triggerCreate.js (see the changes I have made above).

I’ve been through and added those for loops now and I’m no longer getting issues. Let me know if you need me to check anything.

EDIT: Clarification

NamVr commented 1 year ago

It's awesome if it works for you!

I agree trigger handler can be improved more, when it was made I was plainly frustrated to just get it work right, and I believe it was working right at that moment (still works in my private bots using this same template)