Skip to content
Permalink
Browse files

update cache cleanup task

  • Loading branch information...
gc committed Jun 3, 2019
1 parent 1cd28b8 commit bad30b03ed87cb0c757c60081d6a4f6ec6332639
Showing with 96 additions and 34 deletions.
  1. +75 −34 bot/tasks/cachecleanup.js
  2. +21 −0 licenses/klasa-pieces.txt
@@ -1,16 +1,36 @@
// Thanks to https://github.com/kyranet
const { Task } = require('klasa');
const { util: { binaryToID } } = require('discord.js');
const { Task, Colors } = require('klasa');
const { SnowflakeUtil } = require('discord.js');

// THRESHOLD equals to 30 minutes in milliseconds:
// - 1000 milliseconds = 1 second
// - 60 seconds = 1 minute
// - 30 minutes
const THRESHOLD = 1000 * 60 * 30;
const EPOCH = 1420070400000;
const EMPTY = '0000100000000000000000';

module.exports = class extends Task {
module.exports = class MemorySweeper extends Task {

constructor(...args) {
super(...args);

// The colors to stylise the console's logs
this.colors = {
red: new Colors({ text: 'lightred' }),
yellow: new Colors({ text: 'lightyellow' }),
green: new Colors({ text: 'green' })
};

// The header with the console colors
this.header = new Colors({ text: 'lightblue' }).format('[CACHE CLEANUP]');
}

async run() {
const OLD_SNOWFLAKE = binaryToID(((Date.now() - THRESHOLD) - EPOCH).toString(2).padStart(42, '0') + EMPTY);
let presences = 0, guildMembers = 0, emojis = 0, lastMessages = 0, users = 0;
const OLD_SNOWFLAKE = SnowflakeUtil.generate(Date.now() - THRESHOLD);
let presences = 0,
guildMembers = 0,
voiceStates = 0,
emojis = 0,
lastMessages = 0,
users = 0;

// Per-Guild sweeper
for (const guild of this.client.guilds.values()) {
@@ -21,44 +41,65 @@ module.exports = class extends Task {
presences += guild.presences.size;
guild.presences.clear();

// Clear emojis
emojis += guild.emojis.size;
guild.emojis.clear();

// Clear members
// Clear members that haven't send a message in the last 30 minutes
const { me } = guild;
for (const [id, member] of guild.members) {
if (member === me) continue;
if (member.voice.channelID) continue;
if (member.lastMessageID && member.lastMessageID > OLD_SNOWFLAKE) continue;
guildMembers++;
voiceStates++;
guild.voiceStates.delete(id);
guild.members.delete(id);
}

// Per-User sweeper
for (const user of this.client.users.values()) {
if (user.settings.get('RSN')) continue;
if (Object.keys(user.settings.get('pets')).length > 0) continue;
if (user.lastMessageID && user.lastMessageID > OLD_SNOWFLAKE) continue;
this.client.users.delete(user.id);
users++;
}
// Clear emojis
emojis += guild.emojis.size;
guild.emojis.clear();
}

// Completely delete all voice channels, delete lastMessageID of channels
for (const channel of guild.channels.values()) {
if (channel.type === 'voice') {
guild.channels.delete(channel.id);
this.client.channels.delete(channel.id);
} else if (channel.type === 'text') {
if (!channel.lastMessageID) continue;
channel.lastMessageID = null;
lastMessages++;
}
}
// Per-Channel sweeper
for (const channel of this.client.channels.values()) {
if (!channel.lastMessageID) continue;
channel.lastMessageID = null;
lastMessages++;
}

// Per-User sweeper
for (const user of this.client.users.values()) {
if (user.settings.get('RSN')) continue;
if (Object.keys(user.settings.get('pets')).length > 0) continue;
if (user.lastMessageID && user.lastMessageID > OLD_SNOWFLAKE) continue;
this.client.users.delete(user.id);
users++;
}

// Emit a log
this.client.emit('verbose', `Cleared ${users} [Users]s | ${presences} [Presence]s | ` +
`${guildMembers} [GuildMember]s | ${emojis} [Emoji]s | ${lastMessages} [Last Message]s.`);
this.client.emit(
'verbose',
`${this.header} ${this.setColor(presences)} [Presence]s | ${this.setColor(guildMembers)} [GuildMember]s | ${this.setColor(voiceStates)} [VoiceState]s | ${this.setColor(
users
)} [User]s | ${this.setColor(emojis)} [Emoji]s | ${this.setColor(lastMessages)} [Last Message]s.`
);
}

/**
* Set a colour depending on the amount:
* > 1000 : Light Red colour
* > 100 : Light Yellow colour
* < 100 : Green colour
* @since 3.0.0
* @param {number} number The number to colourise
* @returns {string}
*/
setColor(number) {
const text = String(number).padStart(5, ' ');
// Light Red color
if (number > 1000) return this.colors.red.format(text);
// Light Yellow color
if (number > 100) return this.colors.yellow.format(text);
// Green color
return this.colors.green.format(text);
}

};
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017-2019 dirigeants

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit bad30b0

Please sign in to comment.
You can’t perform that action at this time.