Discord.js ReactionCollector帮助菜单-重置表情符号

时间:2019-03-09 19:13:32

标签: javascript bots discord discord.js

我正在使用以下代码为Discord机器人创建帮助菜单。

let pages = ['Page one!', 'Second page', 'Third page'];
let page = 1; 

    const embed = new Discord.RichEmbed() // Define a new embed
    .setColor(0xffffff) // Set the color
    .setFooter(`Page ${page} of ${pages.length}`)
    .setDescription(pages[page-1])

    message.channel.send(embed).then(msg => {

    msg.react('⬅').then( r => {
        msg.react('➡')

        // Filters
        const backwardsFilter = (reaction, user) => reaction.emoji.name === '⬅' && user.id === message.author.id;
        const forwardsFilter = (reaction, user) => reaction.emoji.name === '➡' && user.id === message.author.id;

        const backwards = msg.createReactionCollector(backwardsFilter, {timer: 6000});
        const forwards = msg.createReactionCollector(forwardsFilter, {timer: 6000});

        backwards.on('collect', r => {
            if (page === 1) return;
            page--;
            embed.setDescription(pages[page-1]);
            embed.setFooter(`Page ${page} of ${pages.length}`);
            msg.edit(embed)
        })

        forwards.on('collect', r => {
            if (page === pages.length) return;
            page++;
            embed.setDescription(pages[page-1]);
            embed.setFooter(`Page ${page} of ${pages.length}`);
            msg.edit(embed)
        })
    })
})

}

除了我必须在表情符号上单击两次以移至下一页/上一页之外,它的工作原理是正确的。我相信我需要删除表情符号,然后以默认设置重新加载它们。

有人知道这样做的最好方法吗?还是我需要做些其他事情?

2 个答案:

答案 0 :(得分:2)

以防万一有人偶然发现此问题并尝试在当前版本的DJS上使用此代码并出错,这是如何在当前版本的Discord.JS(v12.2.0)中编写整个函数的方法:

NB:假设您已经传递了客户端,消息和可选的任何参数

编辑:修改后,如果用户选择后退按钮但已经在第一页上,或者前进按钮但已经在最后一页上,则机器人将删除响应

const Discord = require('discord.js')
let pages = ['Page one!', 'Second page', 'Third page']
let page = 1 

const embed = new Discord.MessageEmbed() // Define a new embed
.setColor(0xffffff) // Set the color
.setFooter(`Page ${page} of ${pages.length}`)
.setDescription(pages[page-1])

message.channel.send({embed}).then(msg => {
  msg.react('⬅').then( r => {
    msg.react('➡')

    // Filters
    const backwardsFilter = (reaction, user) => reaction.emoji.name === '⬅' && user.id === message.author.id
    const forwardsFilter = (reaction, user) => reaction.emoji.name === '➡' && user.id === message.author.id

    const backwards = msg.createReactionCollector(backwardsFilter, {timer: 6000})
    const forwards = msg.createReactionCollector(forwardsFilter, {timer: 6000})

    backwards.on('collect', (r, u) => {
        if (page === 1) return r.users.remove(r.users.cache.filter(u => u === message.author).first())
        page--
        embed.setDescription(pages[page-1])
        embed.setFooter(`Page ${page} of ${pages.length}`)
        msg.edit(embed)
        r.users.remove(r.users.cache.filter(u => u === message.author).first())
    })

    forwards.on('collect', (r, u) => {
        if (page === pages.length) return r.users.remove(r.users.cache.filter(u => u === message.author).first())
        page++
        embed.setDescription(pages[page-1])
        embed.setFooter(`Page ${page} of ${pages.length}`)
        msg.edit(embed)
        r.users.remove(r.users.cache.filter(u => u === message.author).first())
    })
  })
})

希望这对任何人都可以帮助:)

答案 1 :(得分:0)

https://discord.js.org/#/docs/main/stable/class/MessageReaction?scrollTo=remove

r.remove(r.users.filter(u => u === message.author).first());

在两个收集器内部都应该做。

编辑

如果您希望每个用户都可以使用此菜单,则最好改用此过滤器:

.filter(u => !u.bot)