Discord.js随机角色反应

时间:2020-04-10 07:38:52

标签: javascript discord discord.js

我正在尝试发出一个发送嵌入命令并与表情符号反应的命令,当成员单击该表情符号时,它将赋予他们随机角色。对我将如何执行此操作有任何想法吗?

1 个答案:

答案 0 :(得分:0)

const {Client, MessageEmbed} = require('discord.js')

const client = new Client()

client.on('message', 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(embed)
    await message.react(emoji)
    await message.awaitReactions(
      // only for await for the ? emoji from the message author
      (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权限,才能向公会成员添加角色。