【问题标题】:How do you make a discord bot resend a image you just send discord.js你如何让一个不和谐的机器人重新发送你刚刚发送的图像 discord.js
【发布时间】:2021-03-04 12:16:21
【问题描述】:

我正在制作一个具有名为“Discord Funniest Memes”或简称 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')

【问题讨论】:

  • 您收到的错误信息是什么?请始终包含它。
  • 我完全忘记了反正我添加了它。感谢询问

标签: javascript image discord discord.js


【解决方案1】:

在调用send() 时,您的meme 似乎是空的,

UnhandledPromiseRejectionWarning 发生在您没有正确捕获由 Promise 抛出(拒绝)的错误时。除非你 await 或在 promise 上调用 .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);
    });

【讨论】:

    【解决方案2】:

    要将图像发送到频道,您需要重新格式化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 也会返回一个承诺,因此您需要确保自己也在处理错误。

    你会是这样的:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 2020-12-05
      • 2020-10-01
      • 2018-05-26
      • 2021-09-17
      • 2021-06-04
      相关资源
      最近更新 更多