JS Discord 按钮错误:“无法发送空消息”

时间:2021-06-25 10:43:15

标签: javascript discord discord.js discord-buttons

嗨,我正在制作一个基于 Javascript 的 Discord 机器人,我真的很想实现新的 Discord 按钮功能,问题是,即使使用文档示例,我也一次又一次地遇到相同的错误:

(node:6184) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message

这是代码(我使用 module.exports,每个命令都有单独的文件):

const { MessageButton } = require('discord-buttons')

module.exports = {
    name: 'invite',
    descritipion: 'test command',
    async execute(client, message, args) {
        let button = new MessageButton()
            .setStyle('url')
            .setURL('https://npmjs.com/discord-buttons')
            .setLabel('Click me !')

        await message.channel.send('Hey, I am powered by npm discord-buttons : https://npmjs.com/discord-buttons');
        await message.channel.send(button);
    }
}

1 个答案:

答案 0 :(得分:2)

您不能只发送一个按钮。相反,尝试将按钮添加到上一条消息,以便它们都在一条消息中发送,如下所示
The official documentation for this is here

// Replace these
const { MessageButton } = require('discord-buttons')
await message.channel.send('Hey, I am powered by npm discord-buttons : https://npmjs.com/discord-buttons');
await message.channel.send(button);

// With these
const { MessageButton, MessageActionRow } = require('discord-buttons')
await message.channel.send('Hey, I am powered by npm discord-buttons : https://npmjs.com/discord-buttons', {
    component: new MessageActionRow().addComponent(button)
});

另外,您是否忘记初始化 DiscordButtons?在初始化 Discord 机器人时执行此操作

// Where you initialized your discord bot
const bot = new Discord.Client()
// Add this line
require("discord-button")(bot)