const { Events, EmbedBuilder } = require('discord.js');
const { client } = require('../index.js');
const { Users } = require('../functions/models');

module.exports = {
  name: Events.GuildMemberAdd,
  async execute(member) {
    const addMemberEmbed = new EmbedBuilder()
      .setTitle(`${member.user.globalName} has joined!`)
      .setDescription(`Welcome ${member} to the **Polarcraft** Discord server!`)
      .setColor(process.env.EMBED_COLOR)
      .setThumbnail(member.user.avatarURL());

    const channel = client.channels.cache.get(process.env.LOG_CHANNEL_ID);
    await channel.send({ embeds: [addMemberEmbed] });

    try {
      await Users.create({
        id: member.user.id,
      });
    } catch (error) {
      if (error.name === 'SequelizeUniqueConstraintError') return;
      console.error(error);
    }
  },
};
