Javascript Discord Bot,与RichEmbeddes&;等待消息
我不太懂Javascript,所以请容忍我 所以,我正试图为我的Discord机器人发出命令。基本上,我希望发生的是,当你发布“!records”时,我希望机器人发送一个RichEmbed,其中包含两个选项,你可以选择Javascript Discord Bot,与RichEmbeddes&;等待消息,javascript,discord,discord.js,Javascript,Discord,Discord.js,我不太懂Javascript,所以请容忍我 所以,我正试图为我的Discord机器人发出命令。基本上,我希望发生的是,当你发布“!records”时,我希望机器人发送一个RichEmbed,其中包含两个选项,你可以选择 if (message.content.startsWith(config.prefix + 'records')) { const embed = new Discord.RichEmbed() .setTitle('D E I C I D E Records')
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,但也会发送记录,而不是等待回复
有人能概括一下我做错了什么吗
此外,我希望能够说一个命令来更新记录,而不必进入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,以及YouTube上的《白痴指南》教程,但没有运气
这里的任何帮助都将不胜感激。如果您查看中的示例,似乎需要将发送响应的代码部分放在
中。然后
差不多
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));
关于文件问题,你能详细说明一下文件的格式和命令的外观吗?你没有正确使用
waitingmessages
。您只需要一个,而不是两个或更多的案件。过滤器将检查消息是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[Name of group]”。差不多吧。有什么办法吗?谢谢。像是
records.RaidWins=“newString”
@是的,没错。因此,当我说命令“!updateraids”,后跟一些文本时,这些文本会进入机器人程序文件中第一个条目旁边,而不是覆盖它。意思是当我说“!records”并选择Raid wins时,它会显示第一个条目和第二个条目,以及我在其中输入的所有其他条目。如果它仍然没有意义,请随意问更多的问题@Wright为什么不列个清单呢<代码>“RaidWins”:[]然后只需将推到列表@ignoratush我该怎么做?我目前在一个外部文件(drecords.json)中有这个列表。我对推送和阵列做了很多研究,昨晚我花了几个小时试图让它工作。我已经缩短了命令,但下面是两个命令之一的代码: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