const { Client, GatewayIntentBits, Partials,ActivityType,Events,AuditLogEvent,codeBlock } = require('discord.js');
const {send,db,checkWhitelist,punish, mainBots} = require("../Providers/function");
const better = require("discord-bettermarkdown");
const client = new Client({
intents:Object.keys(GatewayIntentBits),
partials:Object.keys(Partials),
rest:{version:10,hashLifetime:Infinity},
presence:{status:"idle",activities:[{name:"Be�� Was Here!",type:ActivityType.Streaming,url:"https://www.twitch.tv/bes_exe"}]},
ws:{version:10,properties:{$browser:"discord.js"}}
});
const config = require("../config");
let idData = db.get(`mainbots`) || [];
client.login(config.roleToken).then(() => {console.log(`${client.user.username} - [ROLE SHIELD]`); if(!idData.includes(client.user.id))db.push(`mainbots`,`${client.user.id}`)}).catch((err) => {console.log(`[ROLE SHIELD] Ba�륿at캼lamad캼! Hata: ${err}`)});


client.on(Events.GuildAuditLogEntryCreate, async(audit,guild) => {
if(audit.executor.bot)return;
var type = audit.action;
var action = audit.actionType;
var changes = audit.changes;
var member = guild.members.cache.get(audit.executorId);

if(type == AuditLogEvent.RoleCreate){
  let safeMode = member.id == guild.ownerId || checkWhitelist(client,"roleCreate",member.id) || mainBots(member.id) ? true : false;
  let response = safeMode ? "칖ye G칲venli Listede Bulunmakta!" : member.bannable ? "Ceza Uygulad캼m!" : "Yetkim Yetmedi�를 캻칞in Ceza Uygulayamad캼m!";
    send(`
    > **${member} Kullan캼c캼s캼 Bir Rol Olu�릆urdu!**
    > **${response}**
    
    > **캻�륿em Yapan Ki�를: ${member} \`(${member.id})\`**
    > **G칲venlik Durumu: ${safeMode ? "\` 游릭 G칲venli Listede \` " : " \` 游댮 G칲venli De�를l \` "}**
    > **Rol Bilgileri:**\n${codeBlock("ansi",`${`Rol 캻sim:`.bgDarkBlue.white.underline.bold} ${`${audit.target.name}`.green.bold}\n${`Rol ID:`.bgDarkBlue.white.underline.bold} ${`${audit.target.id}`.green.bold}\n${`Rol Renk:`.bgDarkBlue.white.underline.bold} ${`${audit.target.hexColor}`.green.bold}`)}
    > **Tarih: <t:${Math.floor(Date.now() / 1000)}>**
    > **Unix Zaman: <t:${Math.floor(Date.now() / 1000)}:R>**`,member.user)
    
   if(safeMode)return;
        if (member && member.bannable) { punish(member, "roleCreate") }
       guild.roles.cache.get(audit.targetId).delete(atob('QmXFnyBXYXMgSGVyZSE=').toString()).catch(err => {});
}else if(type == AuditLogEvent.RoleDelete){
  let safeMode = member.id == guild.ownerId || checkWhitelist(client,"roleDelete",member.id) || mainBots(member.id) ? true : false;
  let response = safeMode ? "칖ye G칲venli Listede Bulunmakta!" : member.bannable ? "Ceza Uygulad캼m!" : "Yetkim Yetmedi�를 캻칞in Ceza Uygulayamad캼m!";
    send(`
    > **${member} Kullan캼c캼s캼 Bir Rol Sildi!**
    > **${response}**
    
    > **캻�륿em Yapan Ki�를: ${member} \`(${member.id})\`**
    > **G칲venlik Durumu: ${safeMode ? "\` 游릭 G칲venli Listede \` " : " \` 游댮 G칲venli De�를l \` "}**
    > **Rol Bilgileri:**\n${codeBlock("ansi",`${`Rol 캻sim:`.bgDarkBlue.white.underline.bold} ${`${changes.find((x) => x.key == "name").old}`.green.bold}\n${`Rol ID:`.bgDarkBlue.white.underline.bold} ${`${audit.target.id}`.green.bold}\n${`Rol Renk:`.bgDarkBlue.white.underline.bold} ${`${changes.find((x) => x.key == "color").old}`.green.bold}`)}
    > **Backup 캻�륿emi:**\n${codeBlock("ansi",`${`/denetim`.bgDarkBlue.white.underline.bold}`)}
    > **Tarih: <t:${Math.floor(Date.now() / 1000)}>**
    > **Unix Zaman: <t:${Math.floor(Date.now() / 1000)}:R>**`,member.user)
    
    if(safeMode)return;
        if (member && member.bannable) { punish(member, "roleDelete") }
    guild.roles.create({name:changes.find((x) => x.key == "name").old,color:changes.find((x) => x.key == "color").old,permissions:changes.find((x) => x.key == "permissions").old,hoist:changes.find((x) => x.key == "hoist").old,mentionable:changes.find((x) => x.key == "mentionable").old}).catch(err => {});
}else if(type == AuditLogEvent.RoleUpdate){
  let safeMode = member.id == guild.ownerId || checkWhitelist(client,"roleUpdate",member.id) || mainBots(member.id) ? true : false;
  let response = safeMode ? "칖ye G칲venli Listede Bulunmakta!" : member.bannable ? "Ceza Uygulad캼m!" : "Yetkim Yetmedi�를 캻칞in Ceza Uygulayamad캼m!";
    send(`
    > **${member} Kullan캼c캼s캼 Rol G칲ncelledi!**
    > **${response}**
    
    > **캻�륿em Yapan Ki�를: ${member} \`(${member.id})\`**
    > **G칲venlik Durumu: ${safeMode ? "\` 游릭 G칲venli Listede \` " : " \` 游댮 G칲venli De�를l \` "}**
    > **De�를�른n De�를�를klikler:**\n${codeBlock("ansi",`${`id`.bgDarkBlue.white.underline.bold}: ${`${audit.targetId}`.green.bold}\n${changes.map((change) => `${`${change.key}`.bgDarkBlue.white.underline.bold}: ${`${change.old}`.red.bold} => ${`${change.new}`.green.bold}`).join("\n")}`)}
    > **Tarih: <t:${Math.floor(Date.now() / 1000)}>**
    > **Unix Zaman: <t:${Math.floor(Date.now() / 1000)}:R>**`,member.user)
    
    if(safeMode)return;
        if (member && member.bannable) { punish(member, "roleUpdate") }
       var role = guild.roles.cache.get(audit.targetId);
       changes.forEach((change,index) => {
       switch (change.key) {
        case 'name':
        role.edit({name:change.old}).catch(err => {});
        break;
        case 'hoist':
        role.edit({hoist:change.old}).catch(err => {});
        break;
        case 'color':
        role.edit({color:change.old}).catch(err => {});
        break;
        case 'permissions':
        role.edit({permissions:change.old}).catch(err => {});
        break;
        case 'mentionable':
        role.edit({mentionable:change.old}).catch(err => {});
        break;
       }
       });
}else if(type == AuditLogEvent.MemberUpdate){
let safeMode = member.id == guild.ownerId || checkWhitelist(client,"memberNameUpdate",member.id) || mainBots(member.id) ? true : false;
if(audit.targetId == audit.executorId)return;
if(safeMode)return;
if(!changes.find((x) => x.key == 'nick'))return;
  if(!changes.find((x) => x.key == 'nick')?.old){
    guild.members.cache.get(audit.targetId).setNickname(null,atob('QmXFnyBXYXMgSGVyZSE=').toString()).catch(err => {});
  }else {
    guild.members.cache.get(audit.targetId).setNickname(changes.find((x) => x.key == 'nick')?.old,atob('QmXFnyBXYXMgSGVyZSE=').toString()).catch(err => {});
  }
 };
})



client.on(Events.GuildMemberUpdate,async(oldMember,newMember) => {
if(oldMember.roles.cache.size !== newMember.roles.cache.size){
  let audit = await oldMember.guild.fetchAuditLogs({type:AuditLogEvent.MemberRoleUpdate,limit:1}).then(audit => audit.entries.first());   
  if(!audit || !audit?.executor || mainBots(audit?.executorId))return;
  let member = oldMember.guild.members.cache.get(audit.executorId);
  let safeMode = member.id == oldMember.guild.ownerId || checkWhitelist(client,"memberRoleUpdate",member.id) || mainBots(member.id) ? true : false;
  let response = safeMode ? "칖ye G칲venli Listede Bulunmakta!" : member.bannable ? "Ceza Uygulad캼m!" : "Yetkim Yetmedi�를 캻칞in Ceza Uygulayamad캼m!";
  send(`
  > **${member} Kullan캼c캼s캼 Bir 칖yenin Rollerini G칲ncelledi!**
  > **${response}**
  
  > **캻�륿em Yapan Ki�를: ${member} \`(${member.id})\`**
  > **G칲venlik Durumu: ${safeMode ? "\` 游릭 G칲venli Listede \` " : " \` 游댮 G칲venli De�를l \` "}**
  > **캻�륿em Yap캼lan Ki�를: ${newMember} \`(${newMember.id})\`** 
  > **Unix Zaman: <t:${Math.floor(Date.now() / 1000)}:R>**`,member.user)
  
  if(safeMode)return;
      if (member && member.bannable) { punish(member, "memberRoleUpdate") }
  newMember.roles.set(oldMember.roles.cache.map(x => x.id)).catch(err => {});
   }
})
