简体   繁体   English

在 discord.js v13 中为帮助命令制作嵌入页面时出错

[英]Error when making embed pages for help command in discord.js v13

So i was coding a help command for my discord bot but I can't seem to make embed pages with it.所以我正在为我的不和谐机器人编写一个帮助命令,但我似乎无法用它制作嵌入页面。 I want there to be 4 buttons - First Page Button, Last Page Button, Previous Page and Next Page.我希望有 4 个按钮 - 第一页按钮、最后一页按钮、上一页和下一页。 I was able to make a code that sends the embed message with the buttons that have the emojis, but the buttons don't seem to work.我能够制作一个代码,使用带有表情符号的按钮发送嵌入消息,但这些按钮似乎不起作用。 Below is my code :下面是我的代码:

const { 
    MessageActionRow, 
    MessageButton, 
    MessageEmbed 
} = require('discord.js');

async function help(msg, serverprefix, client) {

    // -------------------------

    const embed1 = new MessageEmbed()
        .setColor('#0099ff')
        .setTitle('Help')
        .setDescription('This is the help page 1!')
        .setFooter('Page 1/3')

    const embed2 = new MessageEmbed()
        .setColor('#0099ff')
        .setTitle('Help')
        .setDescription('This is the help page 2!')
        .setFooter('Page 2/3')

    const embed3 = new MessageEmbed()
        .setColor('#0099ff')
        .setTitle('Help')
        .setDescription('This is the help page 3!')
        .setFooter('Page 3/3')
    
    // -------------------------

    const row = new MessageActionRow()
        .addComponents(
            new MessageButton()
                .setCustomId('first')
                .setStyle('PRIMARY')
                .setEmoji('955425801904455700')
        )

        .addComponents(
            new MessageButton()
                .setCustomId('prev')
                .setStyle('PRIMARY')
                .setEmoji('955425801883508756')
        )

        .addComponents(
            new MessageButton()
                .setCustomId('next')
                .setStyle('PRIMARY')
                .setEmoji('955425802151927858')
        )

        .addComponents(
            new MessageButton()
                .setCustomId('last')
                .setStyle('PRIMARY')
                .setEmoji('955425801833173044')
        )
    
    // -------------------------
    
    msg.channel.send({
        embeds: [embed1],
        components: [row]
    })

    // -------------------------

    const filter = i => i.user.id === msg.author.id;
    const collector = msg.channel.createMessageComponentCollector({ filter, idle: 20000 });

    // -------------------------

    collector.on('collect', async (i) => {
        if (i.customId == 'first') {
            if (i.deferred == true || i.replied == true) {
                await i.editReply({
                    embeds: [embed1],
                    components: [row]
                })
            }
            
            await i.update({
                embeds: [embed1],
                components: [row]
            })
        } else if (i.customId == 'prev') {
            if (i.message.embeds[0].footer.text == 'Page 1/3') {
                if (i.deferred == true || i.replied == true) {
                    await i.editReply({
                        embeds: [embed3],
                        components: [row]
                    })
                }
                
                await i.update({
                    embeds: [embed3],
                    components: [row]
                })
            }

            if (i.message.embeds[0].footer.text == 'Page 2/3') {
                if (i.deferred == true || i.replied == true) {
                    await i.editReply({
                        embeds: [embed1],
                        components: [row]
                    })
                }
                
                await i.update({
                    embeds: [embed1],
                    components: [row]
                })
            }

            if (i.message.embeds[0].footer.text == 'Page 3/3') {
                if (i.deferred == true || i.replied == true) {
                    await i.editReply({
                        embeds: [embed2],
                        components: [row]
                    })
                }
                
                await i.update({
                    embeds: [embed2],
                    components: [row]
                })
            }
        } else if (i.customId == 'next') {
            if (i.message.embeds[0].footer.text == 'Page 1/3') {
                if (i.deferred == true || i.replied == true) {
                    await i.editReply({
                        embeds: [embed2],
                        components: [row]
                    })
                }
                
                await i.update({
                    embeds: [embed2],
                    components: [row]
                })
            }

            if (i.message.embeds[0].footer.text == 'Page 2/3') {
                if (i.deferred == true || i.replied == true) {
                    await i.editReply({
                        embeds: [embed3],
                        components: [row]
                    })
                }
                
                await i.update({
                    embeds: [embed3],
                    components: [row]
                })
            }

            if (i.message.embeds[0].footer.text == 'Page 3/3') {
                if (i.deferred == true || i.replied == true) {
                    await i.editReply({
                        embeds: [embed1],
                        components: [row]
                    })
                }
                
                await i.update({
                    embeds: [embed1],
                    components: [row]
                })
            }
        } else if (i.customId == 'last') {
            if (i.deferred == true || i.replied == true) {
                await i.editReply({
                    embeds: [embed3],
                    components: [row]
                })
            }
            
            await i.update({
                embeds: [embed3],
                components: [row]
            })
        }
    });

    // -------------------------

}

module.exports = {
    name: 'help',
    description: 'Help command for the bot',
    execute(msg, serverprefix, client) {
        help(msg, serverprefix, client);
    }
}

This code gives this error :这段代码给出了这个错误:

错误

I have tried a few solutions like checking with an if statement if it is already replied / deferred then edit, and without if, but it's the same.我尝试了一些解决方案,例如使用 if 语句检查它是否已经回复/延迟然后编辑,而没有 if,但它是相同的。 I also copy-pasted this code from Discord Guide :我还从Discord Guide复制粘贴了这段代码:

await i.deferUpdate();
await wait(4000);
await i.editReply({ content: 'A button was clicked!', components: [row] });

and I have tried many more but none of them seem to work.我已经尝试了更多,但它们似乎都不起作用。 I also know there are npm packages for this kind of pagination which make it very easy but I want to do it with discord.js only.我也知道有用于这种分页的 npm 包,这使得它非常容易,但我只想用 discord.js 来做。 I am using discord.js v13 and I'm on Mac.我正在使用 discord.js v13,我在 Mac 上。 Any help will be appreciated.任何帮助将不胜感激。

Do you really want so many pages for your help command?你真的想要这么多页来为你的帮助命令吗? People arn't gonna read that!人们不会读它的! I use this as help command!我用它作为帮助命令! goodluck.祝你好运。
this is the index:这是索引:

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.name, command);
}

and this the tickethelp:这是票务帮助:

const { prefix } = require('../config.json');
const { MessageEmbed, Collection, Permissions } = require('discord.js');

module.exports = {
    name: 'tickethelp',
    description: 'List all of all the commands or info about a specific command. This includes ticket commands.',
    aliases: ['thelp'],
    admin: true,
    execute(message, args, client) {
        var commandEmbedDesc = '';
        const { commands } = message.client;

        if (!message.member.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)) return message.channel.send("You don't have the permissions to execute this command.")

        if (!args.length) {

            const helpDesc = commands.map(command => '**' + command.name + '**').join('\n');

            const embed = new MessageEmbed()
            .setTitle('Here\'s a list of all my ticket commands:')
            .setDescription(helpDesc)
            .setColor('RANDOM')
            .setFooter({text: '\nYou can send ${prefix}help [command name] to get info on a specific command!'});
            return message.channel.send({embeds: [embed]});

        }


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

        commandEmbedDesc += `**Name:** ${command.name}\n`;

        if (command.aliases) commandEmbedDesc += `**Aliases:** ${command.aliases.join(', ')}\n`;
        if (command.description) commandEmbedDesc += `**Description:** ${command.description}\n`;
        if (command.usage) commandEmbedDesc += `**Usage:** ${prefix}${command.name} ${command.usage}\n`;
        if (command.options) {

            commandEmbedDesc += `\n*Options:*\n`;

            command.options.forEach(item => {
                commandEmbedDesc += `${item.name}: ${item.description}\n`;
                commandEmbedDesc += `Input type: ${item.type}\nRequired: ${item.required}\n`;

                if (item.choices) {
                    commandEmbedDesc += `**Choices:**\n`;

                    item.choices.forEach(option => {
                        commandEmbedDesc += `**${option.value}:** ${option.name}\n`;
                    });
                }

                commandEmbedDesc += `\n`;

            });
        }

        const embed = new MessageEmbed()
            .setTitle(`Here\'s a short explanation of: ${command.name}`)
            .setDescription(commandEmbedDesc)
            .setColor('RANDOM')
            .setFooter({text: '\nYou can send ${prefix}help [command name] to get info on a specific command!'});
        message.reply({ embeds: [embed]});

        
        
    },
};

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

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