Bagel03
Bagel03

Reputation: 715

React to welcome message discord.js

Whenever a user joins my server, a message like this pops up: Image of welcome message

I need my bot to add those emojis automatically, however when listing to the "guildMemberAdd" event, I have no way to get to this message. ie.

client.on('guildMemberAdd', member => {
//Looked at docs, have no way to get this message
})

How would I go about finding these messages when someone joins?

Upvotes: 1

Views: 839

Answers (2)

Aras Uludağ
Aras Uludağ

Reputation: 183

If you look at the message object of the welcome message, you'll see a different type value compared to normal messages called GUILD_MEMBER_JOIN.

Message {
  channelId: '...',
  guildId: '...',
  id: '...',
  createdTimestamp: 1659971407821,
  type: 'GUILD_MEMBER_JOIN',
  system: true,
  content: '',
  author: ClientUser {
    id: '...',
    bot: true,
    system: false,
    flags: UserFlags { bitfield: 0 },
    username: 'Testing Bot',
    discriminator: '4903',
    avatar: null,
    banner: undefined,
    accentColor: undefined,
    verified: true,
    mfaEnabled: false
  },
  pinned: false,
  tts: false,
  nonce: null,
  embeds: [],
  components: [],
  attachments: Collection(0) [Map] {},
  stickers: Collection(0) [Map] {},
  editedTimestamp: null,
  reactions: ReactionManager { message: [Circular *1] },
  mentions: MessageMentions {
    everyone: false,
    users: Collection(0) [Map] {},
    roles: Collection(0) [Map] {},
    _members: null,
    _channels: null,
    crosspostedChannels: Collection(0) [Map] {},
    repliedUser: null
  },
  webhookId: null,
  groupActivityApplication: null,
  applicationId: null,
  activity: null,
  flags: MessageFlags { bitfield: 0 },
  reference: null,
  interaction: null
}

So you can simply add this under messageCreate.

client.on("messageCreate", (message) => {

  if (message.type === "GUILD_MEMBER_JOIN") {
    message.react("👍");
  }

});

Upvotes: 0

Itamar S
Itamar S

Reputation: 1565

After working a little bit, I've found a little solution to your question:

After you have already seen, Discord sends a little random generated message on a guildMemberAdd event as far as it's enabled by the discord guild owner. This message, of course, has a message object, that we can simply get using the method .lastMessage, which returns the latest message's object in the channel we choose.

Using this, we can easily figure out a way to go on about reacting to a Discord-generated message using:

setTimeout(() => {
    const message = member.guild.channels.cache.get('welcome channel id').lastMessage
    message.react('👍')
}, 500)
// Note: I have set a timeout of half a millisecond as it usually takes the bot longer to register 
// a message at the same time of another command.

From here, the bot will get the latest message from the channel we've picked, and add the reactions we pick.

Upvotes: 1

Related Questions