From 989bb5d671d5259aacaf85a5fee1a8bfe51a2511 Mon Sep 17 00:00:00 2001 From: masterhc Date: Sun, 11 Feb 2024 02:53:38 +0000 Subject: [PATCH] Restore strike self-actionability --- lib.js | 85 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/lib.js b/lib.js index 63985e6..135a0df 100644 --- a/lib.js +++ b/lib.js @@ -1265,46 +1265,63 @@ class Strikes * TODO: Handle player if strikes have reached the threshold * */ - const Strikes = await strikesM.find().then(s=>{return s}); - const guilds = await guildsM.find({strikes:true}).then(g=>{return g}); - if(!guilds) return console.log('Lib: Striker: No Guilds') - var guildIds = []; - for(var i = 0; i{return s}) + if(Strikes.length==0) return console.log('Lib: Striker: No Strikes') + const guilds = await guildsM.find({strikes:true}).then(g=>{return g}) + .then(guilds => guilds.map(guild => guild.gID));; + if(guilds.length==0) return console.log('Lib: Striker: No Guilds') + const validStrikes = await strikesM.find({validated:true}).then(res=>{return res}); + const strikesByGuildID = new Map(); + validStrikes.forEach(strike => { - guildIds.push(guilds[i].gID); - } - var filtered = Strikes.filter( - (e) => { - return guildIds.indexOf(e.guildID)>=0; - } - ) - guildIds.forEach(guild=> + const { strokedID, guildID } = strike; + + // If the guildID key doesn't exist in strikesByGuildID, create it + if (!strikesByGuildID.has(guildID)) strikesByGuildID.set(guildID, new Map()); + + const strikesByStrokedID = strikesByGuildID.get(guildID); + + // If the strokedID key doesn't exist in the strikesByStrokedID map, create it + if (!strikesByStrokedID.has(strokedID)) strikesByStrokedID.set(strokedID, [strike]); + else strikesByStrokedID.get(strokedID).push(strike); + }); + const guildsWith3Strikes = new Map(); + + strikesByGuildID.forEach((strikesByStrokedID, guildID) => { + // Filter strikes by each strokedID to find users with at least three strikes + const usersWith3Strikes = []; + strikesByStrokedID.forEach((strikes, strokedID) => { + if (strikes.length >= 3) { + usersWith3Strikes.push({ user: strokedID, strikes }); + } + }); + + // If there are users with at least three strikes, add them to the map + if (usersWith3Strikes.length > 0) { + guildsWith3Strikes.set(guildID, usersWith3Strikes); + } + }); + const guildsWith3StrikesFiltered = new Map([...guildsWith3Strikes].filter(([guildID]) => guilds.includes(guildID))); + guildsWith3StrikesFiltered.forEach((guild, guildid)=> + { + guild.forEach((user)=> { - filtered.forEach(strike=> - { - (async (guild, strike)=> - { - const ammount = await strikesM.find({guildID:guild, strokedID:strike.strokedID, validated:true}).then(res=>{return res.length}); - if(ammount>2) - { - this.handleStroked(strike.strokedID, guild); - } - })(guild, strike) - }) + this.handleStroked(user,guildid); }) + }) + } handleStroked(user, guild) { - // strikesM.deleteMany({ guildID: guild, strokedID: user }, (err, res) => { - // if (!err) return console.log('Lib: Strikes: Deleted: From User:', user, 'on Guild:', guild) - // }); - // If the user is found, kick them from the server - // const member = this.client.guilds.cache.get(guild).members.cache.get(user); - // if (!member) return console.log('Lib:Strikes: Handle Struck: Member not valid') - // console.log(member.user.username) - // member.kick('Optional reason that will be displayed in the audit logs') - // .then(() => console.log(`Successfully kicked ${user.user.username}`)) - // .catch(err => console.log(`Unable to kick ${user.user.username}: ${err}`)); + const member = this.client.guilds.cache.get(guild).members.cache.get(user.user); + if (!member) return console.log('Lib:Strikes: Handle Struck: Member not valid', member) + const reason = `Strike 1: ${user.strikes[0].reason}; Strike 2: ${user.strikes[1].reason}; Strike 3: ${user.strikes[2].reason}`; + member.kick(reason) + .then(() => console.log(`Successfully kicked ${member.user.username}`)) + .catch(err => console.log(`Unable to kick ${member.user.username}: ${err}`)); + strikesM.deleteMany({ guildID: guild, strokedID: user.user }, (err, res) => { + if (!err) return console.log('Lib: Strikes: Deleted: From User:', member.user.username, 'on Guild:', member.guild.name) + }); } } module.exports.Strikes = Strikes;