如何使我的Discord机器人只忽略某些命令的设置前缀?(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!");
}
},
};

最新更新