Unable to get Discord.js bot to send join/leave logs

Viewed 27

I'm working to try and develop a Discord.js bot. However, I've run into an issue I can't find a fix for. These two chunks of code (formatted how I like it) are supposed to send join and leave messages in a specific discord channel. I'm rather new to coding, so I don't know what the issue is. When a user leaves, nothing happens, and when a user joins, the bot crashes with an error I can't understand. I assumed it was part of the code that's getting user info, such as Discord.GuildMember.user.id, but I've tried every other variation of it I've used for the rest of the code. I'm unsure what's not working. Thank you for any help in advance!

client.on("guildMemberAdd", message => {

  const currentDate = new Date();
  const timestampInMs = currentDate.getTime();
  const unixTimestamp = Math.floor(currentDate.getTime() / 1000);

  const userJoined = new EmbedBuilder()
    .setColor(0x0099FF)
  .setTitle(`${Discord.GuildMember.username} joined the server.`)
  .setDescription(`Joined: <@${Discord.GuildMember.user.id}> **Account Created**:  ${Discord.GuildMember.user.createdAt}`)
  .setAuthor({ name: `User Logging System`, iconURL: (Discord.GuildMember.avatarURL()) })
  .addFields(
    { name: `User ID`, value: `${Discord.GuildMember.user.id}` },
    { name: `Timestamp`, value: `<t:${unixTimestamp}>`}
  )
  .setFooter({ text: `${currentDate}` })

  {client.channels.cache.get("1010193667975282698")
      .send ({ embeds: [userJoined] });

}});

client.on("guildMemberRemove", message => {

  const currentDate = new Date();
  const timestampInMs = currentDate.getTime();
  const unixTimestamp = Math.floor(currentDate.getTime() / 1000);

    const userLeft = new EmbedBuilder()
  .setColor(0x0099FF)
  .setTitle(`${Discord.GuildMember.username} left the server.`)
  .setDescription(`Left: <@${Discord.GuildMember.user.id}> **Account Created**:  ${Discord.GuildMember.user.createdAt}`)
  .setAuthor({ name: `User Logging System`, iconURL: (Discord.GuildMember.avatarURL()) )
  .addFields(
    { name: `User ID`, value: `${Discord.GuildMember.user.id}` },
    { name: `Timestamp`, value: `<t:${unixTimestamp}>`}
  )
  .setFooter({ text: `${currentDate}` })

  {client.channels.cache.get("1010666655615029259")
      .send ({ embeds: [userLeft] });

}});
1 Answers

Your code seems to be fine except for a few typos. Try something like this

client.on("guildMemberAdd", member => {

  const currentDate = new Date();
  const timestampInMs = currentDate.getTime();
  const unixTimestamp = Math.floor(currentDate.getTime() / 1000);

  const userJoined = new EmbedBuilder()
    .setColor(0x0099FF)
  .setTitle(`${member.user.username} joined the server.`)
  .setDescription(`Joined: <@${member.user.id}> **Account Created**: ${member.user.createdAt}`)
  .setAuthor({ name: `User Logging System`, iconURL: (member.user.avatarURL()) })
  .addFields(
    { name: `User ID`, value: `${member.user.id}` },
    { name: `Timestamp`, value: `<t:${unixTimestamp}>`}
  )
  .setFooter({ text: `${currentDate}` })

  member.channels.cache.get("1010193667975282698").send({ embeds: [userJoined] });

});

client.on("guildMemberRemove", member => {

  const currentDate = new Date();
  const timestampInMs = currentDate.getTime();
  const unixTimestamp = Math.floor(currentDate.getTime() / 1000);

    const userLeft = new EmbedBuilder()
  .setColor(0x0099FF)
  .setTitle(`${member.user.username} left the server.`)
  .setDescription(`Left: <@${member.user.id}> **Account Created**:  ${member.user.createdAt}`)
  .setAuthor({ name: `User Logging System`, iconURL: (member.user.avatarURL()) )
  .addFields(
    { name: `User ID`, value: `${member.user.id}` },
    { name: `Timestamp`, value: `<t:${unixTimestamp}>`}
  )
  .setFooter({ text: `${currentDate}` })

  member.channels.cache.get("1010666655615029259").send({ embeds: [userLeft] });

});

I haven't done this in a while so if there's any errors mb, but this is what it should look like.

Related