From 873992dbbfb7ae2ce0b1fd710dacbd45c40d8627 Mon Sep 17 00:00:00 2001 From: masterhc Date: Fri, 9 Feb 2024 23:59:00 +0000 Subject: [PATCH] Revamp of strikes --- commands/admin/strike.js | 13 ++---- lib.js | 93 ++++++++++++++++++++++++++-------------- models/strikes.js | 1 + 3 files changed, 65 insertions(+), 42 deletions(-) diff --git a/commands/admin/strike.js b/commands/admin/strike.js index 8c051bb..dc84568 100644 --- a/commands/admin/strike.js +++ b/commands/admin/strike.js @@ -1,6 +1,6 @@ -const {Command,ErrorMessage, ErrorType, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle} = require('../../lib.js') +const {Command,ErrorMessage, ErrorType, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, Strikes} = require('../../lib.js') const strikeM = require('../../models/strikes'); class strike extends Command{ @@ -18,6 +18,8 @@ const strikeM = require('../../models/strikes'); { //Check admin privilege this.message = message; + const isActive = await new Strikes(this.client).getAreStrikesActive(message.guildId); + if(!isActive) return new ErrorMessage(this.client).send(ErrorType.FeatureInnactive, message) var confirm = await this.confirmArgs(message,args); if(confirm=='Error') return new ErrorMessage(this.client).send(ErrorType.Arguments, message, ['Use the mention method to pass the user.', 'Make sure you provide a reason.']) var strike = new strikeM(); @@ -27,6 +29,7 @@ const strikeM = require('../../models/strikes'); strike.guildID = message.guild.id strike.reason = confirm.reason; strike.date = Date.now(); + strike.expireAt = (new Date(Date.now()+(15*24*60*60*1000))); strike.save(err=> { if(err)console.log(err); @@ -60,14 +63,6 @@ const strikeM = require('../../models/strikes'); collector.on('collect', async m => { m.message.delete() - .then(()=> - { - // console.log('Strikes: Embed Message: Deleted'); - }) - .catch(()=> - { - // console.log('Strikes: Embed Message: failed to delete.'); - }) }); collector.on('end', async ()=> { diff --git a/lib.js b/lib.js index adccd9d..e25a7c0 100644 --- a/lib.js +++ b/lib.js @@ -413,7 +413,8 @@ const ErrorType = Arguments: "Arguments", NoArguments: "No Arguments Given", NotOnTheList: "Unknown Command", - OldMessages: "Can't delete this messages." + OldMessages: "Can't delete this messages.", + FeatureInnactive: "This feature is not active." } module.exports.ErrorType = ErrorType; @@ -496,6 +497,10 @@ class ErrorMessage case ErrorType.OldMessages: embed.data.fields[0].value =ErrorType.OldMessages; break; + case ErrorType.FeatureInnactive: + embed.data.fields[0].value =ErrorType.FeatureInnactive; + embed.data.fields[1].value = 'If you are an admin you can activate the function on the dashboard. (rem.wordfights.com)' + break; default: break; } @@ -1246,41 +1251,62 @@ class Strikes }, 5*60*1000); //TODO: Handle old strikes - //* Strikes life time should be difined by guild - //* A default should be set - //? 15 days + /** + * db.strikes.createIndex({expireAt:1}, {expireAfterSeconds:0}) + * db.strikes.insertOne( + * {whatever data it takes, + * ! expireAt:new Date(new Date().getTime() + 60000) + * }) + * + * + */ } + async getAreStrikesActive(guildId) + { + const flag = await guildsM.find({gID:guildId}).then(g=>{return g.strikes}); + if(!flag) return false; + return true; + } async check() { - const Strikes = await strikesM.find().then(s=>{return s}); - //console.log(Strikes); - const guilds = await guildsM.find({strikes:true}).then(g=>{return g}); - if(!guilds) return console.log('Striker: No Guilds') - var guildIds = []; - for(var i = 0; i { - return guildIds.indexOf(e.guildID)>=0; - } - ) - guildIds.forEach(guild=> - { - filtered.forEach(strike=> - { - (async (guild, strike)=> - { - const ammount = await strikesM.find({guildID:guild, strokedID:strike.strokedID}).then(res=>{return res.length}); - if(ammount>2) - { - this.handleStroked(strike.strokedID, guild); - } - })(guild, strike) - }) - }) + + /** + * TODO: Grab validated strikes. + * TODO: Check if guild has strikes active. + * TODO: Handle player if strikes have reached the threshold + * TODO: X + * + */ + + // const Strikes = await strikesM.find().then(s=>{return s}); + // //console.log(Strikes); + // const guilds = await guildsM.find({strikes:true}).then(g=>{return g}); + // if(!guilds) return console.log('Striker: No Guilds') + // var guildIds = []; + // for(var i = 0; i { + // return guildIds.indexOf(e.guildID)>=0; + // } + // ) + // guildIds.forEach(guild=> + // { + // filtered.forEach(strike=> + // { + // (async (guild, strike)=> + // { + // const ammount = await strikesM.find({guildID:guild, strokedID:strike.strokedID}).then(res=>{return res.length}); + // if(ammount>2) + // { + // this.handleStroked(strike.strokedID, guild); + // } + // })(guild, strike) + // }) + // }) } handleStroked(user, guild) { @@ -1295,4 +1321,5 @@ class Strikes .then(() => console.log(`Successfully kicked ${user.user.username}`)) .catch(err => console.log(`Unable to kick ${user.user.username}: ${err}`)); } -} \ No newline at end of file +} +module.exports.Strikes = Strikes; diff --git a/models/strikes.js b/models/strikes.js index 1a8e5b0..4900635 100644 --- a/models/strikes.js +++ b/models/strikes.js @@ -10,6 +10,7 @@ new Schema( guildID:{type:String, required: true}, reason:{type:String, required:true}, date:{type:Date, required:true}, + expireAt:{type:Date, required:true} } );