![](/img/trans.png)
[英]How can I make my bot set a slowmode on a Discord channel, 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.