Discord.js嵌入错误无法发送空消息

时间:2020-06-19 08:41:26

标签: javascript discord discord.js

所以我正在尝试使用嵌入中显示的命令列表来创建一个帮助命令。我的代码有点用,但是它抛出错误“ 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 });
 },
};

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

由于msg.channel.send(data)变量是一个数组,因此您可以尝试用msg.channel.send(data.join('\n'))替换data

我不明白为什么不能发送数组。