我正在尝试制作一个类似dank memer的命令,当有人说F或F时,机器人会回复F。我的问题是,没有前缀它就无法工作,但我希望能够在没有前缀的情况下完成。这是我的密码。我使用命令处理程序。
//THIS IS THE INDEX.JS FILE
const Discord = require('discord.js');
const { default_prefix, token_bot } = require('./config.json');
const client = new Discord.Client();
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./Commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Ready Player One!');
});
client.on('message', message => {
if (!message.content.startsWith(default_prefix) || message.author.bot) return;
const args = message.content.slice(default_prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'f'){
client.commands.get('f').execute(message, args);
}
这个是它调用的f.js命令。
module.exports = {
name: 'f',
description: "This is an f command",
execute(message, args){
message.channel.send('F');
},
};
这不是我的全部index.js文件,它太长了。
这个问题的解决方案非常简单。所有的命令都有前缀,所以我不会考虑你试图创建的命令,我会说它在技术上更像是一个自动响应。因此,您不应该使用args
或command
变量的代码来检查";F";已经发送,您应该直接检查邮件的内容。
client.on('message', message => {
if (!message.content.startsWith(default_prefix) || message.author.bot) return;
const args = message.content.slice(default_prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (message.content.toLowerCase() == 'f'){
client.commands.get('f').execute(message, args);
}
});
我建议查看args
和command
变量,并了解如何检索它们的值。事实上,您的代码似乎严重依赖于一个简单的模板,但如果您想完全自定义bot/命令的外观和功能,则不能完全依赖于此模板。