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.')
.setDMPermission(false)
.setNSFW(false),
execute: async ({ interaction, client }) => {
execute: async ({ interaction, client, executionId }) => {
const commandList = client.commands
.filter((command) => {
// don't include system commands
+1 -1
View File
@@ -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();
+2 -2
View File
@@ -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;
}
+1 -1
View File
@@ -34,7 +34,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}
+2 -2
View File
@@ -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);
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -28,7 +28,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+1 -1
View File
@@ -28,7 +28,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id);
if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+6 -17
View File
@@ -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.');
}
}
};
+26 -7
View File
@@ -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(
+33 -10
View File
@@ -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;
}
+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/')) {
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.');
}
}
+13 -1
View File
@@ -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;
}
@@ -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;
}