ReQ
ReQ

Reputation: 3

guildMemberAdd event doesn't work in discord.js bot

I want to know what is the solution to this problem and what I'm supposed to do to fix this error.

I tried to fix for about 3 hours, but I got no results.

I would like this event to trigger when someone joins the server to list nickname, tag and account creation time using EmbedBuilder.

guildMemberAdd Event:

const {GuildMember,EmbedBuilder} = require("discord.js");

const moment = require("moment");

module.exports = {
name: "guildMemberAdd",
/\*\*
\*
\* @param {GuildMember} member
\*/

    async execute(member, client){
        const guildConfig = client.guildConfig.get(member.guild.id);
        if(!guildConfig) return;
    
        const guildRoles = member.guild.roles.cache;
        let assigneRole = member.user
        guildRoles.get(guildConfig.memberRole);
    
        if(!assigneRole) assigneRole = " Not configured.";
        else await member.roles.add(assigneRole)
        .catch(()=> { assigneRole = "Failed due to role hierarchy"});
    
        const welcomChannel = (await member.guild.channels.fetch()).get(guildConfig.welcomChannel);
        if(!welcomChannel) return
        let color = "#74e21e"
        const accountCreation = parseInt(member.user.createdTimestamp / 1000);
        const joiningTime = parseInt(member.joinedAt / 1000);
    
        const MonthAgo = moment().subtract(2,"month").unix();
        const weekAgo = moment().subtract(2,"weeks").unix();
        const dayAgo = moment().subtract(2,"days").unix();
    
        if(accountCreation >= MonthAgo){
            color = "#e2bb1e"
        }
        if(accountCreation >= weekAgo){
            color = "#e24d1c"
        }
        if(accountCreation >= dayAgo){
            color = "#e21e1e"
        }
    
        const Embed = new EmbedBuilder()
        .setAuthor({name: `${member.user.tag}| ${member.id}`, iconURL: member.displayAvatarURL({dynamic: true})})
        .setColor(color)
        .setThumbnail(member.user.displayAvatarURL({dynamic: true,size: 256}))
        .setDescription([
            `- User: ${member.user}`,
            `- Role Assigend: ${assigneRole}`,
            `- Account Created: <t:${accountCreation}:D> | <t:${accountCreation}:R>`,
            `- Account Joined: <t:${joiningTime}:D> | <t:${joiningTime}:R>`
        ].join("\n"))
        .setFields({text: "Joined"})
        .setTimestamp();
        welcomChannel.send({Embed: [Embed]});
    }

}

Error:

node:events:491
      throw er; // Unhandled 'error' event
      ^

CombinedPropertyError: Received one or more errors
    at ArrayValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:457:70)
    at ArrayValidator.parse (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:201:88)
    at EmbedBuilder.spliceFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:207:31)
    at EmbedBuilder.setFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:215:10)
    at Object.execute (D:\\Vinnint\\Events\\guildMemberAdd.js:54:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
  errors: \[
    \[
      0,
      CombinedPropertyError: Received one or more errors
          at ObjectValidator.handleIgnoreStrategy (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1252:70)
          at ObjectValidator.handleStrategy (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1105:47)
          at ObjectValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:1205:17)
          at ObjectValidator.run (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:187:23)
          at ArrayValidator.handle (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:451:37)
          at ArrayValidator.parse (D:\\Vinnint\\node_modules\\@sapphire\\shapeshift\\dist\\index.js:201:88)
          at EmbedBuilder.spliceFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:207:31)
          at EmbedBuilder.setFields (D:\\Vinnint\\node_modules\\@discordjs\\builders\\dist\\index.js:215:10)
          at Object.execute (D:\\Vinnint\\Events\\guildMemberAdd.js:54:10)
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
        errors: \[
          \[ 'name', \[MissingPropertyError\] \],
          \[ 'value', \[MissingPropertyError\] \]
        \]
      }
    \]
  \]
}

Node.js v18.12.1

Upvotes: 0

Views: 271

Answers (1)

Will Sullivan
Will Sullivan

Reputation: 354

I see a couple of small errors in your EmbedBuilder code.

  • setFields does not exist. Use addFields to add fields to an Embed
  • addFields requires both a name and a value
    • Note: addFields is not required
  • Channel send method expects an array of embeds
const Embed = new EmbedBuilder()
  .setAuthor({
    name: `${member.user.tag} | ${member.id}`,
    iconURL: member.displayAvatarURL({ dynamic: true }),
  })
  .setColor(color)
  .setThumbnail(member.user.displayAvatarURL({ dynamic: true, size: 256 }))
  .setDescription(
    [
      `- User: ${member.user}`,
      `- Role Assigend: ${assigneRole}`,
      `- Account Created: <t:${accountCreation}:D> | <t:${accountCreation}:R>`,
      `- Account Joined: <t:${joiningTime}:D> | <t:${joiningTime}:R>`,
    ].join("\n")
  )
  .addFields({ name: "Event", value: "Joined" })
  .setTimestamp();
await welcomChannel.send({ embeds: [Embed] });

Upvotes: 1

Related Questions