From c630dd6da70808fa4381e1c4c65f5e056a125ad6 Mon Sep 17 00:00:00 2001 From: David Ralph Date: Mon, 8 Feb 2021 18:18:58 +0000 Subject: [PATCH] Only create a logger instance once --- .gitignore | 1 + package.json | 2 +- src/commands/add.js | 4 +--- src/commands/close.js | 4 +--- src/commands/closeall.js | 4 +--- src/commands/delete.js | 4 +--- src/commands/help.js | 4 +--- src/commands/new.js | 4 +--- src/commands/panel.js | 4 +--- src/commands/remove.js | 4 +--- src/events/debug.js | 5 +---- src/events/error.js | 5 +---- src/events/message.js | 6 ++---- src/events/messageDelete.js | 4 +--- src/events/messageReactionAdd.js | 4 +--- src/events/messageUpdate.js | 4 +--- src/events/rateLimit.js | 5 +---- src/events/ready.js | 2 +- src/events/warn.js | 5 +---- src/index.js | 6 ++++-- 20 files changed, 24 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index e800978..7a35e15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ logs/ +.vscode/ node_modules/ user/dev.env user/dev.config.js diff --git a/package.json b/package.json index 92776e7..f72174e 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "mysql2": "2.x", "mariadb": "2.x", "pg": "8.x", - "tedious": "9.x" + "tedious": "11.x" }, "devDependencies": { "eslint": "^7.19.0", diff --git a/src/commands/add.js b/src/commands/add.js index 22d7d1c..53aa84b 100644 --- a/src/commands/add.js +++ b/src/commands/add.js @@ -7,8 +7,6 @@ */ const { MessageEmbed } = require('discord.js'); -const Logger = require('leekslazylogger'); -const log = new Logger(); module.exports = { name: 'add', @@ -17,7 +15,7 @@ module.exports = { aliases: ['none'], example: 'add @member to #ticket-23', args: true, - async execute(client, message, args, {config, Ticket}) { + async execute(client, message, args, log, {config, Ticket}) { const guild = client.guilds.cache.get(config.guild); const notTicket = new MessageEmbed() diff --git a/src/commands/close.js b/src/commands/close.js index 79ecdc8..d42fe6a 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -6,8 +6,6 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); const { join } = require('path'); @@ -20,7 +18,7 @@ module.exports = { aliases: ['none'], example: 'close #ticket-17', args: false, - async execute(client, message, args, { config, Ticket }) { + async execute(client, message, _args, log, { config, Ticket }) { const guild = client.guilds.cache.get(config.guild); const notTicket = new MessageEmbed() diff --git a/src/commands/closeall.js b/src/commands/closeall.js index 25f4caf..4091902 100644 --- a/src/commands/closeall.js +++ b/src/commands/closeall.js @@ -5,8 +5,6 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); const { join } = require('path'); @@ -26,7 +24,7 @@ module.exports = { example: 'closeall 1mo 1w', args: false, disabled: !config.commands.closeall.enabled, - async execute(client, message, args, { + async execute(client, message, args, log, { config, Ticket }) { diff --git a/src/commands/delete.js b/src/commands/delete.js index a5347cd..681d50c 100644 --- a/src/commands/delete.js +++ b/src/commands/delete.js @@ -6,8 +6,6 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); @@ -21,7 +19,7 @@ module.exports = { aliases: ['del'], example: 'delete #ticket-17', args: false, - async execute(client, message, args, { + async execute(client, message, _args, log, { config, Ticket }) { diff --git a/src/commands/help.js b/src/commands/help.js index ff27d12..2853fd4 100644 --- a/src/commands/help.js +++ b/src/commands/help.js @@ -6,8 +6,6 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); module.exports = { @@ -17,7 +15,7 @@ module.exports = { aliases: ['command', 'commands'], example: 'help new', args: false, - execute(client, message, args, {config}) { + execute(client, message, args, log, {config}) { const guild = client.guilds.cache.get(config.guild); const commands = Array.from(client.commands.values()); diff --git a/src/commands/new.js b/src/commands/new.js index b3170d3..801f8b1 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -6,8 +6,6 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); const { join } = require('path'); @@ -21,7 +19,7 @@ module.exports = { example: 'new my server won\'t start', args: false, disabled: !config.commands.new.enabled, - async execute(client, message, args, {config, Ticket}) { + async execute(client, message, args, log, {config, Ticket}) { if (!config.commands.new.enabled) return; // stop if the command is disabled diff --git a/src/commands/panel.js b/src/commands/panel.js index f7ee9bb..f31a07a 100644 --- a/src/commands/panel.js +++ b/src/commands/panel.js @@ -6,8 +6,6 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); module.exports = { @@ -17,7 +15,7 @@ module.exports = { aliases: ['widget'], args: false, permission: 'MANAGE_GUILD', - async execute(client, message, _args, {config, Setting}) { + async execute(client, message, _args, log, {config, Setting}) { const guild = client.guilds.cache.get(config.guild); let msgID = await Setting.findOne({ where: { key: 'panel_msg_id' } }); diff --git a/src/commands/remove.js b/src/commands/remove.js index f59e033..baae520 100644 --- a/src/commands/remove.js +++ b/src/commands/remove.js @@ -7,8 +7,6 @@ */ const { MessageEmbed } = require('discord.js'); -const Logger = require('leekslazylogger'); -const log = new Logger(); module.exports = { name: 'remove', @@ -17,7 +15,7 @@ module.exports = { aliases: ['none'], example: 'remove @member from #ticket-23', args: true, - async execute(client, message, args, {config, Ticket}) { + async execute(client, message, args, log, {config, Ticket}) { const guild = client.guilds.cache.get(config.guild); const notTicket = new MessageEmbed() diff --git a/src/events/debug.js b/src/events/debug.js index 584e856..04bb2b6 100644 --- a/src/events/debug.js +++ b/src/events/debug.js @@ -6,12 +6,9 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); - module.exports = { event: 'debug', - execute(_client, [e]) { + execute(_client, log, [e]) { log.debug(e); } }; \ No newline at end of file diff --git a/src/events/error.js b/src/events/error.js index 4147598..53f68f6 100644 --- a/src/events/error.js +++ b/src/events/error.js @@ -6,12 +6,9 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); - module.exports = { event: 'error', - execute(_client, [e]) { + execute(_client, log, [e]) { log.error(e); } }; \ No newline at end of file diff --git a/src/events/message.js b/src/events/message.js index 58bc428..8d5dbfd 100644 --- a/src/events/message.js +++ b/src/events/message.js @@ -7,13 +7,11 @@ */ const { Collection, MessageEmbed } = require('discord.js'); -const Logger = require('leekslazylogger'); -const log = new Logger(); const archive = require('../modules/archive'); module.exports = { event: 'message', - async execute(client, [message], {config, Ticket, Setting}) { + async execute(client, log, [message], {config, Ticket, Setting}) { const guild = client.guilds.cache.get(config.guild); @@ -104,7 +102,7 @@ Type \`${config.prefix}new\` on the server to create a new ticket.`); setTimeout(() => timestamps.delete(message.author.id), cooldownAmount); try { - command.execute(client, message, args, {config, Ticket, Setting}); + command.execute(client, message, args, log, {config, Ticket, Setting}); log.console(`${message.author.tag} used the '${command.name}' command`); } catch (error) { log.warn(`An error occurred whilst executing the '${command.name}' command`); diff --git a/src/events/messageDelete.js b/src/events/messageDelete.js index 24b0878..1e35e63 100644 --- a/src/events/messageDelete.js +++ b/src/events/messageDelete.js @@ -6,14 +6,12 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const fs = require('fs'); const { join } = require('path'); module.exports = { event: 'messageDelete', - async execute(_client, [message], {config, Ticket}) { + async execute(_client, log, [message], {config, Ticket}) { if (!config.transcripts.web.enabled) return; if (message.partial) { diff --git a/src/events/messageReactionAdd.js b/src/events/messageReactionAdd.js index 3ca336a..d69c961 100644 --- a/src/events/messageReactionAdd.js +++ b/src/events/messageReactionAdd.js @@ -6,15 +6,13 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); const { join } = require('path'); module.exports = { event: 'messageReactionAdd', - async execute(client, [r, u], {config, Ticket, Setting}) { + async execute(client, log, [r, u], {config, Ticket, Setting}) { if (r.partial) { try { await r.fetch(); diff --git a/src/events/messageUpdate.js b/src/events/messageUpdate.js index 4a54d5c..5ec7d4a 100644 --- a/src/events/messageUpdate.js +++ b/src/events/messageUpdate.js @@ -6,14 +6,12 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); const fs = require('fs'); const { join } = require('path'); module.exports = { event: 'messageUpdate', - async execute(_client, [o, n], {config, Ticket}) { + async execute(_client, log, [o, n], {config, Ticket}) { if (!config.transcripts.web.enabled) return; if (o.partial) { diff --git a/src/events/rateLimit.js b/src/events/rateLimit.js index afb50fc..4ef9107 100644 --- a/src/events/rateLimit.js +++ b/src/events/rateLimit.js @@ -6,12 +6,9 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); - module.exports = { event: 'rateLimit', - execute(_client, [limit]) { + execute(_client, log, [limit]) { log.warn('Rate-limited! (Enable debug mode in config for details)'); log.debug(limit); } diff --git a/src/events/ready.js b/src/events/ready.js index 32f1828..d1afb17 100644 --- a/src/events/ready.js +++ b/src/events/ready.js @@ -12,7 +12,7 @@ const config = require('../../user/' + require('../').config); module.exports = { event: 'ready', - execute(client) { + execute(client, log) { log.success(`Authenticated as ${client.user.tag}`); const updatePresence = () => { diff --git a/src/events/warn.js b/src/events/warn.js index 2205b04..e442402 100644 --- a/src/events/warn.js +++ b/src/events/warn.js @@ -6,12 +6,9 @@ * */ -const Logger = require('leekslazylogger'); -const log = new Logger(); - module.exports = { event: 'warn', - execute(_client, [e]) { + execute(_client, log, [e]) { log.warn(e); } }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index a0f3ebc..5bb5530 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ */ const version = Number(process.version.split('.')[0].replace('v', '')); -if (!version === 12 || !version > 12) return console.log('Please upgrade to Node v12 or higher'); +if (version < 12) return console.log('Please upgrade to Node v12 or higher'); const fs = require('fs'); const { join } = require('path'); @@ -29,9 +29,11 @@ const client = new Discord.Client({ autoReconnect: true, partials: ['MESSAGE', 'CHANNEL', 'REACTION'], }); + client.events = new Discord.Collection(); client.commands = new Discord.Collection(); client.cooldowns = new Discord.Collection(); + const utils = require('./modules/utils'); const leeks = require('leeks.js'); @@ -127,7 +129,7 @@ for (const file of events) { const event = require(`./events/${file}`); client.events.set(event.event, event); // client.on(event.event, e => client.events.get(event.event).execute(client, e, Ticket, Setting)); - client.on(event.event, (e1, e2) => client.events.get(event.event).execute(client, [e1, e2], {config, Ticket, Setting})); + client.on(event.event, (e1, e2) => client.events.get(event.event).execute(client, log, [e1, e2], {config, Ticket, Setting})); log.console(log.format(`> Loaded &7${event.event}&f event`)); }