From e30d07b040efe43eea47e59299861d4be81d5d98 Mon Sep 17 00:00:00 2001 From: TheLlamamarketer <98122020+TheLlamamarketer@users.noreply.github.com> Date: Wed, 20 Sep 2023 21:44:33 +0200 Subject: [PATCH] final touches --- commands/test/changeRoles.ts | 105 --------------------------------- commands/test/inviteTracker.ts | 79 ++++++++++++------------- 2 files changed, 38 insertions(+), 146 deletions(-) delete mode 100644 commands/test/changeRoles.ts diff --git a/commands/test/changeRoles.ts b/commands/test/changeRoles.ts deleted file mode 100644 index 32c7e1f..0000000 --- a/commands/test/changeRoles.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { Guild, Role } from 'discord.js'; -import { client, db } from '../../index'; -import cron from 'cron'; - -async function hi() { - console.log('change Roles is working!') - - // Fetch users from the database - const users = await db.db('contrabot').collection("users").find({}).toArray(); - - // Get the guild and roles - const guild = client.guilds.cache.get('1119231777391788062'); - if (!guild) { - console.error('Guild not found'); - return; - } - - // Define an object to store invite data per user - const invites: { [key: string]: { [key: string]: number } } = {}; - - try { - // Fetch all Guild Invites - const firstInvites = await guild.invites.fetch(); - - // Populate the invites object with invite code and uses - firstInvites.forEach((invite) => { - if (invite.uses !== null && invite.inviter) { - const inviterId = invite.inviter.id; - const user = users.find((user) => user.userId === inviterId); - - if (user) { - invites[user.userId] = invites[user.userId] || {}; - invites[user.userId][invite.code] = invite.uses; - } - } - }); - } catch (error) { - console.error('Error fetching invites:', error); - return; - } - - // Event listener for inviteDelete - client.on("inviteDelete", (invite) => { - if (invite.inviter) { - const inviterId = invite.inviter.id; - const user = users.find((user) => user.userId === inviterId); - - if (user && invites[user.userId]) { - delete invites[user.userId][invite.code]; - } - } - }); - - // Event listener for inviteCreate - client.on("inviteCreate", (invite) => { - if (invite.inviter) { - const inviterId = invite.inviter.id; - const user = users.find((user) => user.userId === inviterId); - - if (user) { - if (typeof invite.uses === 'number') { - invites[user.userId] = invites[user.userId] || {}; - invites[user.userId][invite.code] = invite.uses; - } - } - } - }); - - // Update the 'invite' property for all users and assign roles based on inviteCount - for (const userId in invites) { - if (!userId) { - console.error('Member ID not found for a user'); - continue; - } - - const inviteCount = Object.values(invites[userId]).reduce((acc, count) => acc + count, 0); - - const rolesToAssign = [ - { role: '1153789870582550598', minInviteCount: 0, maxInviteCount: 2 }, - { role: '1153796740072349726', minInviteCount: 3, maxInviteCount: 4 }, - { role: '1153992358212423730', minInviteCount: 5, maxInviteCount: Infinity }, - ]; - - for (const { role, minInviteCount, maxInviteCount } of rolesToAssign) { - if (inviteCount >= minInviteCount && inviteCount <= maxInviteCount) { - assignRoleIfQualified(role, userId, guild); - break; // Stop after assigning the highest matching role - } - } - } -} - -async function assignRoleIfQualified(roleId: string, userId: any, guild: Guild) { - const member = await guild.members.fetch(userId); - if (member) { - if (!member.roles.cache.has(roleId)) { - await member.roles.add(roleId).catch(console.error); - } - } else { - console.error('Member not found'); - } -} - -const job = new cron.CronJob('0 * * * * *', hi); // checks for invites every minute -//job.start(); \ No newline at end of file diff --git a/commands/test/inviteTracker.ts b/commands/test/inviteTracker.ts index d15dba3..d1f065b 100644 --- a/commands/test/inviteTracker.ts +++ b/commands/test/inviteTracker.ts @@ -1,4 +1,4 @@ -import { Guild, Role } from 'discord.js'; +import { Guild, GuildMember, Role, Collection } from 'discord.js'; import { client, db } from '../../index'; import cron from 'cron'; @@ -17,6 +17,7 @@ async function trackInvites() { // Create an object to store invite data per user const inviteData: { [key: string]: number } = {}; + // Store number of invites per inviter invites.forEach((invite) => { const inviter = invite.inviter; @@ -30,7 +31,7 @@ async function trackInvites() { }); - // Update the user invite counts in your database + // Update the user invite counts const users = await db.db('contrabot').collection('users').find({}).toArray(); for (const user of users) { @@ -48,54 +49,50 @@ async function trackInvites() { { $set: { inviteCount: inviteCount } } ); - - const rolesToAssign = [ - { role: guild.roles.cache.get('1153789870582550598'), minInviteCount: 1, maxInviteCount: 2 }, - { role: guild.roles.cache.get('1153796740072349726'), minInviteCount: 3, maxInviteCount: 4 }, - { role: guild.roles.cache.get('1153992358212423730'), minInviteCount: 5, maxInviteCount: Infinity }, - ]; - const rolesToRemove = rolesToAssign.map((roleData) => roleData.role).filter((role) => role !== undefined); - - for (const { role, minInviteCount, maxInviteCount } of rolesToAssign) { - if (role) { - if (inviteCount >= minInviteCount && inviteCount <= maxInviteCount) { - assignRoleIfQualified(role, userId, guild); - rolesToRemove.splice(rolesToRemove.indexOf(role), 1); - break; // Stop after assigning the highest matching role - } - } else { - console.error(`Role not found for user ${userId}`); - } - } - removeRoles(userId, rolesToRemove, guild) + assignRoles(inviteCount, userId, guild); } } -async function assignRoleIfQualified(role: Role, userId: any, guild: Guild) { + +async function assignRoles(inviteCount: number, userId: string, guild: Guild) { + const rolesToAssign = [ + { role: '1153789870582550598', minInviteCount: 1, maxInviteCount: 2 }, + { role: '1153796740072349726', minInviteCount: 3, maxInviteCount: 4 }, + { role: '1153992358212423730', minInviteCount: 5, maxInviteCount: Infinity }, + ]; + const rolesToRemove: Collection = new Collection(); + const member = await guild.members.fetch(userId); - if (member) { - if (!member.roles.cache.has(role.id)) { - await member.roles.add(role).catch(console.error); + + for (const { role, minInviteCount, maxInviteCount } of rolesToAssign) { + const targetRole = guild.roles.cache.get(role); + + if (!targetRole) { + console.error(`Role not found for user ${userId} `); + continue; + } else if (!member) { + console.error(`Member not found`); + continue; + } + + if (inviteCount >= minInviteCount && inviteCount <= maxInviteCount) { + if (!member.roles.cache.has(targetRole.id)) { + await member.roles.add(targetRole).catch(console.error); + } + } else { + rolesToRemove.set(targetRole.id, targetRole); } - } else { - console.error('Member not found'); } + removeRoles(rolesToRemove, member); } -async function removeRoles(userId: any, rolesToRemove: any, guild: Guild) { - const member = await guild.members.fetch(userId); - if (member) { - for (const role of rolesToRemove) { - if (member.roles.cache.has(role.id)) { - console.log(`Removing role ${role.name} from user ${userId}`); - await member.roles.remove(role).catch(console.error); - } else { - console.log(`User ${userId} does not have role ${role.name}`); - } + +async function removeRoles(rolesToRemove: Collection, member: GuildMember) { + for (const role of rolesToRemove.values()) { + if (member.roles.cache.has(role.id)) { + await member.roles.remove(role).catch(console.error); } - } else { - console.error('Member not found'); } } -const job = new cron.CronJob('0 * * * * *', trackInvites); // checks for invites every minute +const job = new cron.CronJob('0 0 * * * *', trackInvites); // checks for invites every hour job.start(); \ No newline at end of file