1

I have been trying to make a command handler with prefix, but I'm getting a problem, the bot won't respond and there is no error either

// Index
---// Collections
client.functions = new Collection();
client.commands = new Collection();
client.interactions = new Collection();

---// Handlers
const handlers = fs
    .readdirSync("./config/handlers/")
    .filter((file) => file.endsWith(".js"));
const commandFolders = fs.readdirSync("./config/commands/");
const interactionFolders = fs.readdirSync("./config/interactions");
const eventFiles = fs
    .readdirSync("./config/events/")
    .filter((file) => file.endsWith(".js"));

(async () => {
    for (file of handlers) {
        require(`./config/handlers/${file}`)(client);
    }
    client.handleCommands(commandFolders);
    client.handleInteractions(interactionFolders);
    client.handleEvents(eventFiles);
    client.login(token);
})();

// The event handler
module.exports = (client) => {
    client.handleEvents = async (eventFiles) => {
        for (const file of eventFiles) {
            const event = require(`../events/${file}`);
            if (event.once) {
                client.once(event.name, (...args) => event.execute(...args, client));
            } else {
                client.on(event.name, (...args) => event.execute(...args, client));
            }
            console.log(`[EVENT HANDLER] - ${file} has been loaded.`);
        }
    };
};


// command handler
const fs = require("fs");

module.exports = (client) => {
    client.handleCommands = async (commandFolders) => {
        for (const folder of commandFolders) {
            const commandFiles = fs
                .readdirSync(`./config/commands/${folder}`)
                .filter((file) => file.endsWith(".js"));
            for (const file of commandFiles) {
                const command = require(`../commands/${folder}/${file}`);
                client.commands.set(command.name, command);
                console.log(`[COMMANDS HANDLER] - ${file} has been loaded`);
            }
        }
    };
};


// and in the last my messageCreate.js
const prefix = ".";
module.exports = {
    name: "messageCreate",
    async execute(message, client) {
        const args = message.content.slice(prefix.length).trim().split(/ +/);
        const command = args.shift().toLowerCase();
        if (!message.user.id === "798928603201929306") return;
        console.log(
            `${message.user.tag} used ${command} in #${message.channel} triggered an command.`
        );
        if (
            !message.content.startsWith(prefix) ||
            message.author.bot ||
            message.channel.type === "DM"
        )
            return;

        if (!client.commands.has(command)) return;

        try {
            client.commands.get(command).execute(message, args, client);
        } catch (error) {
            console.error(error);
            message.reply("there was an error trying to execute that command!");
        }
    },
};


// ping command
module.exports = {
    name: "ping",
    async execute(message, args, client) {
        message.channel.send(`Pong ${client.ws.ping}`)
    }
} // I haven't tried this yet because I'm getting an error in the messageCreate.js

notes:

1. sorry for the long code block

2. hope I inputted everything for getting enough help/info

3. my discord.js version is v13

4. if you need more info about my files, let me know in the comments

lvlahraam
  • 13
  • 4
  • i tried and added `console.log(`${command} has been triggered.`);` after the event in messageCreate.js and i see that nothing would log – lvlahraam Aug 26 '21 at 19:09
  • 2
    Does this answer your question? "[message event listener not working properly](https://stackoverflow.com/q/64394000/90527)", "[Having trouble sending a message to a channel with Discord.js](https://stackoverflow.com/q/68795635/90527)", "[Discord bot is not replying to messages](https://stackoverflow.com/q/68804831/90527)". – outis Oct 30 '21 at 22:21

1 Answers1

1

It is likely that you created your client with the wrong intents. If you need to listen to messages in servers, make sure you use the GUILD_MESSAGES intent.

const bot = new Discord.Client({intents:[Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES]})
theusaf
  • 1,781
  • 1
  • 9
  • 19
  • shouldn't the intents be `discord.Intents.FLAGS.GUILDS, discord.Intents.FLAGS.GUILD_MEMBERS` ?I think writing them out was an older version of djs, but not entirely sure – Christoph Blüm Aug 26 '21 at 20:22