Discord.js智能帮助嵌入



我正在尝试制作一个帮助嵌入,为命令文件文件夹中的每个文件创建一个新字段。它一直显示一个错误,我不知道如何修复它。

这是我的代码:

const Discord = require('discord.js');
const pagination = require('discord.js-pagination')
const { prefix } = require('../config.json').prefix;
module.exports = {
name: "help", //.ping
description: "Help command.",
use: `${prefix}help`,
async run (bot, message, args) {
const embed1 = new Discord.MessageEmbed()
.setTitle('Help')
.setDescription(`Prefix: ${prefix}`)
for (const thing of require('../commands')) {
.addField();
}
}
}

将.addField((更改为embed1.addField。还可以尝试添加一些参数来添加add字段,例如字段内部的文本和字段的标题,如addField("测试"、"测试"(。IDK你的错误是什么,所以这些是可能的修复。

如果其他一切都是正确的,那么应该这样做:

async run (bot, message, args) {
const embed1 = new Discord.MessageEmbed()
.setTitle('Help')
.setDescription(`Prefix: ${prefix}`);
for (const thing of require('../commands')) {
embed1.addField();
}
}

您似乎在使用discordjs.guide网站上提供的设置。这意味着,在client.commandsbot.commands上已经有一组可用的命令。它保存了您需要的所有数据。

集合有一个返回新数组的map()方法。您可以使用它来迭代commands并返回一个新的字段对象(一个具有namevalue属性的对象(。一旦有了这个数组,就可以使用.addFields方法,该方法(与.addField不同(接受字段数组。

这样做的问题是字段的最大数量是25。如果您有超过25个命令,则需要在不同的嵌入中发送它们。您已经导入了discord.js-pagination,它允许您提供一个嵌入数组,然后它可以处理所有困难的工作。但您仍然需要确保字段不超过25个。

为了解决这个问题,您可以将原始数组分块为最多25个项目的新数组。我已经为您编写了一个帮助函数来实现这一点。它返回一个数组数组。

现在,您可以对块进行迭代,为每个块创建一个新的嵌入对象,并设置分页。

查看下面的代码:

const Discord = require('discord.js');
const pagination = require('discord.js-pagination');
const { prefix } = require('../config.json').prefix;
module.exports = {
name: 'help', //.ping
description: 'Help command.',
use: `${prefix}help`,
async run(bot, message, args) {
const MAX_FIELDS = 25;
// iterate over the commands and create field objects
const fields = bot.commands.map((command) => ({
name: 'Command',
value: `${command.name}n${command.description}n${command.use}`,
}));
// if there is less than 25 fields, you can safely send the embed
// in a single message
if (fields.length <= MAX_FIELDS)
return message.channel.send(
new Discord.MessageEmbed()
.setTitle('Help')
.setDescription(`Prefix: ${prefix}`)
.addFields(fields),
);
// if there are more, you need to create chunks w/ max 25 fields
const chunks = chunkify(fields, MAX_FIELDS);
// an array of embeds used by `discord.js-pagination`
const pages = [];
chunks.forEach((chunk) => {
// create a new embed for each 25 fields
pages.push(
new Discord.MessageEmbed()
.setTitle('Help')
.setDescription(`Prefix: ${prefix}`)
.addFields(chunk),
);
});
pagination('some message', pages);
},
};
// helper function to slice arrays
function chunkify(arr, len) {
let chunks = [];
let i = 0;
let n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, (i += len)));
}
return chunks;
}

最新更新