【问题标题】:Discord Bot, having trouble with RichEmbeds & awaitMessagesDiscord Bot,在 RichEmbeds 和 awaitMessages 上遇到问题
【发布时间】:2018-09-13 09:54:22
【问题描述】:

我不太懂Javascript,所以请多多包涵。

所以,我正在尝试为我的 Discord Bot 发出命令。基本上,我想要发生的是,当您发布“!records”时,我希望机器人发送一个 RichEmbed,其中包含您可以选择的 2 个选项。

if (message.content.startsWith(config.prefix + 'records')) {
  const embed = new Discord.RichEmbed()
    .setTitle('D E I C I D E Records')
    .setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png')
    .setColor(3447003)
    .setDescription('Please select an option.')
    .setTimestamp()
    .addField('1. Raid Wins', 'Respond with "1" for Raid Wins.')
    .addField('2. Defense Wins', 'Respond with "2" for Defense Wins.')

message.channel.send({embed})

我已经确定了那部分。它正是我想要的。它发送带有两个选项的 RichEmbed。

接下来我想要发生的是,当您发送“1”或“2”时,我希望机器人回复正确的记录。这是我无法正确处理的部分。以下是所有代码:

if (message.content.startsWith(config.prefix + 'records')) {
  const embed = new Discord.RichEmbed()
    .setTitle('D E I C I D E Records')
    .setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png')
    .setColor(3447003)
    .setDescription('Please select an option.')
    .setTimestamp()
    .addField('1. Raid Wins', 'Respond with "1" for Raid Wins.')
    .addField('2. Defense Wins', 'Respond with "2" for Defense Wins.')

    message.channel.send({embed})
    message.channel.awaitMessages(response => response.content === '1', {
      max: 1,
      time: 10000,
      errors: ['Ran out of time!'],
    })
    message.channel.send(drecords.RaidWins)
      message.channel.awaitMessages(response => response.content === '2', {
        max: 1,
        time: 10000,
        errors: ['Ran out of time!']
    })
    message.channel.send(drecords.DefenseWins)
} else

目前,当您发布“!记录”时,它会发送 RichEmbed,但也会发送记录,而不是等待回复。

谁能概述一下我在这里做错了什么?

另外,我希望能够说一个命令来更新记录,而无需进入 Bot 的文件并手动执行。

if (message.content.startsWith(config.prefix + 'updateraids')) {
  let newRecords = message.content.split(" ").slice(1, 2)[0];
  drecords.RaidWins = newRecords;

  fs.writeFile('./drecords.json', JSON.stringify(drecords), (err) => console.error);
} else

if (message.content.startsWith(config.prefix + 'updatedefenses')) {
  let newRecords = message.content.split(" ").slice(1, 2)[1];
  drecords.DefenseWins = newRecords;

  fs.writeFile('./drecords.json', JSON.stringify(drecords), (err) => console.error);
}

目前,当您说出这些命令中的任何一个,然后说出一些文本时,例如“!updatedefenses DefenseWin2”,它会替换第一个字符串 (DefenseWin1),而不是在 Bot 的文件中添加另一个字符串。我将如何删除一个条目而不是添加一个条目?对不起,如果这里有点太多,想把它们都挤到一篇文章中,因为它们都是相关的。

我已经完成了好几个小时的研究,浏览了其他 Stackoverflow 问题、浏览了 Discord.js 和 An Idiot's Guide YouTube 教程,但没有运气。

我们将不胜感激。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您没有正确使用awaitMessages。您的情况只需要一个,而不是 2 个或更多。过滤器将检查消息是 1 还是 2。这是您的代码的外观。我还修复了您的“超时”错误处理程序。

    client.on("message", message => {
        if (message.content.startsWith("/records")) {
            const embed = new Discord.RichEmbed()
            .setTitle('D E I C I D E Records')
            .setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png')
            .setColor(3447003)
            .setDescription('Please select an option.')
            .setTimestamp()
            .addField('1. Raid Wins', 'Respond with "1" for Raid Wins.')
            .addField('2. Defense Wins', 'Respond with "2" for Defense Wins.');
            message.channel.send({embed})
    
            message.channel.awaitMessages(response => (response.content === '1' || response.content === "2"), {
                max: 1,
                time: 10000,
                errors: ['time']
            }).then(mg => {
                if (mg.first().content === "1"){ //Have to use .first() because mg is a Collection
                    message.channel.send(drecords.RaidWins);
                }else if (mg.first().content === "2"){
                    message.channel.send(drecords.DefenseWins);
                }
            }).catch((err) => {
                message.channel.send("Ran out of time!");
            })
        }
    })
    

    至于第二个问题,当您使用 .writeFile 时,您实际上是在覆盖文件。你会想要使用appendFile。但是,您使用的是 JSON。您可以简单地导入文件 (require it) 并更新它。如果这是您想要的,我是否建议您将 json 文件更改为如下所示。

    {
        "RaidWins": 0,
        "DefenseWins": 0
    }
    

    然后导入和更新会是这样的

    let records = require("./drecords.json");
    records.RaidWins += 1; //adding one to the wins
    
    fs.writeFile("./drecords.json", JSON.Stringify(records), err => console.error);
    

    【讨论】:

    • 这帮了很多忙。第一个问题解决了。但是对于第二个问题,抱歉,如果不清楚,我实际上想用另一个字符串更新它,所以“!updateraids [组名称]”。类似的东西。有什么办法吗?谢谢。
    • 那么喜欢records.RaidWins = "newString"? @Ignoratus
    • 是的,没错。因此,当我说命令“!updateraids”,然后是一些文本时,该文本会进入第一个条目旁边的 Bot 文件,而不是覆盖它。这意味着当我说“!记录”并选择 Raid 获胜时,它会显示第一个条目以及第二个条目,以及我在那里输入的所有其他条目。如果仍然没有意义,请随时提出更多问题。 @赖特
    • 为什么不把它列个清单呢? "RaidWins": [] 然后只是 push 到列表 @Ignoratus
    • 我该怎么做?我目前在外部文件 (drecords.json) 中有该列表。我对push 和数组进行了大量研究,昨晚我花了几个小时试图让它工作。我已经缩短了命令,但这是两个命令之一的代码:if (message.content.startsWith(config.prefix + 'ur')) { var newRRecords = drecords.RaidWins newRRecords.push() fs.appendFile('../drecords.json', JSON.stringify(drecords), (err) => console.error); } 这是 drecords.json 文件的代码:{ "RaidWins": [], "DefenseWins": [] }@Wright
    【解决方案2】:

    如果您查看awaitMessages section of the docs 中的示例,您似乎需要将发送响应的代码部分放在.then 中。

    类似

    message.channel.awaitMessages(response => response.content === '1', {
      max: 1,
      time: 10000,
      errors: ['Ran out of time!'],
    })
    .then(message.channel.send(drecords.RaidWins));
    
    message.channel.awaitMessages(response => response.content === '2', {
        max: 1,
        time: 10000,
        errors: ['Ran out of time!']
    })
    .then(message.channel.send(drecords.DefenseWins));
    

    至于文件问题,能否详细说明文件的格式和命令是什么样的?

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2021-06-19
      • 2020-09-17
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多