const { Client, Intents } = require("discord.js");
const config = require('./config.json')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });
client.login(config.token);
client.on("guildMemberAdd", async (member) => {
await member.roles.add("967497971686080534")
})
public async exec(oldState, newState): Promise<void> {
if (oldState.channel) {
await handlerChannelLeader(this.client, oldState, 'leave')
}
if (newState.channel) {
await handlerChannelLeader(this.client, newState, 'join')
}
}
export async function handlerChannelLeader(client, state: VoiceState, type: 'leave' | 'join') {
const {channel, member} = state
let channel_leaders = await client.db.query(`SELECT * FROM channel_leaders WHERE channel_id='${channel.id}'`)
await delLastStar(member, member.nickname ? member.nickname : member.user.username)
if (channel_leaders['length'] === 0)
await client.db.query(`INSERT INTO channel_leaders (channel_id,user_id)VALUES('${channel.id}', '0')`)
channel_leaders = await client.db.query(`SELECT * FROM channel_leaders WHERE channel_id='${channel.id}'`)
if (channel.members.size === 0) {
await client.db.query(`UPDATE channel_leaders SET user_id='0' WHERE channel_id='${channel.id}'`)
await delLastStar(member, member.nickname ? member.nickname : member.user.username)
} else if (type === 'leave' && channel_leaders[0].user_id === member.id) {
const firstMember = channel.members.first()
await client.db.query(`UPDATE channel_leaders SET user_id='${firstMember.id}' WHERE channel_id='${channel.id}'`)
await changeLeaderNickname(firstMember)
} else if (type === 'join' && channel.members.size === 1) {
await client.db.query(`UPDATE channel_leaders SET user_id='${member.id}' WHERE channel_id='${channel.id}'`)
await changeLeaderNickname(member)
} else if (type === 'join' && channel.members.filter((m) => m.id !== channel_leaders[0].user_id).size === 0) {
await client.db.query(`UPDATE channel_leaders SET user_id='${member.id}' WHERE channel_id='${channel.id}'`)
await changeLeaderNickname(member)
}
}