【问题标题】:some discord bot command aliases work and some don't, not getting any errors?一些不和谐的机器人命令别名有效,有些则无效,没有出现任何错误?
【发布时间】:2020-04-15 19:54:52
【问题描述】:

所以我正在创建一个不和谐的机器人,它似乎可以完美地工作,除了一件事:有时,命令别名不起作用。他们只是没有得到回应。

我怀疑这可能与撇号/特殊字符有关,但经过一些测试后,我得出结论认为这两者都不会导致错误。然后,我认为这可能是别名长度的问题,但我用一个随机的长词作为别名对其进行了测试,并且效果很好。我四处搜索,并没有发现关于别名的限制或为什么会发生这种情况,所以我完全不知所措。

这是我的一个错误命令的代码:

const Discord = require("discord.js");
const colors = require("../../colors.json");

module.exports.run = async (bot, message, args) => {
    let embed = new Discord.RichEmbed()
    .setColor(colors.purple)
    .setAuthor('FIREFLY CURSE', 'placeholder.image.link', 'placeholder.link')
    .setDescription("placeholder description")
    .setThumbnail('placeholder.image.link')
    .addField('Test', 'X', false)
    .addField('Test', "Y", false);
    message.channel.send({embed:embed});
}

module.exports.config = {
    name: "firefly curse",
aliases: ["fireflycurse", "fireflyc", "fc", "firefly curse", "supercalifragilisticexpialidocious", "test'test", "test test", "numbertest",]
}

上面列出了我尝试过的别名。除了“萤火虫诅咒”和“测试测试”之外的所有工作。这让我觉得空间是问题,但是在 不同的 命令中,带有空格的别名 does 工作。除了嵌入文本中的文本不同之外,代码之间绝对没有区别,这不应该影响代码本身的执行。

命令处理程序代码:

const fs = require("fs");
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();

async function load(dir){
fs.readdir(`./commands/${dir}/`, (err, files) => {
        if(err) console.log(err)

        let jsfile = files.filter(f => f.split(".").pop);
        if(jsfile.length <= 0) {
            return console.log("commands missing!");
        }

    jsfile.forEach((f, i) => {
        let pull = require(`./commands/${dir}/${f}`)
        bot.commands.set(pull.config.name, pull);  
        pull.config.aliases.forEach(alias => {
            bot.aliases.set(alias, pull.config.name)

        })
    })
});
}

Bot.on 代码块:

bot.on("message", async message => {
    if(message.author.bot || message.channel.type === "dm") return;

    let prefix = botconfig.prefix;
    let messageArray = message.content.split(" ")
    let cmd = messageArray[0];
    let args = messageArray.slice(1);

    if(!message.content.startsWith(prefix)) return;
    let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))
    if(commandfile) commandfile.run(bot,message,args)

})

【问题讨论】:

  • 请显示命令处理程序代码(它如何检测消息是否为命令以及它如何存储命令)。
  • 添加到主帖(我想?我可能错过了一些,因为我不确定你指的是什么)

标签: javascript bots discord discord.js


【解决方案1】:

请显示您的bot.on message 块。

像往常一样,机器人命令会带有空格,因此如果命令名称有空格,则不能由命令 hendler 处理

附:很抱歉在回答中问它,不能写 cmets :C

【讨论】:

  • 添加到主帖。我希望它像空格一样简单,但就像我提到的那样,它确实在某些情况下有效
  • 是的,您的 cmd 是消息中的第一个单词,因此当您尝试使用空格获取命令时,机器人找不到它。作为您可以添加条件的原因:if(!commandfile &amp;&amp; messageArray.length &gt; 1) messageArray = message.content.split(" ") cmd = messageArray.splice(0,2).join(' ') args = messageArray.slice(2); commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length))) if(commandfile) commandfile.run(bot,message,args)
【解决方案2】:

查看从消息中获取命令的代码,您需要考虑到空格可以在命令中,也可以在参数中。 现在,您只期望并获得消息的第一个单词,并将其作为命令映射的键,但是,您还应该允许参数的第一个元素允许空格:

if (!message.content.startsWith(botconfig.prefix)) return;

const args = message.content.slice(botconfig.prefix.length).trim().split(' ');
const cmd = args.shift().toLowerCase();
// this is a simplified way to get the command, removing the prefix and creating the arguments

let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))

const commandFile = args.length === 0 ? bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd)) : bot.commands.get(cmd  + ' ' + args[0]) || bot.commands.get(bot.aliases.get(cmd + ' ' + args[0])) || bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd));
// the ternary operator

if (commandfile) commandfile.run(bot,message,args)

如果你不熟悉ternary operators,那么这是扩展代码:

let commandFile;
if (args.length === 0) {
  commandFile = bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd));
  // this will only run if there are no arguments in the message. You can expect the command to have no spaces
} else {
  commandFile = bot.commands.get(cmd  + ' ' + args[0]) || bot.commands.get(bot.aliases.get(cmd + ' ' + args[0])) || bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd));
  // since the arguments length is larger than 0, the cmd + (space) + first index of arguments may be a command or alias, so check for that first. If there isn't any found, check if cmd is a command or alias
}

【讨论】:

  • 我明白你在说什么,但我不确定如何将你的代码合并到我自己的代码中。 (尝试了很多不同的方法来替换现有的 arg/cmd 变量,但无济于事)如果不是太麻烦,您能否更具体地说明我需要替换的内容?
  • 我只在message 事件中使用了您的代码(并替换了该部分),并且只是更改了几行。
【解决方案3】:

您想要创建像!say hello 这样运行的命令,因此机器人将发送hello 消息。无需为此添加别名,例如 say hello

在你的机器人脚本中,除了一个错误之外,一切都是正确的:

const Discord = require("discord.js");
const colors = require("../../colors.json");

module.exports.run = async (bot, message, args) => {

    if(args[0] === 'curse'){

    let embed = new Discord.MessageEmbed()
    .setColor(colors.purple)
    .setAuthor('FIREFLY CURSE', 'placeholder.image.link', 'placeholder.link')
    .setDescription("placeholder description")
    .setThumbnail('placeholder.image.link')
    .addField('Test', 'X', false)
    .addField('Test', "Y", false);
    message.channel.send({embed:embed});
}
}

module.exports.config = {
    name: "firefly",
    aliases: []
}

现在试试命令!firefly curse,这样就可以了!

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 2023-03-08
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 2014-10-05
    • 2019-10-11
    相关资源
    最近更新 更多