简体   繁体   中英

How can I make my Discord bot ignore the set prefix for certain commands only? (Discord.js)

I'm very new to Java and trying to code a simple bot. When I first coded this, it did not have a command handler with multiple files, and everything worked. However, while some commands like sending random images still work, I can't figure out a way to make my bot ignore prefixes for certain commands: For example, before my bot could respond to "Where are you?"with "I am here" without having the prefix "." in front. When I include this command in the index,js file with an if statement it still works. but trying to put it in another file doesn't. I'd really appreciate if anyone could help me with this.

Here is my code:

index.js

const discord = require('discord.js');

const client = new discord.Client({ disableMentions: 'everyone' });

client.config = require('./config/bot');
client.commands = new discord.Collection();

fs.readdirSync('./commands').forEach(dirs => {
    const commands = fs.readdirSync(`./commands/${dirs}`).filter(files => files.endsWith('.js'));

    for (const file of commands) {
        const command = require(`./commands/${dirs}/${file}`);
        console.log(`Loading command ${file}`);
        client.commands.set(command.name.toLowerCase(), command);
    };
});

const events = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of events) {
    console.log(`Loading discord.js event ${file}`);
    const event = require(`./events/${file}`);
    client.on(file.split(".")[0], event.bind(null, client));
};

client.login(client.config.discord.token);

Some of my commands files:

sendrand.js (This one works)

module.exports = {
    name: 'sendrand',
    category: 'sendpic',

    execute(client, message, args) {
        var num = 33;
        var imgNum = Math.floor(Math.random() * (num - 1 + 1) + 1);
        message.channel.send({files: ["./randpics/" + imgNum + ".png"]});
    }
};

where.js(This one doesn't)

module.exports = {
    name: 'where',
    category: 'core',

    execute(client, message, args) {
        if(message.startsWith('where are you){
            message.channel.reply('I am here!)
    }
};

I know I could make it so that the bot would respond to ",where are you", but I'd like to have it without the prefix if possible

You could do:

module.exports = {
  name: "respond",
  category: "general",
  execute(client, message) {
    if (message.content.toLowerCase().startsWith("where are you")) {
      message.reply("I am here!");
    }
  },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM