From 6425e3930cf5b25199a927527f23a19e208b4db1 Mon Sep 17 00:00:00 2001 From: "Isaac (eartharoid)" Date: Sun, 23 Aug 2020 15:35:24 +0100 Subject: [PATCH] soon :tm: --- package-lock.json | 6 +++--- package.json | 2 +- src/commands/close.js | 2 +- src/commands/new.js | 2 +- src/events/message.js | 2 +- src/events/messageReactionAdd.js | 2 +- src/index.js | 27 ++++++++++++++++++++++++--- src/{utils => modules}/archive.js | 17 ++++++++++++++--- src/{utils => modules}/banner.js | 0 src/{utils => modules}/updater.js | 0 src/modules/utils.js | 18 ++++++++++++++++++ 11 files changed, 64 insertions(+), 14 deletions(-) rename src/{utils => modules}/archive.js (87%) rename src/{utils => modules}/banner.js (100%) rename src/{utils => modules}/updater.js (100%) create mode 100644 src/modules/utils.js diff --git a/package-lock.json b/package-lock.json index e33aeb4..ec81f1e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1452,9 +1452,9 @@ "integrity": "sha512-e6UVJ1fj8f2clpHy+KpXVWVxjzB3XYFGyKRJHDlT8Gy/75BT+9bYUacpHSCoXp7RTtyMSr4eBjZrp0nHyyQVbg==" }, "leekslazylogger": { - "version": "2.0.0-alpha.6", - "resolved": "https://registry.npmjs.org/leekslazylogger/-/leekslazylogger-2.0.0-alpha.6.tgz", - "integrity": "sha512-DqOSsd8r9ks9ddCTxI34p41SGCX8+QvVij8/NDNe//yTHo2+JlipWYfnUR2UNMvXTXaIAtGYguTxhMaU4ibs4w==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/leekslazylogger/-/leekslazylogger-2.0.0.tgz", + "integrity": "sha512-VKaf8RSyJSp8hbMqZ9nInLs29Ix7FbfdteT4yd2jWSz+A7E4eGKqyEG0r/usiAiEd7u0aX4IJv/8I6BEwrGYhw==", "requires": { "@eartharoid/dtf": "^1.0.7", "leeks.js": "0.0.9" diff --git a/package.json b/package.json index f74a1ef..27e4bb1 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "boxen": "^4.2.0", "discord.js": "^12.3.1", "dotenv": "^8.2.0", - "leekslazylogger": "^2.0.0-alpha.6", + "leekslazylogger": "^2.0.0", "line-reader": "^0.4.0", "node-fetch": "^2.6.0", "sequelize": "^6.3.4", diff --git a/src/commands/close.js b/src/commands/close.js index 010eb47..390bfb6 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -11,7 +11,7 @@ const log = new ChildLogger(); const { MessageEmbed } = require('discord.js'); const config = require('../../user/config'); const fs = require('fs'); -const archive = require('../utils/archive'); +const archive = require('../modules/archive'); module.exports = { name: 'close', diff --git a/src/commands/new.js b/src/commands/new.js index bba5420..8803b54 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -132,7 +132,7 @@ module.exports = { await m.delete(); }, 15000); - // require('../utils/archive').create(client, c); // create files + // require('../modules/archive').create(client, c); // create files let ping; switch (config.tickets.ping) { diff --git a/src/events/message.js b/src/events/message.js index 42cb941..7c6a15e 100644 --- a/src/events/message.js +++ b/src/events/message.js @@ -10,7 +10,7 @@ const { Collection, MessageEmbed } = require('discord.js'); const ChildLogger = require('leekslazylogger').ChildLogger; const log = new ChildLogger(); const config = require('../../user/config'); -const archive = require('../utils/archive'); +const archive = require('../modules/archive'); module.exports = { event: 'message', diff --git a/src/events/messageReactionAdd.js b/src/events/messageReactionAdd.js index fbc1aa9..8293086 100644 --- a/src/events/messageReactionAdd.js +++ b/src/events/messageReactionAdd.js @@ -134,7 +134,7 @@ module.exports = { } }); - // require('../utils/archive').create(client, c); // create files + // require('../modules/archive').create(client, c); // create files let ping; switch (config.tickets.ping) { diff --git a/src/index.js b/src/index.js index 16e3366..4fb3835 100644 --- a/src/index.js +++ b/src/index.js @@ -17,8 +17,8 @@ const client = new Discord.Client({ client.events = new Discord.Collection(); client.commands = new Discord.Collection(); client.cooldowns = new Discord.Collection(); - -require('./utils/banner')(leeks); // big coloured text thing +const utils = require('./modules/utils'); +require('./modules/banner')(leeks); // big coloured text thing const config = require('../user/config'); const Logger = require('leekslazylogger'); @@ -30,7 +30,7 @@ const log = new Logger({ }); log.multi(log); // required to allow other files to access the logger -require('./utils/updater')(); // check for updates +require('./modules/updater')(); // check for updates /** @@ -103,6 +103,27 @@ for (const file of commands) { log.info(`Loaded ${events.length} events and ${commands.length} commands`); +const one_day = 1000 * 60 * 60 * 24; +const txt = 'user/transcripts/text'; +const clean = () => { + const files = fs.readdirSync(txt).filter(file => file.endsWith('.txt')); + let total = 0; + for (const file of files) { + let diff = (new Date() - new Date(fs.statSync(`${txt}/${file}`).mtime)); + if (Math.floor(diff / one_day) > config.transcripts.text.keep_for) { + fs.unlinkSync(`${txt}/${file}`); + total++; + } + } + if (total > 0) + log.info(`Deleted ${total} old text ${utils.plural('transcript', total)}`); +}; + +if (config.transcripts.text.enabled) { + clean(); + setInterval(clean, one_day); +} + process.on('unhandledRejection', error => { log.warn('An error was not caught'); log.warn(`Uncaught ${error.name}: ${error.message}`); diff --git a/src/utils/archive.js b/src/modules/archive.js similarity index 87% rename from src/utils/archive.js rename to src/modules/archive.js index e84e6ba..df3c5c0 100644 --- a/src/utils/archive.js +++ b/src/modules/archive.js @@ -68,13 +68,23 @@ module.exports.add = (client, message) => { }; } - message.mentions.channels.each(c => data.entities.channels[c.id] = { + // mentions.users + message.mentions.members.each(m => data.entities.users[m.id] = { // for mentions + avatar: m.user.avatarURL(), + username: m.user.username, + discriminator: m.user.discriminator, + displayName: m.user.displayName, + color: m.displayColor, + badge: m.user.bot ? 'bot' : null + }); + + message.mentions.channels.each(c => data.entities.channels[c.id] = { // for mentions only name: c.name }); - message.mentions.roles.each(r => data.entities.roles[r.id] = { + message.mentions.roles.each(r => data.entities.roles[r.id] = { // for mentions only name: r.name, - color: r.color + color: r.color === 0 ? 7506394 : r.color }); fs.writeFileSync(json, JSON.stringify(data)); @@ -106,6 +116,7 @@ module.exports.export = (client, channel) => new Promise((resolve, reject) => { }, () => { // fs.writeFileSync('user/data.json', JSON.stringify(data)); // FOR TESTING // post(data).then() + // delete raw .json and .log resolve(config.transcripts.web.server); // json.url }); }); diff --git a/src/utils/banner.js b/src/modules/banner.js similarity index 100% rename from src/utils/banner.js rename to src/modules/banner.js diff --git a/src/utils/updater.js b/src/modules/updater.js similarity index 100% rename from src/utils/updater.js rename to src/modules/updater.js diff --git a/src/modules/utils.js b/src/modules/utils.js new file mode 100644 index 0000000..d86de48 --- /dev/null +++ b/src/modules/utils.js @@ -0,0 +1,18 @@ +/** + * + * @name DiscordTickets + * @author eartharoid + * @license GNU-GPLv3 + * + */ + +module.exports = { + /** + * @description Appends 's' to a word if plural number + * @param {string} word - singular version of word + * @param {number} num - integer + */ + plural(word, num) { + return num !== 1 ? word + 's' : word; + } +}; \ No newline at end of file