簡體   English   中英

如何讓我的 Discord 機器人只忽略某些命令的設置前綴? (不和諧.js)

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

我對 Java 很陌生,並試圖編寫一個簡單的機器人。 當我第一次編寫此代碼時,它沒有包含多個文件的命令處理程序,並且一切正常。 然而,雖然發送隨機圖像等一些命令仍然有效,但我無法找到一種方法讓我的機器人忽略某些命令的前綴:例如,在我的機器人可以響應“你在哪里?”之前帶有“我在這里”而沒有前綴“。” 在前。 當我將此命令包含在帶有 if 語句的 index.js 文件中時,它仍然有效。 但試圖把它放在另一個文件中不會。 如果有人可以幫助我,我將不勝感激。

這是我的代碼:

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);

我的一些命令文件:

sendrand.js(這個有效)

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(這個沒有)

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

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

我知道我可以讓機器人響應“,你在哪里”,但如果可能的話,我希望它沒有前綴

你可以這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM