From e0706c632f7e1678545600a621065e5247d2ef0d Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:26:46 +1100 Subject: [PATCH] LevelSys self-destruction test --- src/models/dailyMsgs.ts | 2 +- src/models/userLevels.ts | 38 +++++++++++++++++--------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/models/dailyMsgs.ts b/src/models/dailyMsgs.ts index 78d3557..32b2f07 100644 --- a/src/models/dailyMsgs.ts +++ b/src/models/dailyMsgs.ts @@ -32,7 +32,7 @@ export class DailyMsgsSvc { }) this.model.sync(); } - nukeDays = async()=>await this.model.destroy(); + nukeDays = async()=>await this.model.truncate(); fetchDays = async()=>await this.model.findAll(); async newDay(formattedDate:number, total:number) { const [instance, created] = await this.model.findOrCreate({where: {day: formattedDate}, defaults: {total}}); diff --git a/src/models/userLevels.ts b/src/models/userLevels.ts index fa809cd..3d3f14d 100644 --- a/src/models/userLevels.ts +++ b/src/models/userLevels.ts @@ -72,10 +72,6 @@ export class UserLevelsSvc { return true; } } - async getActiveUsers() { - const members = await this.model.findAll({order: [['messages', 'DESC']], limit: 5}); - return members; - } async messageIncremental(userId:string) { const data = await this.model.findByPk(userId); if (data && data.dataValues.isBlocked) return; @@ -110,39 +106,39 @@ export class UserLevelsSvc { }) } async initSelfdestruct() { - // Every 1st of January at 11:00 (Midnight in London, Middayish in Sydney) + // Every 1st of January at 11:00 (London Time) cron.schedule('0 11 1 1 *', async()=>{ Logger.console('log', 'Cron', 'Running job "resetAllData", this is activated every 1st of January'); const performCountBeforeReset = await this.model.count(); - const topMembers = await this.getActiveUsers(); - + const topMembers = await this.model.findAll({order: [['messages', 'DESC']], limit: 5}); Logger.console('log', 'Cron:resetAllData', `Counted ${performCountBeforeReset.toLocaleString()} members before reset`); - await this.client.dailyMsgs.nukeDays(); - await this.model.truncate(); - + try { + await this.client.dailyMsgs.nukeDays(); + await this.model.truncate(); + // Send notification to dcServer's logs channel after cronjob is complete. (this.client.channels.resolve(this.client.config.dcServer.channels.bot_log) as Discord.TextChannel).send({embeds: [new this.client.embed() .setColor('#A3FFE3') .setTitle('Yearly data reset has begun!') .setDescription(MessageTool.concatMessage( 'I have gone ahead and reset everyone\'s rank data.', - `There was ${Intl.NumberFormat('en-US').format(performCountBeforeReset)} members in database before reset.\n`, + `There was **${Intl.NumberFormat('en-US').format(performCountBeforeReset)}** members in database before reset.\n`, 'Top 5 most active members:', ...topMembers.map((m,i)=>`${i+1}. **${(this.client.users.resolve(m.dataValues.id) ? this.client.users.resolve(m.dataValues.id).displayName : '*Unknown User*')}** - **${m.dataValues.messages.toLocaleString()}** messages`) )) ]}); - } catch (why) { - Logger.debug('Cron:resetAllData', why) - } - // Reset LRSstart to current Epoch and save it to config file - const newEpoch = new Date().getTime(); - if (this.client.config.LRSstart !== newEpoch) { - this.client.config.LRSstart = newEpoch; - Logger.console('log', 'Cron:resetAllData', `Resetting LRSstart to \`${newEpoch}\`, saved to config file`); - writeFileSync('src/config.json', JSON.stringify(this.client.config, null, 2)); - Logger.console('log', 'Cron:resetAllData', 'Job completed'); + // Reset LRSstart to current Epoch and save it to config file + const newEpoch = new Date().getTime(); + if (this.client.config.LRSstart !== newEpoch) { + this.client.config.LRSstart = newEpoch; + Logger.console('log', 'Cron:resetAllData', `Resetting LRSstart to \`${newEpoch}\`, saved to config file`); + writeFileSync('src/config.json', JSON.stringify(this.client.config, null, 2)); + Logger.console('log', 'Cron:resetAllData', 'Job completed'); + } + } catch (why) { + Logger.console('error', 'Cron:resetAllData', why) } }) }