Discord. js случайные ролевые реакции - PullRequest
0 голосов
/ 10 апреля 2020

Я пытаюсь создать команду, которая посылает код для вставки и реагирует с эмодзи, и когда участник щелкает по эмодзи, это дает им случайную роль. Любая идея о том, как я go об этом?

1 Ответ

0 голосов
/ 14 апреля 2020
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, чтобы иметь возможность добавить роль к члену гильдии.

...