- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不太了解 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,还会发送记录,而不是等待回复。
有人可以概述一下我在这里做错了什么吗?
此外,我希望能够说出一个命令来更新记录,而无需进入机器人的文件并手动执行。
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/
在文档中,它指出 Message 可以有一个 array of Embeds . 如果可以用一条消息发送多个 RichEmbed,这显然提出了一个问题。 发送嵌入的正常方式是使用 MessageOpt
我试图在不和谐中打印获取嵌入消息,但发生了这种情况: TypeError: Discord.RichEmbed is not a constructor 这是我的代码: const Discord =
我得到了这个: ( Node :5496)UnhandledPromiseRejectionWarning:未处理的 promise 拒绝(拒绝ID:2):TypeError:无法读取属性rty 'm
我的控制台上不断出现错误,提示 RichEmbed 字段可能不为空。当我为每个字段定义了值时...以下是代码: if (cmd === `${prefix}suggest`) { // USAGE
我正在使用 discord.js 开发一个 Discord 机器人。使用我的机器人,用户拥有自己的个人资料。所以他们!profile @me然后他们的个人资料就出现了。我想知道是否有一种方法可以让用户
我知道这个问题存在,但即使我调查了另一个问题并且我使用了“sendEmbed”而不是“发送”,但似乎没有任何效果,如果有人可以帮助解决这个错误,我将不胜感激:) 这是我的代码: const Disco
我正在尝试创建类似[this](https://stackoverflow.com/) 的链接,但我似乎无法找到任何可能的答案。 我已经尝试过 markdown 语法(如上所示),但我似乎找不到任何其
我按照其他一些问题制作了一个用于丰富嵌入的 react 收集器(我确实首先尝试使用awaitReaction方法,但我无法应用它)。现在我遇到一个问题,即机器人添加 react 后就会立即进行编辑,而
哎呀!我在输出嵌入时遇到问题。唯一让我困惑的问题是我没有一个没有任何东西的字段,它通过并在 Discord 中发送,然后出错。 当我检测到编辑时,它会做同样的事情,这段代码在我的 bot.on("me
所以基本上我一直在为消息编辑制作日志。该函数的目的是在有人编辑消息时写入 modlog channel 。我还希望机器人在更新前后写消息。 代码如下: bot.on('messageUpdate',
我不太了解 Javascript,所以请多多包涵。 所以,我正在尝试为我的 Discord Bot 发出命令。基本上,我想要发生的是,当您发布“!records”时,我希望 Bot 发送一个 Rich
我是一名优秀的程序员,十分优秀!