const { EmbedBuilder } = require("discord.js");
const bannedTag = require("../../../../../../Global/Schemas/bannedTag");
const ertum = require("../../../../../../Global/Settings/Setup.json")
const ertucuk = require("../../../../../../Global/Settings/System");
const regstats = require("../../../../../../Global/Schemas/registerStats");
const { green, red } = require("../../../../../../Global/Settings/Emojis.json")
const client = global.client;


client.on("userUpdate", async (oldMember, newMember) => {
  
  const guild = client.guilds.cache.get(ertucuk.ServerID);
  const member = guild.members.cache.get(newMember.id);
  const ChatChannel = guild.channels.cache.get(ertum.ChatChannel)

  if (oldMember.displayName == newMember.displayName || oldMember.bot || newMember.bot || member.roles.cache.has(ertum.JailedRoles[0])) return;

  if (ertum.ServerTag.some(ertu => client.users.cache.get(newMember.id).displayName.includes(ertu))) {
    member.roles.add(ertum.TaggedRole);
    client.channels.cache.find(x => x.name === "taglÄ±_log").send({embeds: [ new EmbedBuilder().setThumbnail(member.displayAvatarURL({ dynamic: true, size: 2048 })).setDescription(`
 ${member} kullanÄ±cÄ±sÄ± <t:${Math.floor(Date.now() / 1000)}:R> tagÄ±mÄ±zÄ± aldÄ±.

\` â�œ \` Ä°sim DeÄŸiÅŸikliÄŸi:  ${oldMember.displayName} **=>** ${newMember.displayName}
\` â�œ \` AnlÄ±k taglÄ± Ã¼ye: **${guild.members.cache.filter(x => x.user.displayName.includes(ertum.ServerTag)).size}**
    `)]});
    ChatChannel.send({ content:`ğŸ�‰ Tebrikler, ${member} kullanÄ±cÄ±sÄ± tagÄ±mÄ±zÄ± alarak ailemize katÄ±ldÄ±!`}).then((e) => setTimeout(() => { e.delete(); }, 5000));
  } else if (ertum.ServerTag.some(ertu => !client.users.cache.get(newMember.id).displayName.includes(ertu) && member.roles.cache.has(ertum.TaggedRole))) {
    let role = guild.roles.cache.get(ertum.TaggedRole);
    let roles = member.roles.cache.clone().filter(e => e.managed || e.position < role.position);
    await member.roles.set(roles).catch();
    client.channels.cache.find(x => x.name === "taglÄ±_log").send({embeds: [ new EmbedBuilder().setThumbnail(member.displayAvatarURL({ dynamic: true, size: 2048 })).setDescription(
`${member} kullanÄ±cÄ±sÄ± <t:${Math.floor(Date.now() / 1000)}:R> tagÄ±mÄ±zÄ± bÄ±raktÄ±.
      
\` â�œ \` Ä°sim DeÄŸiÅŸikliÄŸi: ${oldMember.displayName}  **=>** ${newMember.displayName} 
\` â�œ \` AnlÄ±k taglÄ± Ã¼ye: **${guild.members.cache.filter(x => x.user.displayName.includes(ertum.ServerTag)).size}**
`)]});
} 


/////////////// YASAKLI TAG KNK //////////////////////////////////////////////////////////////////
const yasaklitag = await bannedTag.findOne({ guildID: ertucuk.ServerID });
if (!yasaklitag) return
yasaklitag.taglar.forEach(async x => {
  
if (!oldMember.tag.includes(x) && newMember.tag.includes(x)) {
    !member.roles.cache.has(ertum.BoosterRole) 
    await member.roles.set(ertum.ForbiddenTagRoles).catch();
    await member.setNickname('YasaklÄ± Tag');
   member.send({ content:`
   **Merhaba** ${member}

   Bu yazÄ±, sunucumuz iÃ§erisindeki kurallarÄ±mÄ±za uymadÄ±ÄŸÄ± tespit edilen bir sembolÃ¼n, sizin hesabÄ±nÄ±zda tespit edildiÄŸini bildirmek amacÄ±yla yazÄ±lmÄ±ÅŸtÄ±r. Ãœzerinizde bulunan (${x}) sembolÃ¼ sunucumuz kurallarÄ±na aykÄ±rÄ± olduÄŸu iÃ§in hesabÄ±nÄ±z yasaklÄ± kategorisine eklenmiÅŸtir.

   Bu durumun dÃ¼zeltilmesi iÃ§in, yasaklÄ± sembolÃ¼ kaldÄ±rmanÄ±z gerekmektedir. SÃ¶z konusu yasaklÄ± sembol hesabÄ±nÄ±zdan Ã§Ä±karÄ±ldÄ±ÄŸÄ±nda, eÄŸer daha Ã¶nce kayÄ±tlÄ±ysanÄ±z otomatik olarak kayÄ±tlÄ± duruma geÃ§eceksiniz. Ancak, eÄŸer kayÄ±tlÄ± deÄŸilseniz, tekrar kayÄ±tsÄ±za dÃ¼ÅŸeceksiniz.
   
   Herhangi bir sorunuz veya aÃ§Ä±klamanÄ±z iÃ§in moderatÃ¶r ekibimizle iletiÅŸime geÃ§ebilirsiniz.
   
   SaygÄ±larÄ±mla,
   **${guild.name}** Moderasyon Ekibi `}).catch(() => {});
  } else
  if (oldMember.tag.includes(x) && !newMember.tag.includes(x)) { 
    !member.roles.cache.has(ertum.BoosterRole) 
    await member.roles.set(ertum.UnRegisteredRoles).catch();
    await member.setNickname(`${member.user.username.includes(ertum.ServerTag) ? ertum.ServerTag : (ertum.ServerUntagged ? ertum.ServerUntagged : (ertum.ServerTag || ""))} Ä°sim | YaÅŸ`);
  member.send({ content:`${guild.name} adlÄ± sunucumuza olan eriÅŸim engeliniz kalktÄ±. Ä°sminizden (${x}) sembolÃ¼nÃ¼ kaldÄ±rarak sunucumuza eriÅŸim hakkÄ± kazandÄ±nÄ±z. Keyifli Sohbetler`}).catch(() => {});
  }
})
})

module.exports.config = {
    Event: "userUpdate"
};
  