久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

你如何在 discord.js 中制作嵌入頁面

How do you make embed pages in discord.js(你如何在 discord.js 中制作嵌入頁面)
本文介紹了你如何在 discord.js 中制作嵌入頁面的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述


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

我有這個代碼來發(fā)送所有公會,名稱 id 和所有者名稱,但是我怎樣才能使第 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

推薦答案

Discord.js v13

您可以使用 Discord 相對較新的 按鈕:

import {MessageActionRow, MessageButton, MessageEmbed} from 'discord.js'

// Constants

const backId = 'back'
const forwardId = 'forward'
const backButton = new MessageButton({
  style: 'SECONDARY',
  label: 'Back',
  emoji: '??',
  customId: backId
})
const forwardButton = new MessageButton({
  style: 'SECONDARY',
  label: 'Forward',
  emoji: '??',
  customId: forwardId
})

// Put the following code wherever you want to send the embed pages:

const {author, channel} = message
const guilds = [...client.guilds.cache.values()]

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

  // You can of course customise this embed however you want
  return new MessageEmbed({
    title: `Showing guilds ${start + 1}-${start + current.length} out of ${
      guilds.length
    }`,
    fields: await Promise.all(
      current.map(async guild => ({
        name: guild.name,
        value: `**ID:** ${guild.id}
**Owner:** ${(await guild.fetchOwner()).user.tag}`
      }))
    )
  })
}

// Send the embed with the first 10 guilds
const canFitOnOnePage = guilds.length <= 10
const embedMessage = await channel.send({
  embeds: [await generateEmbed(0)],
  components: canFitOnOnePage
    ? []
    : [new MessageActionRow({components: [forwardButton]})]
})
// Exit if there is only one page of guilds (no need for all of this)
if (canFitOnOnePage) return

// Collect button interactions (when a user clicks a button),
// but only when the button as clicked by the original message author
const collector = embedMessage.createMessageComponentCollector({
  filter: ({user}) => user.id === author.id
})

let currentIndex = 0
collector.on('collect', async interaction => {
  // Increase/decrease index
  interaction.customId === backId ? (currentIndex -= 10) : (currentIndex += 10)
  // Respond to interaction by updating message with new embed
  await interaction.update({
    embeds: [await generateEmbed(currentIndex)],
    components: [
      new MessageActionRow({
        components: [
          // back button if it isn't the start
          ...(currentIndex ? [backButton] : []),
          // forward button if it isn't the end
          ...(currentIndex + 10 < guilds.length ? [forwardButton] : [])
        ]
      })
    ]
  })
})

這是一個預覽(帶有顯示分頁的垃圾字段):

Here's a preview (with rubbish fields to show the pagination):

這是我使用反應發(fā)布的原始版本.此代碼僅適用于 Discord.js v12.

This is the original version I posted using reactions. This code only works for Discord.js v12.

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
  return new MessageEmbed({
    title: `Showing guilds ${start + 1}-${start + current.length} out of ${guilds.length}`,
    fields: current.map(guild => ({
      name: guild.name,
      value: `**ID:** ${guild.id}
**Owner:** ${guild.owner.user.tag}`
    }))
  })
}

const {author, channel} = message.author

// send the embed with the first 10 guilds
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', async reaction => {
    // remove the existing reactions
    await message.reactions.removeAll()
    // increase/decrease index
    reaction.emoji.name === '??' ? currentIndex -= 10 : currentIndex += 10
    // edit message with new embed
    await message.edit(generateEmbed(currentIndex))
    // react with left arrow if it isn't the start
    if (currentIndex !== 0) await message.react('??')
    // react with right arrow if it isn't the end
    if (currentIndex + 10 < guilds.length) await message.react('??')
  })
})

這篇關于你如何在 discord.js 中制作嵌入頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創(chuàng)建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 95国产精品 | 男女爱爱福利视频 | 免费黄色av网站 | 伊人春色在线观看 | 成人国产精品视频 | 日韩成人| 欧美精品一区二区在线观看 | 四虎首页| 99热这里有精品 | 国产中文区二幕区2012 | 一区二区高清 | 日韩欧美大片 | 国产精品自产av一区二区三区 | 精品欧美一区二区三区 | 午夜羞羞| 国产精品美女久久久 | 日日干天天操 | 日韩福利 | 二区av| 亚洲欧美日本在线 | aaa一区| 成人在线免费 | 日韩a在线 | 在线伊人 | 精品1区| 国产二区三区 | 毛片区 | 人人干人人看 | 日韩一区三区 | 久久国产精99精产国高潮 | 在线观看成年视频 | 91网视频| 一区二区三区四区av | 中文字幕亚洲欧美 | 成人毛片视频免费 | 欧美伊人影院 | 干干干操操操 | 国产97碰免费视频 | 中文字幕一区二区三区四区五区 | 精品欧美乱码久久久久久 | 久久国产欧美日韩精品 |