【问题标题】:Discord.js random role reactionsDiscord.js 随机角色反应
【发布时间】:2021-10-18 18:34:12
【问题描述】:

我正在尝试创建一个发送嵌入并与表情符号做出反应的命令,当成员单击表情符号时,它会给他们一个随机角色。对我将如何做这件事有任何想法吗?

【问题讨论】:

  • 请发布您目前尝试过的代码。 SO 不是代码编写服务,社区只能在您发布带有详细信息的问题时提供帮助。

标签: javascript discord discord.js


【解决方案1】:
const {Client, MessageEmbed} = require('discord.js')

const client = new Client()

client.on('messageCreate', async ({author, channel, guild, member}) => {
  // don't do anything if it's a bot
  if (author.bot) return

  // don't do it if it's a DM
  if (!guild) return

  // replace these with whatever you want
  const embed = new MessageEmbed({title: 'some embed'})
  const emoji = '?'

  const roles = guild.roles.cache.filter(role =>
    // you can't add the @everyone role
    role.name !== '@everyone' &&
    // or managed roles
    !role.managed &&
    // or roles that the bot isn't above
    guild.me.roles.highest.comparePositionTo(role) > 0
  )

  try {
    if (!roles.size) return await channel.send('There are no roles that I can add!')

    const message = await message.channel.send({embeds: [embed]})
    await message.react(emoji)
    await message.awaitReactions({
      // only for await for the ? emoji from the message author
      filter: (reaction, user) => reaction.emoji.name === emoji && user.id === author.id,
      // stop after 1 reaction
      max: 1
    })
    await member.roles.add(roles.random())
  } catch (error) {
    // log all errors
    console.error(error)
  }
})

client.login('your token')

注意:您的机器人必须拥有MANAGE_ROLES 权限才能向公会成员添加角色。

【讨论】:

    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2021-09-11
    • 2020-08-30
    • 2020-07-06
    相关资源
    最近更新 更多