简体   繁体   English

Discord.js 帮助命令编译命令列表并嵌入缩略图

[英]Discord.js help command to compile list of commands and embed with thumbnails

I have a very simple Discord bot that posts images/gifs on command by linking Imgur/Tenor htmls.我有一个非常简单的 Discord 机器人,它通过链接 Imgur/Tenor html 来按命令发布图像/gif。

The commands are stored in individual .js files ./commands/命令存储在单独的.js文件中./commands/

I would like to create a help command that collects all current commands in the folder, and embeds the command name, with the executed command beneath it so that a thumbnail of the image/gif is created, but I mostly just about managed to follow the Discord.js guide so I'm very very new to this.我想创建一个帮助命令来收集文件夹中的所有当前命令,并嵌入命令名称,在其下方带有执行的命令,以便创建图像/gif 的缩略图,但我主要是设法遵循Discord.js 指南所以我对此非常陌生。 Can anyone suggest some code to help me get started?任何人都可以建议一些代码来帮助我入门吗? How can I populate embed fields based on an array of existing commands?如何根据现有命令数组填充嵌入字段?

Example of bot below:下面的机器人示例:

The commands are imported in index.js via:这些命令通过以下方式导入index.js

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

And executed via:并通过以下方式执行:

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    
        const args = message.content.slice(prefix.length).split(/ +/);
        const commandName = args.shift().toLowerCase();
    
        if (!client.commands.has(commandName)) return;
        const command = client.commands.get(commandName); 
    
        try {
                    command.execute(message, args);
            } catch (error) {
                    console.error(error);
                    message.reply('Something bad happened!');
            }
    });

An example of a command ./commands/test.js is:命令./commands/test.js一个例子是:

module.exports = {
    name: 'test',
    description: 'A test',
    execute(message, args) {
        message.channel.send('This is a test.');
    }
}

You can use a .forEach() loop.您可以使用.forEach()循环。
For example:例如:

module.exports = {
    name: 'help',
    description: 'View a full list of commands',
    execute(message, client) {
        const Discord = require('discord.js');
        const embed = Discord.MessageEmbed();

        client.commands.forEach(command => {
            embed.addField(`${command.name}`, `${command.description}`, false);
        }
        message.channel.send(embed);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM