【问题标题】:Discord.js Embed error Cannot send an empty messageDiscord.js 嵌入错误无法发送空消息
【发布时间】:2020-10-09 11:40:19
【问题描述】:

所以我正在尝试使用嵌入中显示的命令列表创建一个帮助命令。我的代码有点工作,但它会引发错误“DiscordAPIError:无法发送空消息”,我已经尝试了我所知道的所有内容以及我找到的内容,但我无法修复它。

这是代码

const Discord = require('discord.js');
const { prefix } = require('../config.json');

module.exports = {
  name: 'help',
  description: 'List all of my commands or info about a specific command.',
  aliases: ['commands', 'cmds'],
  usage: '[command name]',
  cooldown: 5,
  execute(msg, args) {

    const data = [];
    const { commands } = msg.client;

    if (!args.length) {

        const helpEmbed = new Discord.MessageEmbed()
            .setColor('YELLOW')
            .setTitle('Here\'s a list of all my commands:')
            .setDescription(commands.map(cmd => cmd.name).join('\n'))
            .setTimestamp()
            .setFooter(`You can send \`${prefix}help [command name]\` to get info on a specific command!`);

        msg.author.send(helpEmbed);

        return msg.author.send(data, { split: true })
            .then(() => {
                if (msg.channel.type === 'dm') return;
                msg.reply('I\'ve sent you a DM with all my commands!');
            })
            .catch(error => {
                console.error(`Could not send help DM to ${msg.author.tag}.\n`, error);
                msg.reply('it seems like I can\'t DM you! Do you have DMs disabled?');
            });
    }

    const name = args[0].toLowerCase();
    const command = commands.get(name) || commands.find(c => c.aliases && c.aliases.includes(name));

    if (!command) {
        return msg.reply('that\'s not a valid command!');
    }

    data.push(`**Name:** ${command.name}`);

    if (command.aliases) data.push(`**Aliases:** ${command.aliases.join(', ')}`);
    if (command.description) data.push(`**Description:** ${command.description}`);
    if (command.usage) data.push(`**Usage:** ${prefix}${command.name} ${command.usage}`);

    data.push(`**Cooldown:** ${command.cooldown || 3} second(s)`);

    msg.channel.send(data, { split: true });
 },
};

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您应该尝试替换此行: msg.channel.send(data, { split: true });

    msg.channel.send(data.join(' '), { split: true }); 因为你的数据变量是一个数组而不是一个字符串

    【讨论】:

    • 可悲的是同样的结果
    • @Soulhax @Soulhax 你在这条线上犯了同样的错误:return msg.author.send(data, { split: true })
    • 我尝试更换两者,但它仍然是一样的。更换两者会使情况变得更糟。当两者都被替换时:imgur.com/a/QJZbGsy 当它是原始代码时:imgur.com/a/8MaVYJD
    • @Soulhax 你是什么意思?
    • 添加了图片链接,方便大家查看
    【解决方案2】:

    问题是错误状态。您正试图在某处发送空消息。

    您可以尝试将msg.channel.send(data) 替换为msg.channel.send(data.join('\n')),因为data 变量是一个数组。

    我不明白为什么发送数组不起作用。

    【讨论】:

    • 发送数组不起作用,因为我想 discord.js 不允许它
    • 遗憾的是还是一样的结果
    • 我认为你的问题不是你试图发送一个数组,而是数组是空的。如果您尝试使用var data 而不是const data 会怎样?
    • @ComputerGeek12 是的,问题是 cons dat = [] 但我不能用 var 替换它。如果我只是将其设置为某个值,则该命令可以正常工作。
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2021-06-25
    • 2020-01-25
    • 2022-01-20
    • 2017-10-31
    • 2021-08-22
    • 2020-02-26
    • 2021-04-26
    相关资源
    最近更新 更多