簡體   English   中英

Discord.js 幫助命令編譯命令列表並嵌入縮略圖

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

我有一個非常簡單的 Discord 機器人,它通過鏈接 Imgur/Tenor html 來按命令發布圖像/gif。

命令存儲在單獨的.js文件中./commands/

我想創建一個幫助命令來收集文件夾中的所有當前命令,並嵌入命令名稱,在其下方帶有執行的命令,以便創建圖像/gif 的縮略圖,但我主要是設法遵循Discord.js 指南所以我對此非常陌生。 任何人都可以建議一些代碼來幫助我入門嗎? 如何根據現有命令數組填充嵌入字段?

下面的機器人示例:

這些命令通過以下方式導入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);
    }

並通過以下方式執行:

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!');
            }
    });

命令./commands/test.js一個例子是:

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

您可以使用.forEach()循環。
例如:

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