【发布时间】: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