【问题标题】:How to create a discord.js command without a prefix?如何创建不带前缀的 discord.js 命令?
【发布时间】:2022-02-04 09:24:54
【问题描述】:

我正在尝试创建一个机器人,它会说晚安回到“晚安”作为辅助命令,但我不知道如何执行没有前缀的单个命令。我发现了一篇关于此的帖子,它有一个很好的答案并应用了它,但现在我遇到了不同的问题。

这是我的bot.js 主文件:

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = 'm!';

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('Meonkl is Online!');
    console.log(`Logged in as ${client.user.tag}!`);
    client.user.setPresence({
        status: "online",  //You can show online, idle....
    });
});

client.on('message', message =>{
    
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command == 'ping'){
        client.commands.get('pong').execute(message, args);
    }else if(message.content.toLowerCase() == 'goodnight'){
        client.commands.get('goodnight').execute(message, args);
    }

    console.log(message.member, message, args);
});

client.login('');

我怀疑问题出在这一行,if(!message.content.startsWith(prefix) || message.author.bot) return;,但我无法将其取出并破坏我的常规命令。

我不确定该怎么做,或者是否有干净的解决方案。

【问题讨论】:

  • 为什么不直接修改ocndition,如果消息以前缀开头,消息内容只能是“晚安”?
  • @esqew 我该怎么做?
  • if (/* starts with prefix? */) { /* command handling */ } else if (/* says goodnight */) { /* do something */ }
  • bot 本来就是监听所有消息的,前缀是其他应用用来区分的,所以不会互相冲突。如果你不想要任何前缀,就不要实现它。

标签: javascript discord.js


【解决方案1】:

如果传入的消息不是以prefix 开头,则消息处理函数 (if(!message.content.startsWith(prefix) || message.author.bot) return) 的第一行将不允许您执行任何其他操作。您可以删除!message.content.startsWith(prefix) 部分:

client.on('message', message => {
    if (message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping') {
        client.commands.get('pong').execute(message, args);
    } else if(message.content.toLowerCase() === 'goodnight') {
        client.commands.get('goodnight').execute(message, args);
    }
});

或者你可以让一些不带前缀的命令通过;只需使用一个数组来存储这些并检查消息是否包含在此数组中。检查下面的sn-p:

client.on('message', (message) => {
  if (message.author.bot) return;

  const unprefixedCommands = ['goodnight', 'morning'];
  const isUnprefixedCommand = unprefixedCommands.includes(message.content.toLowerCase());

  if (!isUnprefixedCommand && !message.content.startsWith(prefix)) return;

  let args;
  let command;

  if (isUnprefixedCommand) {
    // don't remove the prefix from the message
    // although it will be an array with the command only
    // not sure how you want to use it :)
    args = message.content.split(/ +/);
    // the command is the message content itself
    // this way both "!goodnight" and "goodnight" will work
    command = message.content.toLowerCase();
  } else {
    // it's the same as before
    args = message.content.slice(prefix.length).split(/ +/);
    command = args.shift().toLowerCase();
  }

  if (command === 'ping') {
    client.commands.get('pong').execute(message, args);
  } else if (command === 'goodnight') {
    client.commands.get('goodnight').execute(message, args);
  }
});

【讨论】:

    【解决方案2】:

    我不知道这是否有帮助,但对于没有命令的消息,我只需开始新的代码行并输入,例如,

    client.on('message', msg => {
    if (msg.content === 'Goodnight!') {
    msg.reply('Pingas!');
    }
    });
    

    【讨论】:

    • 如果您想不回复就这样做,而不是“msg.reply”,您可以输入“msg.channel.send”。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2018-01-03
    • 2021-09-10
    • 2021-04-04
    • 2018-07-21
    相关资源
    最近更新 更多