【问题标题】:Discord API error : cannot send an empty messageDiscord API 错误:无法发送空消息
【发布时间】:2021-09-02 05:27:58
【问题描述】:

我正在关注有关如何编写不和谐机器人的 YouTube 教程,但在尝试发送嵌入时遇到了臭名昭著的错误“无法发送空消息”。这是我的代码:

main.js:

const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '!';
const fs = require('fs');
 
client.commands = new Discord.Collection();
 
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Picobot is ready for use!');
});
 
client.on('message', message =>{
    if (!message.content.startsWith(prefix) || message.author.bot) return;
 
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
 
    if (command === 'ping') {
        client.commands.get('ping').execute(message, args);
    } else if(command === 'pong') {
        client.commands.get('pong').execute(message, args);
    } else if(command === 'permissions') {
        client.commands.get('permissions').execute(message, args);
    }

    if (command === 'embed') {
        client.commands.get('command').execute(message, args, Discord);
    }
});

embed.js:

module.exports = {
    name: 'embed',
    description: 'Embeds',
    execute(message, args, Discord) {
        const newEmbed = new Discord.MessageEmbed()
        .setColor('#304281')
        .setTitle('Rules')
        .setURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
        .setDescription('This is a test embed')
        .addFields(
            {name: 'Rule 1', value: 'Placeholder 1'},
            {name: 'Rule 2', value: 'Placeholder 2'},
            {name: 'Rule 3', value: 'Placeholder 3'},
        )
        .setImage('https://images.radio-canada.ca/q_auto,w_960/v1/ici-info/16x9/rick-astley-videoclip-never-gonna-give-you-up.png');
        message.channel.send({ newEmbed:newEmbed });
    }
}

我看到许多其他人遇到此错误,到目前为止我找到并尝试的唯一解决方案是将message.channel.send({ newEmbed:newEmbed }) 更改为message.channel.send(newEmbed),但仍然会弹出相同的错误。我在 2021 年还没有看到任何关于这个错误的问题得到解答,所以我想我会在这里拍摄,提前谢谢。

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    从 v12 过渡到 v13,我们遇到了另一个对某些用户来说毫无意义的令人厌烦的变化

    // message.channel.send(newEmbed) does not work anymore
    message.channel.send({
        embeds: [newEmbed]
    });
    // The above is the way to go!
    

    另一个变化:消息事件监听器:

    client.on("message", () => {})
    

    已折旧并已更改为messageCreate

    【讨论】:

    • 非常感谢您的详细解答!从现在开始,我一定会检查文档。
    【解决方案2】:

    在 v13 中,发送嵌入的结构如下:

    message.channel.send({ embeds: [newEmbed] });
    

    Embeds - Discord.JS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-26
      • 2019-03-05
      • 2021-05-29
      • 2021-03-02
      • 2020-03-20
      • 2021-12-28
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多