我正在制作一个机器人,它有一个被称为“不和谐最有趣的模因”或简称DFM的功能。这个功能的要点是,人们可以使用命令"DFM.submit 'IMAGEFILE'“提交#memes中的表情包,然后如果人们执行命令"DFM”,它将发送一些当天/每周投票率最高的表情包,但我遇到的问题是机器人没有发送图像,而是给了我一个错误消息。
编辑:我忘记了错误信息"(node:3532) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message“
const Discord = require('discord.js');
const bot = new Discord.Client();
var meme
const DFM = ['some meme link.png', 'another meme link.png']
var randomDFM = DFM[Math.floor(Math.random() * DFM.length)];
if(message.content.startsWith("DFM.submit")){
let meme = message.content.split(" ");
meme.shift();
meme = meme.join(" ");
message.channel.send(meme)
message.channel.send('Your post was submitted to "Discords Funniest Memes"')
DFM.unshift(meme)
}
if(message.content === "DFM"){
message.channel.send(randomDFM)
randomDFM = DFM[Math.floor(Math.random() * DFM.length)];
}
bot.login('no token for you')发布于 2021-03-03 11:10:27
在调用send()时,您的meme似乎是空的。
当您没有正确地捕获由promise抛出(拒绝)的错误时,就会发生UnhandledPromiseRejectionWarning。除非您对promise执行await或调用.then(),否则剩余的代码将继续执行,而不等待send()方法的结果。如果在没有等待的情况下,send()在后台运行时出现错误,那么就会得到UnhandledPromiseRejectionWarning。
从discord.js的文档看,message.channel.send返回了一个承诺。看看at the docs以及他们是如何使用then()和catch()的。
你可能想要做的事情是这样的。这是未经测试的,但应该会给您一个想法。
message.channel.send(meme)
.then(() => {
return message.channel.send('Your post was submitted to ...');
})
.catch(error => {
// handle your error here
console.error(error);
});发布于 2021-03-04 12:16:22
为了将图像发送到通道,您需要重新格式化message.channel.send命令。
它应该看起来像这样:
if(message.content === "DFM"){
// randomDFM should contain the url to the file you are trying to send
message.channel.send("Your Bot's Message", {files: [randomDFM]})
randomDFM = DFM[Math.floor(Math.random() * DFM.length)]
}message.channel.send也会返回一个promise,所以你需要确保你也在处理错误。
你会是这样的:
if(message.content === "DFM"){
// randomDFM should contain the url to the file you are trying to send
message.channel.send("Your Bot's Message", {files: [randomDFM]})
.then(() => {
randomDFM = DFM[Math.floor(Math.random() * DFM.length)]
})
.catch((err) => {
console.log(err);
}
}https://stackoverflow.com/questions/66449557
复制相似问题