discord.js - 我正在 discord.js 中制作自定义动态嵌入帮助命令,如何嵌入命令信息?
问题描述
该代码有效,它在我执行 -help 时嵌入,但如果我执行 -help {command},它会显示未嵌入的文本。我将如何嵌入名称、别名、描述和冷却时间(动态)。
这是我尝试过的。
module.exports = {
name: 'help',
aliases: ['h'],
description: 'A help Command',
cooldown: 5,
execute(message, args, cmd, client, Discord) {
const data = [];
const { commands } = message.client;
const prefix = process.env.PREFIX;
if (!args.length) {
const title = 'Here\'s a list of all my commands:';
const description = data.push(commands.map(command => command.name).join(', '));
const footer = `You can send ${prefix}help [command name] to get info on a specific command!`;
const helpEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
.setTitle(title)
.setDescription(data)
.setTimestamp()
.setFooter(footer);
return message.author.send(helpEmbed)
.then(() => {
if (message.channel.type === 'dm') return;
message.reply('I\'ve sent you a DM with all my commands!');
})
.catch(error => {
console.error(`Could not send help DM to ${message.author.tag}.\n`, error);
message.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 message.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}`);
if (command.cooldown) data.push(`**Cooldown:** ${command.cooldown} seconds`);
message.channel.send(data, { split: true });
}
}
解决方案
目前,您只是发送一个字符串数组,而不是一个嵌入对象。要创建嵌入,您可以执行以下操作之一:
使用MessageEmbed
构造函数:
const {MessageEmbed} = require("discord.js");
const embed = new MessageEmbed();
// dynamically add data using MessageEmbed methods
if (someCondition) embed.addField("name", "value");
if (anotherCondition) embed.setDescription("description");
使用对象创建嵌入:
const embed = {
description: "",
fields: []
};
// dynamically add data using basic JavaScript methods
if (someCondition) embed.fields.push({name: "name", value: "value"});
if (anotherCondition) embed.description = "description";
检查文档以查看您可以在该类中使用的方法MessageEmbed
。
另一个有用的资源是这个Embed Visualizer。
推荐阅读
- c# - 一键创建并写入txt文件
- python - to_numeric() 在errors = 'coerce' 时返回float 值,即使当列表包含'nan' 值时downcast = 'integer'
- c++ - 如何在 Qt QGraphicsView 中全宽显示图像?
- php - 如何用 PHP 解析 xml 站点地图?
- python - 如何限制从 csv 获取的数据?
- python - 有没有一种简单的方法来检查查询集中的所有属性是否具有相同的值?
- java - 仅针对特定 try catch 的 Java 异常
- wordpress - 计算自定义分类中多个子术语下的所有帖子
- php - 如何使用 Woocommerce API 为产品添加变体
- html - 为什么元素 div 也会改变它的位置