久久久久久久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?)
主站蜘蛛池模板: 一本色道久久综合亚洲精品高清 | 成人亚洲片 | 欧美无乱码久久久免费午夜一区 | 欧洲尺码日本国产精品 | 成人在线精品 | a级片在线观看 | 国产一区二区三区色淫影院 | 欧美日韩一| 日韩电影中文字幕在线观看 | 日本一区二区视频 | 国产一区 | 日韩免费高清视频 | 日韩免费在线视频 | 欧美视频在线播放 | 色屁屁在线观看 | 国产免费一区 | 欧美日韩久久精品 | 偷拍亚洲色图 | 欧美爱爱视频 | 毛片久久久 | 麻豆国产一区二区三区四区 | 一区二区在线免费播放 | 亚洲国产黄| 成人av电影免费在线观看 | 女同久久| 精品久久影院 | 91国自视频 | 国产伦精品一区二区三区精品视频 | 91在线看片 | 国产亚洲精品美女久久久久久久久久 | 精品一区二区三区免费视频 | 18性欧美| 日日碰狠狠躁久久躁96avv | 国产内谢 | 中文字幕视频在线免费 | 亚洲区视频| 国产一区视频在线 | 久久久久亚洲精品 | 亚洲一区在线免费观看 | 亚洲精品久久久久中文字幕欢迎你 | 精品一区二区三区入口 |