你如何让不和谐机器人重新发送你刚刚发送的图像 discord.js

时间:2021-03-03 01:42:42

标签: javascript image discord discord.js

我正在制作一个机器人,该机器人具有称为“Discord Funniest Memes”或简称 DFM 的功能。该功能的重点是人们可以使用命令“DFM.submit 'IMAGEFILE'”在#memes 中提交模因,然后如果人们执行命令“DFM”,它将发送当天/每周投票最高的一些模因,但是我遇到的问题是机器人没有发送图像,而是给我一条错误消息。

编辑:我忘记了错误消息“(节点:3532)UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息”

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')

2 个答案:

答案 0 :(得分:0)

看起来您的 meme 在调用 send() 时是空的,

UnhandledPromiseRejectionWarning 发生在您没有正确捕获由 Promise 抛出(拒绝)的错误时。除非您对 Promise 进行 await 或调用 .then(),否则剩余的代码将继续执行,而无需等待 send() 方法的结果。如果 send() 在没有等待的情况下在后台运行时出现错误,那么您将获得 UnhandledPromiseRejectionWarning

discord.js 的文档来看,message.channel.send 返回一个 promise。看看 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);
    });

答案 1 :(得分:0)

为了向频道发送图像,您需要重新格式化 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);
    }
}
相关问题