首页 文章

Discord Bot,遇到RichEmbeds和awaitMessages问题

提问于
浏览
2

我不太了解Javascript,所以请耐心等待 .

所以,我正在尝试为我的Discord Bot制作一个命令 . 基本上,我想要发生的是,当你发布“!records”时,我希望Bot发送一个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”时,我希望Bot回复正确的记录 . 这是我无法做对的部分 . 以下是所有代码:

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

此时,当您发布"!records"时,它会发送RichEmbed,但也会发送记录,而不是等待回复 .
enter image description here

有人可以概述我在这里做错了吗?

此外,我希望能够说一个命令来更新记录,而无需进入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的指南YouTube教程,但没有运气 .

这里的任何帮助将非常感激 .

2 回答

  • 0

    您没有正确使用 awaitMessages . 您只需要一个,而不是2个或更多 . 过滤器将检查消息是1还是2.这是您的代码应该是什么样子 . 我还修复了"time up"错误处理程序 .

    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 )并进行更新即可 . 如果这是你想要的,我建议你改变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);
    
  • 1

    如果您查看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));
    

    至于文件问题,您能详细说明文件的格式以及命令的外观吗?

相关问题