【问题标题】:Trying to do a single Discord.js command without a prefix尝试执行一个不带前缀的 Discord.js 命令
【发布时间】:2020-12-10 02:22:32
【问题描述】:

我正在尝试像 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 文件,它太长了。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    解决方案非常简单。所有命令都有前缀,因此我不会将您尝试创建的命令视为命令,我会说它在技术上更像是一种自动响应。因此,您不应使用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 变量并了解如何检索它们的值。实际上,您的代码似乎严重依赖于一个简单的模板,但如果您想完全自定义机器人/命令的外观和功能,则不能完全依赖此模板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2020-11-08
      • 2018-01-03
      • 2021-09-10
      • 2021-04-04
      • 2020-10-30
      • 2010-12-15
      相关资源
      最近更新 更多