DiscordAPIError:无法发送空消息。但是我可以记录我试图发送到控制台的东西,发送的数据来自 MongoDB

Posted

技术标签:

【中文标题】DiscordAPIError:无法发送空消息。但是我可以记录我试图发送到控制台的东西,发送的数据来自 MongoDB【英文标题】:DiscordAPIError: Cannot send an empty message. However I can log the thing I am trying to send to console, the data being sent is from MongoDB 【发布时间】:2021-09-19 08:55:46 【问题描述】:
Code:
const ModSchema = require('../models/ModSchema');

module.exports = 
    name: 'warns',
    description: "lists a members warns.",
    execute(message, args, client, Discord)
        const UserIDsearch = message.mentions.members.first().id;
        ModSchema.findOne( 'UserID': UserIDsearch , function (err, Punishments) 
            if (err) return console.log(err);
            let data = Punishments;
            console.log(data);
            message.channel.send(data);
        )
    

Error Message:
(node:7397) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (/Users/mynamegoeshere/ESENTRIX CLONE FILE/Esentrix-Bot-5/node_modules/discord.js/src/rest/RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RequestHandler.push (/Users/mynamegoeshere/ESENTRIX CLONE FILE/Esentrix-Bot-5/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7397) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7397) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What I want to send in Discord: 
"Punishments": [
    
      "PunishType": "Mute",
      "Moderator": "Poetic",
      "Reason": "test",
      "Time": "10000 ms"
    

这是用于从 MongoDB 数据库查询数据的代码,数据存储为 JSON 文件,但我认为如果将其设置为 const/let,它会被字符串化。请让我知道如何发送数据,因为我似乎找不到问题。请记住,我尝试发送的文本可以使用 console.log(data); 记录到控制台,但是在使用 message.channel.send(data); 时出现上述错误;如果您需要更多信息,请告诉我。

【问题讨论】:

【参考方案1】:

也许可以试试

module.exports = 
    name: 'warns',
    description: "lists a members warns.",
    execute(message, args, client, Discord)
        const UserIDsearch = message.mentions.members.first().id;
        ModSchema.findOne( 'UserID': UserIDsearch , function (err, Punishments) 
            if (err) return console.log(err);
            if(Punishments)
                let data = Punishments;
                console.log(data);
                message.channel.send(data);
            
        )
    

或者只是为了确保它的字符串化,试试

JSON.stringify(data)

【讨论】:

将答案标记为正确,因为它有效

以上是关于DiscordAPIError:无法发送空消息。但是我可以记录我试图发送到控制台的东西,发送的数据来自 MongoDB的主要内容,如果未能解决你的问题,请参考以下文章

不和谐机器人的NodeJS错误:UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息

DiscordAPIError:无法发送空消息。但是我可以记录我试图发送到控制台的东西,发送的数据来自 MongoDB

无法发送空消息 - Discord.JS v13

尝试发送嵌入时抛出错误

我无法向用户发送任何消息

DiscordAPIError:缺少权限 - 机器人崩溃而不是给出错误消息