简体   繁体   中英

How can I execute certain part of code from a reaction on a private message (not a channel message) with DiscordJS?

I am creating a Discord bot that sends private welcome messages to users that enter my server.

Click to see my Discord bot private message screen

I want to execute some lines of code (I want to add different roles to the users) which differ if the user reacts with those three emojis.
Online I found guides that refer to channel messages only and I don't understand which approach to use with private messages.

Thank you!

The approach for private messages should be the same as channel messages.

// Create a reaction filter that only will collect those three emojis
const filter = (reaction, user) => ['🇮🇹', '🇬🇧', '🤫'].includes(reaction.emoji.name)

// Create reaction collector (message is the message the bot sent).
// The time is the time in milliseconds that the collector should run for
// (i.e. how long the user has to react).
const collector = message.createReactionCollector(filter, {time: 15000})

// Fired when the user reacts
collector.on('collect', (reaction, user) => {
  switch (reaction.name) {
    case '🇮🇹':
      message.reply('you chose Italian!'(
      break
    case '🇬🇧':
      message.reply('you chose English!')
      break
    case '🤫':
      message.reply('you have a secret code!')
  }
})

For more information see the Discord.js guide ( archive ). Note: The guide hasn't been fully updated to v12 so it says that the second parameter on the collector's collect event is the collector itself, like it was in v11.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM