From 021f33fc38807e1f1955b7fc7e9b33c7dcfcf690 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Wed, 22 Jun 2022 21:43:57 -0400 Subject: [PATCH] Started moving common log messages to one spot --- mod.ts | 12 +++--------- src/commands/apiCmd.ts | 9 +++------ src/commands/apiCmd/allowBlock.ts | 17 +++++------------ src/commands/apiCmd/apiHelp.ts | 5 ++--- src/commands/apiCmd/deleteGuild.ts | 9 +++------ src/commands/apiCmd/showHideWarn.ts | 17 +++++------------ src/commands/apiCmd/status.ts | 17 +++++------------ src/commands/audit.ts | 5 ++--- src/commands/auditCmd/auditDB.ts | 7 +++---- src/commands/auditCmd/auditGuilds.ts | 5 ++--- src/commands/auditCmd/auditHelp.ts | 5 ++--- src/commands/emoji.ts | 9 +++------ src/commands/handleMentions.ts | 5 ++--- src/commands/help.ts | 5 ++--- src/commands/info.ts | 5 ++--- src/commands/ping.ts | 3 ++- src/commands/privacy.ts | 5 ++--- src/commands/report.ts | 13 ++++--------- src/commands/rip.ts | 5 ++--- src/commands/roll.ts | 5 ++--- src/commands/rollHelp.ts | 5 ++--- src/commands/stats.ts | 7 +++---- src/commands/version.ts | 5 ++--- src/solver/rollQueue.ts | 9 +++------ src/utils.ts | 28 ++++++++++++++++++++++++---- 25 files changed, 90 insertions(+), 127 deletions(-) diff --git a/mod.ts b/mod.ts index e78a2f1..8c12fc9 100644 --- a/mod.ts +++ b/mod.ts @@ -85,22 +85,16 @@ startBot({ }], status: 'online', }); - sendMessage(config.logChannel, `${config.name} has started, running version ${config.version}.`).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`); - }); + sendMessage(config.logChannel, `${config.name} has started, running version ${config.version}.`).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:88', 'Startup', e)); }, 1000); }, guildCreate: (guild: DiscordenoGuild) => { log(LT.LOG, `Handling joining guild ${JSON.stringify(guild)}`); - sendMessage(config.logChannel, `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`); - }); + sendMessage(config.logChannel, `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:95', 'Join Guild', e)); }, guildDelete: (guild: DiscordenoGuild) => { log(LT.LOG, `Handling leaving guild ${JSON.stringify(guild)}`); - sendMessage(config.logChannel, `I have been removed from: ${guild.name} (id: ${guild.id}).`).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`); - }); + sendMessage(config.logChannel, `I have been removed from: ${guild.name} (id: ${guild.id}).`).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:99', 'Leave Guild', e)); dbClient.execute('DELETE FROM allowed_guilds WHERE guildid = ? AND banned = 0', [guild.id]).catch((e) => { log(LT.ERROR, `Failed to DELETE guild from DB: ${JSON.stringify(e)}`); }); diff --git a/src/commands/apiCmd.ts b/src/commands/apiCmd.ts index f54b4ee..f5d7fba 100644 --- a/src/commands/apiCmd.ts +++ b/src/commands/apiCmd.ts @@ -9,6 +9,7 @@ import { } from '../../deps.ts'; import apiCommands from './apiCmd/_index.ts'; import { failColor } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const api = async (message: DiscordenoMessage, args: string[]) => { // Light telemetry to see how many times a command is being run @@ -26,9 +27,7 @@ export const api = async (message: DiscordenoMessage, args: string[]) => { color: failColor, title: 'API commands are only available in guilds.', }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('apiCmd.ts:30', message, e)); return; } @@ -75,8 +74,6 @@ export const api = async (message: DiscordenoMessage, args: string[]) => { title: 'API commands are powerful and can only be used by guild Owners and Admins.', description: 'For information on how to use the API, please check the GitHub README for more information [here](https://github.com/Burn-E99/TheArtificer).', }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('apiCmd.ts:77', message, e)); } }; diff --git a/src/commands/apiCmd/allowBlock.ts b/src/commands/apiCmd/allowBlock.ts index 5910261..ee9cd1c 100644 --- a/src/commands/apiCmd/allowBlock.ts +++ b/src/commands/apiCmd/allowBlock.ts @@ -7,14 +7,13 @@ import { LT, } from '../../../deps.ts'; import { generateApiFailed, generateApiSuccess } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => { let errorOutInitial = false; const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); - message.send(generateApiFailed(apiArg)).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + message.send(generateApiFailed(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:16', message, e)); errorOutInitial = true; }); if (errorOutInitial) return; @@ -25,9 +24,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,active) values(?,?,?)`, [message.guildId, message.channelId, (apiArg === 'allow' || apiArg === 'enable') ? 1 : 0]).catch( (e0) => { log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`); - message.send(generateApiFailed(apiArg)).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + message.send(generateApiFailed(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:27', message, e)); errorOut = true; }, ); @@ -36,9 +33,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => await dbClient.execute(`UPDATE allowed_guilds SET active = ? WHERE guildid = ? AND channelid = ?`, [(apiArg === 'allow' || apiArg === 'enable') ? 1 : 0, message.guildId, message.channelId]).catch( (e0) => { log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`); - message.send(generateApiFailed(apiArg)).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + message.send(generateApiFailed(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:36', message, e)); errorOut = true; }, ); @@ -46,7 +41,5 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => if (errorOut) return; // We won't get here if there's any errors, so we know it has bee successful, so report as such - message.send(generateApiSuccess(`${apiArg}ed`)).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.send(generateApiSuccess(`${apiArg}ed`)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:44', message, e)); }; diff --git a/src/commands/apiCmd/apiHelp.ts b/src/commands/apiCmd/apiHelp.ts index ecdecf0..5beb615 100644 --- a/src/commands/apiCmd/apiHelp.ts +++ b/src/commands/apiCmd/apiHelp.ts @@ -7,6 +7,7 @@ import { LT, } from '../../../deps.ts'; import { infoColor1, infoColor2 } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const help = (message: DiscordenoMessage) => { message.send({ @@ -63,7 +64,5 @@ You may enable and disable the API rolls for your guild as needed.`, ], }, ], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('apiHelp.ts:67', message, e)); }; diff --git a/src/commands/apiCmd/deleteGuild.ts b/src/commands/apiCmd/deleteGuild.ts index 21f0185..904df44 100644 --- a/src/commands/apiCmd/deleteGuild.ts +++ b/src/commands/apiCmd/deleteGuild.ts @@ -7,6 +7,7 @@ import { LT, } from '../../../deps.ts'; import { failColor, successColor } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const deleteGuild = async (message: DiscordenoMessage) => { let errorOut = false; @@ -18,9 +19,7 @@ export const deleteGuild = async (message: DiscordenoMessage) => { title: 'Failed to delete this guild from the database.', description: 'If this issue persists, please report this to the developers.', }], - }).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('deleteGuild.ts:22', message, e)); errorOut = true; }); if (errorOut) return; @@ -31,7 +30,5 @@ export const deleteGuild = async (message: DiscordenoMessage) => { color: successColor, title: 'This guild\'s API setting has been removed from The Artifier\'s Database.', }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('deleteGuild.ts:33', message, e)); }; diff --git a/src/commands/apiCmd/showHideWarn.ts b/src/commands/apiCmd/showHideWarn.ts index 287223d..b0085e6 100644 --- a/src/commands/apiCmd/showHideWarn.ts +++ b/src/commands/apiCmd/showHideWarn.ts @@ -7,14 +7,13 @@ import { LT, } from '../../../deps.ts'; import { generateApiFailed, generateApiSuccess } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) => { let errorOutInitial = false; const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); - message.send(generateApiFailed(`${apiArg} on`)).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + message.send(generateApiFailed(`${apiArg} on`)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:16', message, e)); errorOutInitial = true; }); if (errorOutInitial) return; @@ -24,25 +23,19 @@ export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) = // Since guild is not in our DB, add it in await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,hidewarn) values(?,?,?)`, [message.guildId, message.channelId, (apiArg === 'hide-warn') ? 1 : 0]).catch((e0) => { log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`); - message.send(generateApiFailed(`${apiArg} on`)).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + message.send(generateApiFailed(`${apiArg} on`)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:26', message, e)); errorOut = true; }); } else { // Since guild is in our DB, update it await dbClient.execute(`UPDATE allowed_guilds SET hidewarn = ? WHERE guildid = ? AND channelid = ?`, [(apiArg === 'hide-warn') ? 1 : 0, message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`); - message.send(generateApiFailed(`${apiArg} on`)).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + message.send(generateApiFailed(`${apiArg} on`)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:33', message, e)); errorOut = true; }); } if (errorOut) return; // We won't get here if there's any errors, so we know it has bee successful, so report as such - message.send(generateApiSuccess(apiArg)).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.send(generateApiSuccess(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:40', message, e)); }; diff --git a/src/commands/apiCmd/status.ts b/src/commands/apiCmd/status.ts index 31787eb..2ed3856 100644 --- a/src/commands/apiCmd/status.ts +++ b/src/commands/apiCmd/status.ts @@ -7,6 +7,7 @@ import { LT, } from '../../../deps.ts'; import { failColor, generateApiStatus } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const status = async (message: DiscordenoMessage) => { // Get status of guild from the db @@ -19,9 +20,7 @@ export const status = async (message: DiscordenoMessage) => { title: 'Failed to check API rolls status for this guild.', description: 'If this issue persists, please report this to the developers.', }], - }).catch((e1) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('status.ts:23', message, e)); errorOut = true; }); if (errorOut) return; @@ -30,18 +29,12 @@ export const status = async (message: DiscordenoMessage) => { if (guildQuery.length > 0) { // Check if guild is banned from using API and return appropriate message if (guildQuery[0].banned) { - message.send(generateApiStatus(true, false)).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.send(generateApiStatus(true, false)).catch((e: Error) => utils.commonLoggers.messageSendError('status.ts:32', message, e)); } else { - message.send(generateApiStatus(false, guildQuery[0].active)).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.send(generateApiStatus(false, guildQuery[0].active)).catch((e: Error) => utils.commonLoggers.messageSendError('status.ts:34', message, e)); } } else { // Guild is not in DB, therefore they are blocked - message.send(generateApiStatus(false, false)).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.send(generateApiStatus(false, false)).catch((e: Error) => utils.commonLoggers.messageSendError('status.ts:38', message, e)); } }; diff --git a/src/commands/audit.ts b/src/commands/audit.ts index 1e295cd..637d636 100644 --- a/src/commands/audit.ts +++ b/src/commands/audit.ts @@ -9,6 +9,7 @@ import { } from '../../deps.ts'; import auditCommands from './auditCmd/_index.ts'; import { failColor } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const audit = async (message: DiscordenoMessage, args: string[]) => { // Light telemetry to see how many times a command is being run @@ -47,8 +48,6 @@ export const audit = async (message: DiscordenoMessage, args: string[]) => { color: failColor, title: `Audit commands are powerful and can only be used by ${config.name}'s owner.`, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('audit.ts:51', message, e)); } }; diff --git a/src/commands/auditCmd/auditDB.ts b/src/commands/auditCmd/auditDB.ts index 8f49ccc..fdc3a49 100644 --- a/src/commands/auditCmd/auditDB.ts +++ b/src/commands/auditCmd/auditDB.ts @@ -9,6 +9,7 @@ import { } from '../../../deps.ts'; import { infoColor2 } from '../../commandUtils.ts'; import { compilingStats } from '../../commonEmbeds.ts'; +import utils from '../../utils.ts'; export const auditDB = async (message: DiscordenoMessage) => { try { @@ -39,10 +40,8 @@ export const auditDB = async (message: DiscordenoMessage) => { timestamp: new Date().toISOString(), fields: embedFields, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('auditDB.ts:43', message, e)); } catch (e) { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + utils.commonLoggers.messageSendError('auditDB.ts:45', message, e) } }; diff --git a/src/commands/auditCmd/auditGuilds.ts b/src/commands/auditCmd/auditGuilds.ts index c80b9fe..3197f6b 100644 --- a/src/commands/auditCmd/auditGuilds.ts +++ b/src/commands/auditCmd/auditGuilds.ts @@ -6,6 +6,7 @@ import { LT, } from '../../../deps.ts'; import { infoColor2 } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const auditGuilds = (message: DiscordenoMessage) => { message.send({ @@ -15,7 +16,5 @@ export const auditGuilds = (message: DiscordenoMessage) => { description: 'WIP', timestamp: new Date().toISOString(), }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('auditGuild.ts:19', message, e)); }; diff --git a/src/commands/auditCmd/auditHelp.ts b/src/commands/auditCmd/auditHelp.ts index d75602e..d0cb950 100644 --- a/src/commands/auditCmd/auditHelp.ts +++ b/src/commands/auditCmd/auditHelp.ts @@ -7,6 +7,7 @@ import { LT, } from '../../../deps.ts'; import { infoColor1 } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; export const auditHelp = (message: DiscordenoMessage) => { message.send({ @@ -31,7 +32,5 @@ export const auditHelp = (message: DiscordenoMessage) => { }, ], }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('auditHelp.ts:35', message, e)); }; diff --git a/src/commands/emoji.ts b/src/commands/emoji.ts index 265eb3d..0cf6ebc 100644 --- a/src/commands/emoji.ts +++ b/src/commands/emoji.ts @@ -8,6 +8,7 @@ import { LT, } from '../../deps.ts'; import { EmojiConf } from '../mod.d.ts'; +import utils from '../utils.ts'; const allEmojiAliases: string[] = []; @@ -29,14 +30,10 @@ export const emoji = (message: DiscordenoMessage, command: string) => { }); // Send the needed emoji - message.send(`<${emji.animated ? 'a' : ''}:${emji.name}:${emji.id}>`).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.send(`<${emji.animated ? 'a' : ''}:${emji.name}:${emji.id}>`).catch((e: Error) => utils.commonLoggers.messageSendError('emoji.ts:33', message, e)); // And attempt to delete if needed if (emji.deleteSender) { - message.delete().catch((e) => { - log(LT.WARN, `Failed to delete message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + message.delete().catch((e: Error) => utils.commonLoggers.messageDeleteError('emoji.ts:36', message, e)); } return true; } diff --git a/src/commands/handleMentions.ts b/src/commands/handleMentions.ts index 5eb7a20..43f13b7 100644 --- a/src/commands/handleMentions.ts +++ b/src/commands/handleMentions.ts @@ -8,6 +8,7 @@ import { LT, } from '../../deps.ts'; import { infoColor1 } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const handleMentions = (message: DiscordenoMessage) => { log(LT.LOG, `Handling @mention message: ${JSON.stringify(message)}`); @@ -26,7 +27,5 @@ export const handleMentions = (message: DiscordenoMessage) => { value: `To learn about my available commands, please run \`${config.prefix}help\``, }], }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('handleMentions.ts:30', message, e)); }; diff --git a/src/commands/help.ts b/src/commands/help.ts index 3df8d78..e44d5a6 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -8,6 +8,7 @@ import { LT, } from '../../deps.ts'; import { infoColor2 } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const help = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -78,7 +79,5 @@ export const help = (message: DiscordenoMessage) => { }, ], }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('help.ts:82', message, e)); }; diff --git a/src/commands/info.ts b/src/commands/info.ts index 127df98..8bf6017 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -7,6 +7,7 @@ import { LT, } from '../../deps.ts'; import { infoColor2 } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const info = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -23,7 +24,5 @@ Additional information can be found on my website [here](https://discord.burne99 Want to check out my source code? Check it out [here](https://github.com/Burn-E99/TheArtificer). Need help with this bot? Join my support server [here](https://discord.gg/peHASXMZYv).`, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('info.ts:27', message, e)); }; diff --git a/src/commands/ping.ts b/src/commands/ping.ts index fd7e4c4..4fdb70b 100644 --- a/src/commands/ping.ts +++ b/src/commands/ping.ts @@ -7,6 +7,7 @@ import { LT, } from '../../deps.ts'; import { generatePing } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const ping = async (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -19,6 +20,6 @@ export const ping = async (message: DiscordenoMessage) => { const m = await message.send(generatePing(-1)); m.edit(generatePing(m.timestamp - message.timestamp)); } catch (e) { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + utils.commonLoggers.messageSendError('ping.ts:23', message, e); } }; diff --git a/src/commands/privacy.ts b/src/commands/privacy.ts index c889b24..aad3ca9 100644 --- a/src/commands/privacy.ts +++ b/src/commands/privacy.ts @@ -8,6 +8,7 @@ import { LT, } from '../../deps.ts'; import { infoColor1 } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const privacy = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -29,7 +30,5 @@ For more details, please check out the Privacy Policy on the GitHub [here](https Terms of Service can also be found on GitHub [here](https://github.com/Burn-E99/TheArtificer/blob/master/TERMS.md).`, }], }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('privacy.ts:33', message, e)); }; diff --git a/src/commands/report.ts b/src/commands/report.ts index d3638b1..ee891d4 100644 --- a/src/commands/report.ts +++ b/src/commands/report.ts @@ -10,6 +10,7 @@ import { sendMessage, } from '../../deps.ts'; import { failColor, generateReport, successColor } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const report = (message: DiscordenoMessage, args: string[]) => { // Light telemetry to see how many times a command is being run @@ -18,18 +19,14 @@ export const report = (message: DiscordenoMessage, args: string[]) => { }); if (args.join(' ')) { - sendMessage(config.reportChannel, generateReport(args.join(' '))).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + sendMessage(config.reportChannel, generateReport(args.join(' '))).catch((e: Error) => utils.commonLoggers.messageSendError('report.ts:22', message, e)); message.send({ embeds: [{ color: successColor, title: 'Failed command has been reported to my developer.', description: `For more in depth support, and information about planned maintenance, please join the support server [here](https://discord.gg/peHASXMZYv).`, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('report.ts:29', message, e)); } else { message.send({ embeds: [{ @@ -37,8 +34,6 @@ export const report = (message: DiscordenoMessage, args: string[]) => { title: 'Please provide a short description of what failed', description: 'Providing a short description helps my developer quickly diagnose what went wrong.', }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('report.ts:37', message, e)); } }; diff --git a/src/commands/rip.ts b/src/commands/rip.ts index 5e37422..1bc41c7 100644 --- a/src/commands/rip.ts +++ b/src/commands/rip.ts @@ -7,6 +7,7 @@ import { LT, } from '../../deps.ts'; import { infoColor2 } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const rip = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -22,7 +23,5 @@ export const rip = (message: DiscordenoMessage) => { December 21, 2020`, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('rip.ts:26', message, e)); }; diff --git a/src/commands/roll.ts b/src/commands/roll.ts index dddfb63..239c951 100644 --- a/src/commands/roll.ts +++ b/src/commands/roll.ts @@ -12,6 +12,7 @@ import { rollingEmbed, warnColor } from '../commandUtils.ts'; import rollFuncs from './roll/_index.ts'; import { queueRoll } from '../solver/rollQueue.ts'; import { QueuedRoll } from '../mod.d.ts'; +import utils from '../utils.ts'; export const roll = async (message: DiscordenoMessage, args: string[], command: string) => { // Light telemetry to see how many times a command is being run @@ -26,9 +27,7 @@ export const roll = async (message: DiscordenoMessage, args: string[], command: color: warnColor, title: 'Command is in development, please try again later.', }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('roll.ts:30', message, e)); return; } diff --git a/src/commands/rollHelp.ts b/src/commands/rollHelp.ts index d01a94e..6dc406b 100644 --- a/src/commands/rollHelp.ts +++ b/src/commands/rollHelp.ts @@ -8,6 +8,7 @@ import { LT, } from '../../deps.ts'; import { infoColor1, infoColor2, successColor } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const rollHelp = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -243,7 +244,5 @@ Available directions: ], }, ], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('rollHelp.ts:247', message, e)); }; diff --git a/src/commands/stats.ts b/src/commands/stats.ts index 3034b15..4c897db 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -10,6 +10,7 @@ import { } from '../../deps.ts'; import { generateStats } from '../commandUtils.ts'; import { compilingStats } from '../commonEmbeds.ts'; +import utils from '../utils.ts'; export const stats = async (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -33,10 +34,8 @@ export const stats = async (message: DiscordenoMessage) => { const cachedGuilds = await cacheHandlers.size('guilds'); const cachedChannels = await cacheHandlers.size('channels'); const cachedMembers = await cacheHandlers.size('members'); - m.edit(generateStats(cachedGuilds + cache.dispatchedGuildIds.size, cachedChannels + cache.dispatchedChannelIds.size, cachedMembers, rolls, total - rolls)).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + m.edit(generateStats(cachedGuilds + cache.dispatchedGuildIds.size, cachedChannels + cache.dispatchedChannelIds.size, cachedMembers, rolls, total - rolls)).catch((e: Error) => utils.commonLoggers.messageSendError('stats.ts:37', message, e)); } catch (e) { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + utils.commonLoggers.messageSendError('stats.ts:39', message, e); } }; diff --git a/src/commands/version.ts b/src/commands/version.ts index fdd6971..08ae361 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -8,6 +8,7 @@ import { LT, } from '../../deps.ts'; import { infoColor1 } from '../commandUtils.ts'; +import utils from '../utils.ts'; export const version = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run @@ -20,7 +21,5 @@ export const version = (message: DiscordenoMessage) => { color: infoColor1, title: `My current version is ${config.version}`, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('version.ts:24', message, e)); }; diff --git a/src/solver/rollQueue.ts b/src/solver/rollQueue.ts index b334752..32887bf 100644 --- a/src/solver/rollQueue.ts +++ b/src/solver/rollQueue.ts @@ -15,6 +15,7 @@ import { SolvedRoll } from '../solver/solver.d.ts'; import { QueuedRoll, RollModifiers } from '../mod.d.ts'; import { generateCountDetailsEmbed, generateDMFailed, generateRollEmbed, infoColor2, rollingEmbed } from '../commandUtils.ts'; import stdResp from '../endpoints/stdResponses.ts'; +import utils from '../utils.ts'; let currentWorkers = 0; const rollQueue: Array = []; @@ -193,9 +194,7 @@ export const queueRoll = async (rq: QueuedRoll) => { The results for this roll will replace this message when it is done.`, }], - }).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(rq.dd.m)} | ${JSON.stringify(e)}`); - }); + }).catch((e: Error) => utils.commonLoggers.messageSendError('rollQueue.ts:197', rq.dd.m, e)); rollQueue.push(rq); } }; @@ -206,9 +205,7 @@ setInterval(async () => { if (rollQueue.length && currentWorkers < config.limits.maxWorkers) { const temp = rollQueue.shift(); if (temp) { - temp.dd.m.edit(rollingEmbed).catch((e) => { - log(LT.ERROR, `Failed to send message: ${JSON.stringify(temp.dd.m)} | ${JSON.stringify(e)}`); - }); + temp.dd.m.edit(rollingEmbed).catch((e: Error) => utils.commonLoggers.messageSendError('rollQueue.ts:208', temp.dd.m, e)); handleRollWorker(temp); } } diff --git a/src/utils.ts b/src/utils.ts index ad7b493..b64ecff 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,6 +5,11 @@ */ import { + // Discordeno deps + DiscordenoMessage, + // Log4Deno deps + log, + LT, // Discordeno deps sendMessage, } from '../deps.ts'; @@ -74,9 +79,14 @@ const cmdPrompt = async (logChannel: bigint, botName: string): Promise => } // help or h // Shows a basic help menu else if (command === 'help' || command === 'h') { - console.log( - `${botName} CLI Help:\n\nAvailable Commands:\n exit - closes bot\n stop - closes the CLI\n m [ChannelID] [messgae] - sends message to specific ChannelID as the bot\n ml [message] sends a message to the specified botlog\n help - this message`, - ); + console.log(`${botName} CLI Help: + +Available Commands: + exit - closes bot + stop - closes the CLI + m [ChannelID] [messgae] - sends message to specific ChannelID as the bot + ml [message] sends a message to the specified botlog + help - this message`); } // Unhandled commands die here else { console.log('undefined command'); @@ -84,4 +94,14 @@ const cmdPrompt = async (logChannel: bigint, botName: string): Promise => } }; -export default { cmdPrompt }; +const genericLogger = (level: LT, message: string) => log(level, message); +const messageSendError = (location: string, message: DiscordenoMessage | string, err: Error) => genericLogger(LT.ERROR, `${location} | Failed to send message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message} `) +const messageDeleteError = (location: string, message: DiscordenoMessage | string, err: Error) => genericLogger(LT.ERROR, `${location} | Failed to delete message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message} `) + +export default { + commonLoggers: { + messageSendError, + messageDeleteError, + }, + cmdPrompt, +};