Discord.js Bot Not Triggering guildMemberAdd Event

27 views Asked by At

Hello,

I'm working on a Discord bot using discord.js, and I'm trying to set up a welcome message and role assignment for new members joining the server. However, it seems like the guildMemberAdd event is not being triggered or recognized by my bot.

Here's the relevant part of my code:

import { Client, TextChannel, GuildMember } from "discord.js";
import { getWelcomeInfoByServerID } from "../api/get";

module.exports = {
 setup: (client: Client) => {
    client.on("guildMemberAdd", async (member: GuildMember) => {
      console.log(`Member joined: ${member.user.tag}`);

      const serverId = member.guild.id;
      const welcomeInfo = (await getWelcomeInfoByServerID(serverId))[0];
      console.log(welcomeInfo);
      if (!welcomeInfo) return;

      const welcomeChannelId = welcomeInfo.channel_id;
      const welcomeMessage = `Welcome to the server, ${member.user.tag}!`;
      console.log(`Welcome Channel ID: ${welcomeChannelId}`);

      const channel = client.channels.cache.get(welcomeChannelId);
      if (!channel || !(channel instanceof TextChannel)) return;

      const roleId = welcomeInfo.role_id;
      const role = member.guild.roles.cache.get(roleId);
      if (!role) return;
      console.log(`Role ID: ${roleId}`);

      try {
        await channel.send(welcomeMessage);
        await member.roles.add(role);
      } catch (error) {
        console.error(`Failed to add role ${roleId} to ${member.user.tag}:`, error);
      }
    });
 },
};

I've ensured that my bot has the necessary permissions (GUILD_MEMBERS intent enabled in the Discord Developer Portal and in the bot's role in the server), and I've verified that the bot is present in the server and has been invited with the correct permissions.

Despite these checks, the guildMemberAdd event does not seem to be triggered. I've added console logs to debug, and the setup function itself is being called, but the event listener for guildMemberAdd does not appear to be triggered when a new member joins.

I'm using discord.js latest version.

Any help or guidance on why this might be happening and how to resolve it would be greatly appreciated.



I've set up an event listener for the guildMemberAdd event in my Discord bot using discord.js. This event is supposed to trigger when a new member joins the server. I've ensured that my bot has the necessary permissions (GUILD_MEMBERS intent) enabled in both the Discord Developer Portal and in the bot's role within the server. This is crucial for the bot to receive member-related events.

I've also added console logs to debug the issue. I've confirmed that the setup function is being called, but the event listener for guildMemberAdd does not appear to be triggered when a new member joins.

I've verified that the bot is present in the server and has been invited with the correct permissions, including the ability to view channels, send messages, and manage roles.

What I was expecting was for the guildMemberAdd event to be triggered when a new member joins the server. This would allow my bot to execute the code within the event listener, which includes sending a welcome message to the new member and assigning them a role. Upon the event being triggered, I was expecting my bot to send a welcome message to the new member in a specific channel and assign them a role based on the information retrieved from my getWelcomeInfoByServerID function.

I was looking for guidance on why the guildMemberAdd event is not being triggered and how to resolve this issue. Additionally, I was seeking advice on best practices for debugging and handling errors in this context.


0

There are 0 answers