fix: updated logging for validators

This commit is contained in:
Marius Begby committed 2023-08-26 14:39:33 +02:00
1 parent 22109823e1
commit 57fc1e3ee8
23 files changed
+118 -70

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ module.exports = {
.setDescription('Show a list of commands and their usage.') .setDescription('Show a list of commands and their usage.')
.setDMPermission(false) .setDMPermission(false)
.setNSFW(false), .setNSFW(false),
execute: async ({ interaction, client }) => { execute: async ({ interaction, client, executionId }) => {
const commandList = client.commands const commandList = client.commands
.filter((command) => { .filter((command) => {
// don't include system commands // don't include system commands
+1 -1
View File
@@ -14,7 +14,7 @@ module.exports = {
.setDescription('Show the bot and system status.') .setDescription('Show the bot and system status.')
.setDMPermission(false) .setDMPermission(false)
.setNSFW(false), .setNSFW(false),
execute: async ({ interaction, client }) => { execute: async ({ interaction, client, executionId }) => {
const uptimeString = await getUptimeFormatted(); const uptimeString = await getUptimeFormatted();
const usedMemoryInMB = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US'); const usedMemoryInMB = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US');
const cpuUsage = await osu.cpu.usage(); const cpuUsage = await osu.cpu.usage();
+2 -2
View File
@@ -29,7 +29,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -37,7 +37,7 @@ module.exports = {
return; return;
} }
if (await queueNoCurrentTrack(interaction, queue)) { if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return; return;
} }
+1 -1
View File
@@ -34,7 +34,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -96,7 +96,7 @@ module.exports = {
return; return;
} }
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -104,7 +104,7 @@ module.exports = {
return; return;
} }
if (await queueNoCurrentTrack(interaction, queue)) { if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return; return;
} }
geniusSearchQuery = queue.currentTrack.title.slice(0, 50); geniusSearchQuery = queue.currentTrack.title.slice(0, 50);
+2 -2
View File
@@ -22,7 +22,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -30,7 +30,7 @@ module.exports = {
return; return;
} }
if (await queueNoCurrentTrack(interaction, queue)) { if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -21,7 +21,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -29,7 +29,7 @@ module.exports = {
return; return;
} }
if (await queueNoCurrentTrack(interaction, queue)) { if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -95,7 +95,7 @@ module.exports = {
return; return;
} }
if (await cannotJoinVoiceOrTalk(interaction)) { if (await cannotJoinVoiceOrTalk({ interaction, executionId })) {
return; return;
} }
@@ -107,7 +107,7 @@ module.exports = {
const player = useMainPlayer(); const player = useMainPlayer();
const query = interaction.options.getString('query'); const query = interaction.options.getString('query');
const transformedQuery = await transformQuery(query); const transformedQuery = await transformQuery({ query, executionId });
let searchResult; let searchResult;
+1 -1
View File
@@ -28,7 +28,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -27,7 +27,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -35,7 +35,7 @@ module.exports = {
return; return;
} }
if (await queueNoCurrentTrack(interaction, queue)) { if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -21,7 +21,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -29,7 +29,7 @@ module.exports = {
return; return;
} }
if (await queueIsEmpty(interaction, queue)) { if (await queueIsEmpty({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -24,7 +24,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
@@ -32,7 +32,7 @@ module.exports = {
return; return;
} }
if (await queueNoCurrentTrack(interaction, queue)) { if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return; return;
} }
+1 -1
View File
@@ -28,7 +28,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id); const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) { if (await queueDoesNotExist({ interaction, queue, executionId })) {
return; return;
} }
+2 -2
View File
@@ -13,8 +13,8 @@ module.exports = {
.setDescription('Show list of guilds where bot is added.') .setDescription('Show list of guilds where bot is added.')
.setDMPermission(false) .setDMPermission(false)
.setNSFW(false), .setNSFW(false),
execute: async ({ interaction, client }) => { execute: async ({ interaction, client, executionId }) => {
if (await notValidGuildId(interaction)) { if (await notValidGuildId({ interaction, executionId })) {
logger.debug('Not a valid guild id.'); logger.debug('Not a valid guild id.');
return; return;
} }
+2 -2
View File
@@ -13,8 +13,8 @@ module.exports = {
.setDescription('Reload the bot commands.') .setDescription('Reload the bot commands.')
.setDMPermission(false) .setDMPermission(false)
.setNSFW(false), .setNSFW(false),
execute: async ({ interaction, client }) => { execute: async ({ interaction, client, executionId }) => {
if (await notValidGuildId(interaction)) { if (await notValidGuildId({ interaction, executionId })) {
logger.debug('Not a valid guild id.'); logger.debug('Not a valid guild id.');
return; return;
} }
+2 -2
View File
@@ -30,8 +30,8 @@ module.exports = {
) )
.addNumberOption((option) => option.setName('page').setDescription('Page number to show').setMinValue(1)), .addNumberOption((option) => option.setName('page').setDescription('Page number to show').setMinValue(1)),
execute: async ({ interaction, client }) => { execute: async ({ interaction, client, executionId }) => {
if (await notValidGuildId(interaction)) { if (await notValidGuildId({ interaction, executionId })) {
return; return;
} }
+2 -2
View File
@@ -16,8 +16,8 @@ module.exports = {
.setDescription('Show the bot and system status.') .setDescription('Show the bot and system status.')
.setDMPermission(false) .setDMPermission(false)
.setNSFW(false), .setNSFW(false),
execute: async ({ interaction, client }) => { execute: async ({ interaction, client, executionId }) => {
if (await notValidGuildId(interaction)) { if (await notValidGuildId({ interaction, executionId })) {
return; return;
} }
+6 -17
View File
@@ -59,7 +59,7 @@ module.exports = {
try { try {
await interaction.deferReply(); await interaction.deferReply();
if (await cannotSendMessageInChannel(interaction)) { if (await cannotSendMessageInChannel({ interaction, executionId })) {
return; return;
} }
@@ -69,9 +69,7 @@ module.exports = {
const outputTime = new Date(); const outputTime = new Date();
const executionTime = outputTime - inputTime; const executionTime = outputTime - inputTime;
logger.info( logger.info(`Command '${interaction}' executed in ${executionTime} ms.`);
`Command '${interaction}' executed in ${executionTime} ms.`
);
} catch (error) { } catch (error) {
logger.warn( logger.warn(
error, error,
@@ -86,9 +84,7 @@ module.exports = {
return; return;
} else { } else {
if (!interaction.deferred || !interaction.replied) { if (!interaction.deferred || !interaction.replied) {
logger.warn( logger.warn('Interaction was not deferred or replied to, and an error was thrown.');
'Interaction was not deferred or replied to, and an error was thrown.'
);
return; 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. // If the interaction has already been replied to, most likely command executed successfully or error is already handled.
return; return;
} else if (interaction.deferred) { } else if (interaction.deferred) {
logger.info( logger.info('Interaction was deferred, editing reply and sending Uh-oh message.');
'Interaction was deferred, editing reply and sending Uh-oh message.'
);
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()
@@ -109,9 +103,7 @@ module.exports = {
] ]
}); });
} else { } else {
logger.info( logger.info('Interaction was not deferred or replied, sending new reply with Uh-oh message.');
'Interaction was not deferred or replied, sending new reply with Uh-oh message.'
);
await interaction.reply({ await interaction.reply({
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()
@@ -125,10 +117,7 @@ module.exports = {
} }
} }
} else { } else {
logger.warn( logger.warn(interaction, 'Interaction created but was not a chat input or autocomplete interaction.');
interaction,
'Interaction created but was not a chat input or autocomplete interaction.'
);
} }
} }
}; };
+26 -7
View File
@@ -1,9 +1,17 @@
const logger = require('../../services/logger');
const config = require('config'); const config = require('config');
const embedOptions = config.get('embedOptions'); const embedOptions = config.get('embedOptions');
const { EmbedBuilder } = require('discord.js'); 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; const channel = interaction.member.voice.channel;
if (!channel.joinable || !channel.speakable) { if (!channel.joinable || !channel.speakable) {
@@ -18,7 +26,7 @@ exports.cannotJoinVoiceOrTalk = async (interaction) => {
}); });
logger.debug( 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; return true;
} }
@@ -26,13 +34,22 @@ exports.cannotJoinVoiceOrTalk = async (interaction) => {
return false; 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; const channel = interaction.channel;
// only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable // only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable
if (!channel.viewable) { if (!channel.viewable) {
logger.info( 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 { try {
@@ -51,8 +68,10 @@ exports.cannotSendMessageInChannel = async (interaction) => {
ephemeral: true ephemeral: true
}); });
} catch (error) { } catch (error) {
if(error.message == 'The reply to this interaction has already been sent or deferred.') { 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.warn(
'Error while sending ephemereal message about insufficient permissions to send message in channel.'
);
logger.debug(error); logger.debug(error);
} else { } else {
logger.error( logger.error(
+33 -10
View File
@@ -1,9 +1,17 @@
const logger = require('../../services/logger');
const config = require('config'); const config = require('config');
const embedOptions = config.get('embedOptions'); const embedOptions = config.get('embedOptions');
const { EmbedBuilder } = require('discord.js'); 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) { if (!queue) {
await interaction.editReply({ await interaction.editReply({
embeds: [ 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 true;
} }
return false; 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) { if (!queue.currentTrack) {
await interaction.editReply({ await interaction.editReply({
embeds: [ 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 true;
} }
return false; 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) { if (queue.tracks.data.length === 0) {
await interaction.editReply({ await interaction.editReply({
embeds: [ 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; return true;
} }
+9 -4
View File
@@ -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/')) { if (query.startsWith('https://open.spotify.com/')) {
try { try {
// regex to check for country-based Spotify url // 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 country-based, transform to normal Spotify url
if (isCountryBased) { 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 type = query.match(regex)[2];
const trackId = query.match(regex)[3]; const trackId = query.match(regex)[3];
query = `https://open.spotify.com/${type}/${trackId}`; query = `https://open.spotify.com/${type}/${trackId}`;
} }
} catch (error) { } catch (error) {
logger.error(error); logger.error(error, 'Error while validating or transforming Spotify url.');
} }
} }
+13 -1
View File
@@ -3,7 +3,16 @@ const embedOptions = config.get('embedOptions');
const systemOptions = config.get('systemOptions'); const systemOptions = config.get('systemOptions');
const { EmbedBuilder } = require('discord.js'); 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)) { if (!systemOptions.systemGuildIds.includes(interaction.guildId)) {
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
@@ -14,6 +23,9 @@ exports.notValidGuildId = async (interaction) => {
.setColor(embedOptions.colors.warning) .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; return true;
} }
@@ -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; 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; return true;
} }