【问题标题】:problem trying to send a client.reply as an embed Message with discord.js尝试使用 discord.js 将 client.reply 作为嵌入消息发送时出现问题
【发布时间】:2022-01-06 03:40:14
【问题描述】:

实际上,我正在工作和学习 discord.js 和 node.js 来制作机器人,但我有一个简单的问题,我不知道为什么嵌入消息不起作用,我尝试使用文档其他开发人员的示例和代码,但在所有情况下,当我尝试将消息发送到频道时,使用 client.reply(embed) 都会向我抛出一个错误,告诉我可以发送空消息。

我正在使用最新版本的 discord.js (v13.3.1),并且我正在使用基本文档事件和命令处理程序(如果我不尝试发送嵌入,则可以完美运行)。

这是我的 index.js 和 help.js 文件:

//This is my index.js i don´t have problem with this but i include it if there are an issue related with this topic.

const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const { token } = require('./config.json');

client.commands = new 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.data.name, command);
}

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        return interaction.reply({ content: 'Parece que ha ocurrido algun problema con el comando.', ephemeral: true });
    }
});

const eventFiles = fs.readdirSync('./Events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const event = require(`./Events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

client.login(token);

现在是 help.js ,这将是一个命令,但我不想只使用 SlashCommands 对命令进行编码,因为我正在尝试将其创建为一个事件:

//This is the help command using an event
const { clientId } = require("../config.json");
const { MessageEmbed } = require("discord.js");
//testx is going to store the embed message
const testx = new MessageEmbed().setTitle('Test').setDescription('Test');
module.exports = {
    name: 'messageCreate',
    execute(client) {
        //This condition determines if isn´t a message of the bot, and if the written command is !!help
        if (client.author.id !== clientId && client.content === '!!help') {
            //if the condition is true, send the embed message
            //client.reply(testx); //the problem is here.
            console.log(testx);
        }
    },
};

这是我从终端得到的错误:

DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (C:\Users\ //...and more info of my dirs

如果我在控制台中打印 testx const,我可以看到我在 const 中设置的两个值填充了文本“Test”,我不知道为什么不起作用或我需要采取什么措施工作。

谢谢,感谢任何帮助。

【问题讨论】:

标签: javascript node.js discord.js bots


【解决方案1】:

在 V13 中,嵌入必须以不同的方式发送。

client.reply({embeds: [testx]});

我还建议在您的命令文件中编辑 clientinteraction,因为这可能会产生误导,或者实际上传递客户端和交互,因为您很可能在某些时候需要访问客户端。

另外,您似乎将interactions 与消息混淆了。

交互没有author 属性,您必须使用usermember

它们也没有content 属性,要获得参数,您必须使用interaction.options

据我所知,您实际上并没有部署您的命令。看起来您已将 V12 和 V13 命令处理程序教程合并在一起。我建议再次阅读 this 教程,并确保您在 V13 中正确地进行操作。

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 2021-08-22
    • 2020-12-05
    • 2021-08-25
    • 2021-06-05
    • 2022-09-28
    • 2012-09-03
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多