gpt4 book ai didi

javascript - Discord Bot,在使用 RichEmbeds 和 awaitMessages 时遇到问题

转载 作者:行者123 更新时间:2023-11-29 23:24:51 24 4
gpt4 key购买 nike

我不太了解 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”时,我希望机器人用正确的记录回复。这是我做不对的部分。以下是所有代码:

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

有人可以概述一下我在这里做错了什么吗?

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

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 教程,但没有运气。

如有任何帮助,我们将不胜感激。

最佳答案

您没有正确使用 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 它)并更新它。如果这是您想要的,我建议您将 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);

关于javascript - Discord Bot,在使用 RichEmbeds 和 awaitMessages 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com