Discord Bot,在使用 RichEmbeds 和 awaitMessages 时遇到问题
Discord Bot, having trouble with RichEmbeds & awaitMessages
我知道的不多Javascript,还请多多包涵。
所以,我正在尝试为我的 Discord 机器人发出命令。基本上,我想要发生的是,当您 post“!记录”时,我希望 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
目前,当您 post "!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 的文件中添加另一个字符串。我将如何删除一个条目而不是添加一个条目?对不起,如果这里有点太多,我想把它们全部压缩成一个post,因为它们都是相关的。
我已经做了好几个小时的研究,研究了其他 Whosebug 问题,研究了 Discord.js,以及 An Idiot's Guide YouTube 教程,但没有成功。
如有任何帮助,我们将不胜感激。
如果您查看 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));
关于文件问题,您能否详细说明文件的格式以及命令是什么样的?
您没有正确使用 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);
我知道的不多Javascript,还请多多包涵。
所以,我正在尝试为我的 Discord 机器人发出命令。基本上,我想要发生的是,当您 post“!记录”时,我希望 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
目前,当您 post "!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 的文件中添加另一个字符串。我将如何删除一个条目而不是添加一个条目?对不起,如果这里有点太多,我想把它们全部压缩成一个post,因为它们都是相关的。
我已经做了好几个小时的研究,研究了其他 Whosebug 问题,研究了 Discord.js,以及 An Idiot's Guide YouTube 教程,但没有成功。
如有任何帮助,我们将不胜感激。
如果您查看 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));
关于文件问题,您能否详细说明文件的格式以及命令是什么样的?
您没有正确使用 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);