From 57fc1e3ee82ac3f3a730be3ddd48ffacbb5cb7f0 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sat, 26 Aug 2023 14:39:33 +0200 Subject: [PATCH] fix: updated logging for validators --- src/commands/info/help.js | 2 +- src/commands/info/status.js | 2 +- src/commands/player/filters.js | 4 +- src/commands/player/loop.js | 2 +- src/commands/player/lyrics.js | 4 +- src/commands/player/nowplaying.js | 4 +- src/commands/player/pause.js | 4 +- src/commands/player/play.js | 4 +- src/commands/player/remove.js | 2 +- src/commands/player/seek.js | 4 +- src/commands/player/shuffle.js | 4 +- src/commands/player/skip.js | 4 +- src/commands/player/volume.js | 2 +- src/commands/system/guilds.js | 4 +- src/commands/system/reload.js | 4 +- src/commands/system/shards.js | 4 +- src/commands/system/systemstatus.js | 4 +- src/events/interactions/interactionCreate.js | 23 +++------- src/utils/validation/permissionValidator.js | 33 +++++++++++--- src/utils/validation/queueValidator.js | 43 ++++++++++++++----- src/utils/validation/searchQueryValidator.js | 13 ++++-- .../validation/systemCommandValidator.js | 14 +++++- src/utils/validation/voiceChannelValidator.js | 4 +- 23 files changed, 118 insertions(+), 70 deletions(-) diff --git a/src/commands/info/help.js b/src/commands/info/help.js index 69bfdf8..0690d6a 100644 --- a/src/commands/info/help.js +++ b/src/commands/info/help.js @@ -9,7 +9,7 @@ module.exports = { .setDescription('Show a list of commands and their usage.') .setDMPermission(false) .setNSFW(false), - execute: async ({ interaction, client }) => { + execute: async ({ interaction, client, executionId }) => { const commandList = client.commands .filter((command) => { // don't include system commands diff --git a/src/commands/info/status.js b/src/commands/info/status.js index ecb5cc8..c2e3617 100644 --- a/src/commands/info/status.js +++ b/src/commands/info/status.js @@ -14,7 +14,7 @@ module.exports = { .setDescription('Show the bot and system status.') .setDMPermission(false) .setNSFW(false), - execute: async ({ interaction, client }) => { + execute: async ({ interaction, client, executionId }) => { const uptimeString = await getUptimeFormatted(); const usedMemoryInMB = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US'); const cpuUsage = await osu.cpu.usage(); diff --git a/src/commands/player/filters.js b/src/commands/player/filters.js index ab33cc9..bd08533 100644 --- a/src/commands/player/filters.js +++ b/src/commands/player/filters.js @@ -29,7 +29,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -37,7 +37,7 @@ module.exports = { return; } - if (await queueNoCurrentTrack(interaction, queue)) { + if (await queueNoCurrentTrack({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/loop.js b/src/commands/player/loop.js index d64cb27..ba6e8d7 100644 --- a/src/commands/player/loop.js +++ b/src/commands/player/loop.js @@ -34,7 +34,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/lyrics.js b/src/commands/player/lyrics.js index d9eb83e..5fc0252 100644 --- a/src/commands/player/lyrics.js +++ b/src/commands/player/lyrics.js @@ -96,7 +96,7 @@ module.exports = { return; } - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -104,7 +104,7 @@ module.exports = { return; } - if (await queueNoCurrentTrack(interaction, queue)) { + if (await queueNoCurrentTrack({ interaction, queue, executionId })) { return; } geniusSearchQuery = queue.currentTrack.title.slice(0, 50); diff --git a/src/commands/player/nowplaying.js b/src/commands/player/nowplaying.js index 94f26a3..a235300 100644 --- a/src/commands/player/nowplaying.js +++ b/src/commands/player/nowplaying.js @@ -22,7 +22,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -30,7 +30,7 @@ module.exports = { return; } - if (await queueNoCurrentTrack(interaction, queue)) { + if (await queueNoCurrentTrack({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/pause.js b/src/commands/player/pause.js index 21fe770..d27ede8 100644 --- a/src/commands/player/pause.js +++ b/src/commands/player/pause.js @@ -21,7 +21,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -29,7 +29,7 @@ module.exports = { return; } - if (await queueNoCurrentTrack(interaction, queue)) { + if (await queueNoCurrentTrack({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/play.js b/src/commands/player/play.js index d6c6361..a18961c 100644 --- a/src/commands/player/play.js +++ b/src/commands/player/play.js @@ -95,7 +95,7 @@ module.exports = { return; } - if (await cannotJoinVoiceOrTalk(interaction)) { + if (await cannotJoinVoiceOrTalk({ interaction, executionId })) { return; } @@ -107,7 +107,7 @@ module.exports = { const player = useMainPlayer(); const query = interaction.options.getString('query'); - const transformedQuery = await transformQuery(query); + const transformedQuery = await transformQuery({ query, executionId }); let searchResult; diff --git a/src/commands/player/remove.js b/src/commands/player/remove.js index 9e30721..82bff3c 100644 --- a/src/commands/player/remove.js +++ b/src/commands/player/remove.js @@ -28,7 +28,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/seek.js b/src/commands/player/seek.js index 5d29e06..dac1f34 100644 --- a/src/commands/player/seek.js +++ b/src/commands/player/seek.js @@ -27,7 +27,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -35,7 +35,7 @@ module.exports = { return; } - if (await queueNoCurrentTrack(interaction, queue)) { + if (await queueNoCurrentTrack({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/shuffle.js b/src/commands/player/shuffle.js index 61be593..9641ffb 100644 --- a/src/commands/player/shuffle.js +++ b/src/commands/player/shuffle.js @@ -21,7 +21,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -29,7 +29,7 @@ module.exports = { return; } - if (await queueIsEmpty(interaction, queue)) { + if (await queueIsEmpty({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/skip.js b/src/commands/player/skip.js index a5ccf12..3b88137 100644 --- a/src/commands/player/skip.js +++ b/src/commands/player/skip.js @@ -24,7 +24,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } @@ -32,7 +32,7 @@ module.exports = { return; } - if (await queueNoCurrentTrack(interaction, queue)) { + if (await queueNoCurrentTrack({ interaction, queue, executionId })) { return; } diff --git a/src/commands/player/volume.js b/src/commands/player/volume.js index 3543d5a..d9ff112 100644 --- a/src/commands/player/volume.js +++ b/src/commands/player/volume.js @@ -28,7 +28,7 @@ module.exports = { const queue = useQueue(interaction.guild.id); - if (await queueDoesNotExist(interaction, queue)) { + if (await queueDoesNotExist({ interaction, queue, executionId })) { return; } diff --git a/src/commands/system/guilds.js b/src/commands/system/guilds.js index 1f900f4..274c938 100644 --- a/src/commands/system/guilds.js +++ b/src/commands/system/guilds.js @@ -13,8 +13,8 @@ module.exports = { .setDescription('Show list of guilds where bot is added.') .setDMPermission(false) .setNSFW(false), - execute: async ({ interaction, client }) => { - if (await notValidGuildId(interaction)) { + execute: async ({ interaction, client, executionId }) => { + if (await notValidGuildId({ interaction, executionId })) { logger.debug('Not a valid guild id.'); return; } diff --git a/src/commands/system/reload.js b/src/commands/system/reload.js index fffa710..46d7072 100644 --- a/src/commands/system/reload.js +++ b/src/commands/system/reload.js @@ -13,8 +13,8 @@ module.exports = { .setDescription('Reload the bot commands.') .setDMPermission(false) .setNSFW(false), - execute: async ({ interaction, client }) => { - if (await notValidGuildId(interaction)) { + execute: async ({ interaction, client, executionId }) => { + if (await notValidGuildId({ interaction, executionId })) { logger.debug('Not a valid guild id.'); return; } diff --git a/src/commands/system/shards.js b/src/commands/system/shards.js index e4be91c..40af199 100644 --- a/src/commands/system/shards.js +++ b/src/commands/system/shards.js @@ -30,8 +30,8 @@ module.exports = { ) .addNumberOption((option) => option.setName('page').setDescription('Page number to show').setMinValue(1)), - execute: async ({ interaction, client }) => { - if (await notValidGuildId(interaction)) { + execute: async ({ interaction, client, executionId }) => { + if (await notValidGuildId({ interaction, executionId })) { return; } diff --git a/src/commands/system/systemstatus.js b/src/commands/system/systemstatus.js index b27f7e2..37e81a9 100644 --- a/src/commands/system/systemstatus.js +++ b/src/commands/system/systemstatus.js @@ -16,8 +16,8 @@ module.exports = { .setDescription('Show the bot and system status.') .setDMPermission(false) .setNSFW(false), - execute: async ({ interaction, client }) => { - if (await notValidGuildId(interaction)) { + execute: async ({ interaction, client, executionId }) => { + if (await notValidGuildId({ interaction, executionId })) { return; } diff --git a/src/events/interactions/interactionCreate.js b/src/events/interactions/interactionCreate.js index 26e9141..cd3cfb0 100644 --- a/src/events/interactions/interactionCreate.js +++ b/src/events/interactions/interactionCreate.js @@ -59,7 +59,7 @@ module.exports = { try { await interaction.deferReply(); - if (await cannotSendMessageInChannel(interaction)) { + if (await cannotSendMessageInChannel({ interaction, executionId })) { return; } @@ -69,9 +69,7 @@ module.exports = { const outputTime = new Date(); const executionTime = outputTime - inputTime; - logger.info( - `Command '${interaction}' executed in ${executionTime} ms.` - ); + logger.info(`Command '${interaction}' executed in ${executionTime} ms.`); } catch (error) { logger.warn( error, @@ -86,9 +84,7 @@ module.exports = { return; } else { if (!interaction.deferred || !interaction.replied) { - logger.warn( - 'Interaction was not deferred or replied to, and an error was thrown.' - ); + logger.warn('Interaction was not deferred or replied to, and an error was thrown.'); return; } @@ -96,9 +92,7 @@ module.exports = { // If the interaction has already been replied to, most likely command executed successfully or error is already handled. return; } else if (interaction.deferred) { - logger.info( - 'Interaction was deferred, editing reply and sending Uh-oh message.' - ); + logger.info('Interaction was deferred, editing reply and sending Uh-oh message.'); await interaction.editReply({ embeds: [ new EmbedBuilder() @@ -109,9 +103,7 @@ module.exports = { ] }); } else { - logger.info( - 'Interaction was not deferred or replied, sending new reply with Uh-oh message.' - ); + logger.info('Interaction was not deferred or replied, sending new reply with Uh-oh message.'); await interaction.reply({ embeds: [ new EmbedBuilder() @@ -125,10 +117,7 @@ module.exports = { } } } else { - logger.warn( - interaction, - 'Interaction created but was not a chat input or autocomplete interaction.' - ); + logger.warn(interaction, 'Interaction created but was not a chat input or autocomplete interaction.'); } } }; diff --git a/src/utils/validation/permissionValidator.js b/src/utils/validation/permissionValidator.js index bac4032..8bd2588 100644 --- a/src/utils/validation/permissionValidator.js +++ b/src/utils/validation/permissionValidator.js @@ -1,9 +1,17 @@ -const logger = require('../../services/logger'); const config = require('config'); const embedOptions = config.get('embedOptions'); const { EmbedBuilder } = require('discord.js'); -exports.cannotJoinVoiceOrTalk = async (interaction) => { +exports.cannotJoinVoiceOrTalk = async ({ interaction, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'permissionValidator.js', + module: 'validator', + name: 'cannotJoinVoiceOrTalk', + executionId: executionId, + shardId: interaction.guild.shardId, + guildId: interaction.guild.id + }); + const channel = interaction.member.voice.channel; if (!channel.joinable || !channel.speakable) { @@ -18,7 +26,7 @@ exports.cannotJoinVoiceOrTalk = async (interaction) => { }); logger.debug( - `User tried to use command ${interaction.commandName} but the bot had no permission to join/speak in the voice channel.` + `User tried to use command '${interaction.commandName}' but the bot had no permission to join/speak in the voice channel.` ); return true; } @@ -26,13 +34,22 @@ exports.cannotJoinVoiceOrTalk = async (interaction) => { return false; }; -exports.cannotSendMessageInChannel = async (interaction) => { +exports.cannotSendMessageInChannel = async ({ interaction, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'permissionValidator.js', + module: 'validator', + name: 'cannotSendMessageInChannel', + executionId: executionId, + shardId: interaction.guild.shardId, + guildId: interaction.guild.id + }); + const channel = interaction.channel; // only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable if (!channel.viewable) { logger.info( - `User tried to use command ${interaction.commandName} but the bot had no permission to send reply in text channel.` + `User tried to use command '${interaction.commandName}' but the bot had no permission to send reply in text channel.` ); try { @@ -51,8 +68,10 @@ exports.cannotSendMessageInChannel = async (interaction) => { ephemeral: true }); } catch (error) { - if(error.message == 'The reply to this interaction has already been sent or deferred.') { - logger.warn('Error while sending ephemereal message about insufficient permissions to send message in channel.'); + if (error.message == 'The reply to this interaction has already been sent or deferred.') { + logger.warn( + 'Error while sending ephemereal message about insufficient permissions to send message in channel.' + ); logger.debug(error); } else { logger.error( diff --git a/src/utils/validation/queueValidator.js b/src/utils/validation/queueValidator.js index 9ca8ee0..b8358fd 100644 --- a/src/utils/validation/queueValidator.js +++ b/src/utils/validation/queueValidator.js @@ -1,9 +1,17 @@ -const logger = require('../../services/logger'); const config = require('config'); const embedOptions = config.get('embedOptions'); const { EmbedBuilder } = require('discord.js'); -exports.queueDoesNotExist = async (interaction, queue) => { +exports.queueDoesNotExist = async ({ interaction, queue, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'queueValidator.js', + module: 'validator', + name: 'queueDoesNotExist', + executionId: executionId, + shardId: interaction.guild.shardId, + guildId: interaction.guild.id + }); + if (!queue) { await interaction.editReply({ embeds: [ @@ -15,15 +23,23 @@ exports.queueDoesNotExist = async (interaction, queue) => { ] }); - logger.debug(`User tried to use command ${interaction.commandName} but there was no queue.`); - + logger.debug(`User tried to use command '${interaction.commandName}' but there was no queue.`); return true; } return false; }; -exports.queueNoCurrentTrack = async (interaction, queue) => { +exports.queueNoCurrentTrack = async ({ interaction, queue, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'queueValidator.js', + module: 'validator', + name: 'queueNoCurrentTrack', + executionId: executionId, + shardId: interaction.guild.shardId, + guildId: interaction.guild.id + }); + if (!queue.currentTrack) { await interaction.editReply({ embeds: [ @@ -35,15 +51,23 @@ exports.queueNoCurrentTrack = async (interaction, queue) => { ] }); - logger.debug(`User tried to use command ${interaction.commandName} but there was no current track.`); - + logger.debug(`User tried to use command '${interaction.commandName}' but there was no current track.`); return true; } return false; }; -exports.queueIsEmpty = async (interaction, queue) => { +exports.queueIsEmpty = async ({ interaction, queue, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'queueValidator.js', + module: 'validator', + name: 'queueIsEmpty', + executionId: executionId, + shardId: interaction.guild.shardId, + guildId: interaction.guild.id + }); + if (queue.tracks.data.length === 0) { await interaction.editReply({ embeds: [ @@ -55,8 +79,7 @@ exports.queueIsEmpty = async (interaction, queue) => { ] }); - logger.debug(`User tried to use command ${interaction.commandName} but there was no tracks in the queue.`); - + logger.debug(`User tried to use command '${interaction.commandName}' but there was no tracks in the queue.`); return true; } diff --git a/src/utils/validation/searchQueryValidator.js b/src/utils/validation/searchQueryValidator.js index bf5e010..0cb9e8d 100644 --- a/src/utils/validation/searchQueryValidator.js +++ b/src/utils/validation/searchQueryValidator.js @@ -1,6 +1,11 @@ -const logger = require('../../services/logger'); +exports.transformQuery = async ({ query, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'searchQueryValidator.js', + module: 'validator', + name: 'transformQuery', + executionId: executionId + }); -exports.transformQuery = async (query) => { if (query.startsWith('https://open.spotify.com/')) { try { // regex to check for country-based Spotify url @@ -11,13 +16,13 @@ exports.transformQuery = async (query) => { // if country-based, transform to normal Spotify url if (isCountryBased) { - logger.debug(`Transforming country-based Spotify url to normal Spotify url from query ${query}.`); + logger.debug(`Transforming country-based Spotify url to normal Spotify url from query '${query}'.`); const type = query.match(regex)[2]; const trackId = query.match(regex)[3]; query = `https://open.spotify.com/${type}/${trackId}`; } } catch (error) { - logger.error(error); + logger.error(error, 'Error while validating or transforming Spotify url.'); } } diff --git a/src/utils/validation/systemCommandValidator.js b/src/utils/validation/systemCommandValidator.js index 387f96f..69d1c69 100644 --- a/src/utils/validation/systemCommandValidator.js +++ b/src/utils/validation/systemCommandValidator.js @@ -3,7 +3,16 @@ const embedOptions = config.get('embedOptions'); const systemOptions = config.get('systemOptions'); const { EmbedBuilder } = require('discord.js'); -exports.notValidGuildId = async (interaction) => { +exports.notValidGuildId = async ({ interaction, executionId }) => { + const logger = require('../../services/logger').child({ + source: 'systemCommandValidator.js', + module: 'validator', + name: 'notValidGuildId', + executionId: executionId, + shardId: interaction.guild.shardId, + guildId: interaction.guild.id + }); + if (!systemOptions.systemGuildIds.includes(interaction.guildId)) { await interaction.editReply({ embeds: [ @@ -14,6 +23,9 @@ exports.notValidGuildId = async (interaction) => { .setColor(embedOptions.colors.warning) ] }); + logger.debug( + `User tried to use command ${interaction.commandName} but system command cannot be executed in the specified guild.` + ); return true; } diff --git a/src/utils/validation/voiceChannelValidator.js b/src/utils/validation/voiceChannelValidator.js index e4d9640..a26731d 100644 --- a/src/utils/validation/voiceChannelValidator.js +++ b/src/utils/validation/voiceChannelValidator.js @@ -23,7 +23,7 @@ exports.notInVoiceChannel = async ({ interaction, executionId }) => { ] }); - logger.debug(`User tried to use command ${interaction.commandName} but was not in a voice channel.`); + logger.debug(`User tried to use command '${interaction.commandName}' but was not in a voice channel.`); return true; } @@ -56,7 +56,7 @@ exports.notInSameVoiceChannel = async ({ interaction, queue, executionId }) => { ] }); - logger.debug(`User tried to use command ${interaction.commandName} but was not in the same voice channel.`); + logger.debug(`User tried to use command '${interaction.commandName}' but was not in the same voice channel.`); return true; }