如何在discord.js中制作嵌入页面 [英] How do you make embed pages in discord.js

查看:119
本文介绍了如何在discord.js中制作嵌入页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 message.channel.send(bot.guilds.cache.map(g=> "*Name:* **"+g.name +'** *ID:* **'+g.id+"** *Owner:* **"+g.owner.user.tag+"**"))

我有这段代码可以发送所有行会,名称ID和所有者名称,但是我该如何做才能显示fisrt 10在一个嵌入页面上,然后一个反应箭头会将您带到下一个显示其他行会的页面,然后向后反应会将您带回到一个页面

I have this code to send all guilds, the name id and owner name, however how can I make this so that the fisrt 10 are displayed on an embed page, then a reaction arrow takes you to the next page showing other guilds and then the back reaction takes you back a page

推荐答案

尝试以下代码:

const guilds = bot.guilds.cache.array()

/**
 * Creates an embed with guilds starting from an index.
 * @param {number} start The index to start from.
 */
const generateEmbed = start => {
  const current = guilds.slice(start, start + 10)

  // you can of course customise this embed however you want
  const embed = new MessageEmbed()
    .setTitle(`Showing guilds ${start + 1}-${start + current.length} out of ${guilds.length}`)
  current.forEach(g => embed.addField(g.name, `**ID:** ${g.id}
**Owner:** ${g.owner.user.tag}`))
  return embed
}

// edit: you can store the message author like this:
const author = message.author

// send the embed with the first 10 guilds
message.channel.send(generateEmbed(0)).then(message => {
  // exit if there is only one page of guilds (no need for all of this)
  if (guilds.length <= 10) return
  // react with the right arrow (so that the user can click it) (left arrow isn't needed because it is the start)
  message.react('➡️')
  const collector = message.createReactionCollector(
    // only collect left and right arrow reactions from the message author
    (reaction, user) => ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === author.id,
    // time out after a minute
    {time: 60000}
  )

  let currentIndex = 0
  collector.on('collect', reaction => {
    // remove the existing reactions
    message.reactions.removeAll().then(async () => {
      // increase/decrease index
      reaction.emoji.name === '⬅️' ? currentIndex -= 10 : currentIndex += 10
      // edit message with new embed
      message.edit(generateEmbed(currentIndex))
      // react with left arrow if it isn't the start (await is used so that the right arrow always goes after the left)
      if (currentIndex !== 0) await message.react('⬅️')
      // react with right arrow if it isn't the end
      if (currentIndex + 10 < guilds.length) message.react('➡️')
    })
  })
})

这篇关于如何在discord.js中制作嵌入页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆