Merge pull request #87 from mariusbegby/logging-in-commands

Logging in commands
This commit is contained in:
Marius Begby authored and GitHub committed 2023-07-19 00:45:03 +02:00
commit addab2f04e
14 files changed
+124 -9

No files matched your search

+14 -3
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions, ffmpegFilterOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
@@ -77,6 +78,7 @@ module.exports = {
],
components: [filterActionRow, disableFiltersActionRow]
});
logger.debug(`Sent embed for command ${interaction.commandName}, awaiting interaction response.`);
const collectorFilter = (i) => i.user.id === interaction.user.id;
try {
@@ -85,6 +87,8 @@ module.exports = {
time: 60000
});
logger.debug(`Received interaction response for command ${interaction.commandName}.`);
confirmation.deferUpdate();
queue.filters.ffmpeg.setInputArgs([
@@ -124,6 +128,8 @@ module.exports = {
// Enable provided filters
queue.filters.ffmpeg.toggle(confirmation.values);
logger.debug(`Enabled filters ${confirmation.values.join(', ')} for command ${interaction.commandName}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -149,12 +155,17 @@ module.exports = {
],
components: []
});
} catch (e) {
if (e.code === 'InteractionCollectorError') {
} catch (error) {
if (error.code === 'InteractionCollectorError') {
logger.debug(`Interaction response timed out for command ${interaction.commandName}.`);
return;
}
throw e;
logger.debug(
error,
`Unhandled error while awaiting interaction response for command ${interaction.commandName}, throwing error.`
);
throw error;
}
}
};
+3
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
@@ -18,6 +19,7 @@ module.exports = {
const queue = useQueue(interaction.guild.id);
if (!queue) {
logger.debug(`User used command ${interaction.commandName} but there was no queue.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -31,6 +33,7 @@ module.exports = {
if (!queue.deleted) {
queue.delete();
logger.debug(`User used command ${interaction.commandName} and deleted the queue.`);
return await interaction.editReply({
embeds: [
+15
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions, botOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist } = require('../../utils/validation/queueValidation');
@@ -45,6 +46,8 @@ module.exports = {
const currentModeUserString = loopModesFormatted.get(currentMode);
if (!mode && mode !== 0) {
logger.debug(`User used command ${interaction.commandName} but no mode was provided.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -59,6 +62,10 @@ module.exports = {
}
if (mode === currentMode) {
logger.debug(
`User used command ${interaction.commandName} but loop mode was already set to ${modeUserString}.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -73,6 +80,8 @@ module.exports = {
queue.setRepeatMode(mode);
if (!queue.repeatMode === mode) {
logger.debug(`User used command ${interaction.commandName} but failed to change loop mode.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -85,6 +94,8 @@ module.exports = {
}
if (queue.repeatMode === 0) {
logger.debug(`User used command ${interaction.commandName} and disabled loop mode.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -101,6 +112,8 @@ module.exports = {
}
if (queue.repeatMode === 3) {
logger.debug(`User used command ${interaction.commandName} and enabled autoplay.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -116,6 +129,8 @@ module.exports = {
});
}
logger.debug(`User used command ${interaction.commandName} and enabled loop mode.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+18 -3
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions, playerOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
@@ -92,6 +93,8 @@ module.exports = {
const loopModeUserString = loopModesFormatted.get(queue.repeatMode);
logger.debug(`User used command ${interaction.commandName}, finished intializing data.`);
const response = await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -140,6 +143,8 @@ module.exports = {
components: [nowPlayingActionRow]
});
logger.debug(`User used command ${interaction.commandName}, finished sending response.`);
const collectorFilter = (i) => i.user.id === interaction.user.id;
try {
const confirmation = await response.awaitMessageComponent({
@@ -150,7 +155,11 @@ module.exports = {
confirmation.deferUpdate();
if (confirmation.customId === 'nowplaying-skip') {
logger.debug(`User used command ${interaction.commandName}, received skip confirmation.`);
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
logger.debug(
`User used command ${interaction.commandName} and tried skipping but there was no queue.`
);
return await interaction.followUp({
embeds: [
new EmbedBuilder()
@@ -164,6 +173,9 @@ module.exports = {
}
if (queue.currentTrack !== currentTrack) {
logger.debug(
`User used command ${interaction.commandName} and tried skipping but the track was already removed.`
);
return await interaction.followUp({
embeds: [
new EmbedBuilder()
@@ -185,6 +197,7 @@ module.exports = {
const repeatModeUserString = loopModesFormatted.get(queue.repeatMode);
logger.debug(`User used command ${interaction.commandName} and skipped the track.`);
return await interaction.followUp({
embeds: [
new EmbedBuilder()
@@ -210,12 +223,14 @@ module.exports = {
components: []
});
}
} catch (e) {
if (e.code === 'InteractionCollectorError') {
} catch (error) {
if (error.code === 'InteractionCollectorError') {
logger.debug(`User used command ${interaction.commandName} but did not respond to the skip prompt.`);
return;
}
throw e;
logger.debug(error, `User used command ${interaction.commandName} but there was an unhandled error.`);
throw error;
}
}
};
+2
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
@@ -31,6 +32,7 @@ module.exports = {
// change paused state to opposite of current state
queue.node.setPaused(!queue.node.isPaused());
logger.debug(`User used command ${interaction.commandName} and set paused state to ${queue.node.isPaused()}.`);
return await interaction.editReply({
embeds: [
+18 -2
View File
@@ -33,6 +33,8 @@ module.exports = {
}
if (!searchResult || searchResult.tracks.length === 0) {
logger.debug(`No results found for query: ${query}`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -49,6 +51,8 @@ module.exports = {
searchResult.tracks[0].raw.duration === 0 &&
searchResult.tracks[0].source === 'youtube'
) {
logger.debug(`Unsupported audio source. Query: ${query}. Track is live, 0 duration and from YouTube.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -83,6 +87,8 @@ module.exports = {
}
}
}));
logger.debug(`player.play() successful. Query: ${query}.`);
} catch (error) {
if (error.message.includes('Sign in to confirm your age')) {
return await interaction.editReply({
@@ -102,6 +108,8 @@ module.exports = {
error.message.includes('Failed to fetch resources for ytdl streaming'))) ||
error.message.includes('Could not extract stream for this track')
) {
logger.debug(error, `Found track but failed to retrieve audio. Query: ${query}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -114,6 +122,8 @@ module.exports = {
}
if (error.message === 'Cancelled') {
logger.debug(error, `Operation cancelled. Query: ${query}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -125,13 +135,14 @@ module.exports = {
});
}
logger.error('Failed to play track with player.play()');
logger.error(error);
logger.error(error, 'Failed to play track with player.play(), unhandled error.');
}
let queue = useQueue(interaction.guild.id);
if (!queue) {
logger.warn(`After player.play(), queue is undefined. Query: ${query}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -151,6 +162,8 @@ module.exports = {
let durationFormat = track.raw.duration === 0 || track.duration === '0:00' ? '' : `\`${track.duration}\``;
if (searchResult.playlist && searchResult.tracks.length > 1) {
logger.debug(`Playlist found and added with player.play(). Query: ${query}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -172,6 +185,8 @@ module.exports = {
}
if (queue.currentTrack === track && queue.tracks.data.length === 0) {
logger.debug(`Track found and added with player.play(), started playing. Query: ${query}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -189,6 +204,7 @@ module.exports = {
});
}
logger.debug(`Track found and added with player.play(), added to queue. Query: ${query}.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+8 -1
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions, playerOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
@@ -20,6 +21,7 @@ module.exports = {
if (!queue) {
if (pageIndex >= 1) {
logger.debug(`User used command ${interaction.commandName} but there was no queue.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -33,6 +35,7 @@ module.exports = {
});
}
logger.debug(`User used command ${interaction.commandName} but there was no queue.`);
queueString = 'The queue is empty, add some tracks with **`/play`**!';
return await interaction.editReply({
embeds: [
@@ -54,6 +57,8 @@ module.exports = {
const totalPages = Math.ceil(queueLength / 10) || 1;
if (pageIndex > totalPages - 1) {
logger.debug(`User used command ${interaction.commandName} but page was higher than total pages.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -99,9 +104,10 @@ module.exports = {
queue.repeatMode === 3 ? embedOptions.icons.autoplay : embedOptions.icons.loop
} Looping**\nLoop mode is set to ${loopModeUserString}. You can change it with **\`/loop\`**.\n\n`
}`;
if (!currentTrack) {
logger.debug(`User used command ${interaction.commandName} but there was no current track.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -133,6 +139,7 @@ module.exports = {
bar = '_No duration available._';
}
logger.debug(`User used command ${interaction.commandName} and got queue in reply with current track.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+4
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist } = require('../../utils/validation/queueValidation');
@@ -30,6 +31,8 @@ module.exports = {
const removeTrackNumber = interaction.options.getNumber('tracknumber');
if (removeTrackNumber > queue.tracks.data.length) {
logger.debug(`User used command ${interaction.commandName} but track number was higher than total tracks.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -46,6 +49,7 @@ module.exports = {
let durationFormat =
removedTrack.raw.duration === 0 || removedTrack.duration === '0:00' ? '' : `\`${removedTrack.duration}\``;
logger.debug(`User used command ${interaction.commandName} and removed track.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+13
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
@@ -45,6 +46,8 @@ module.exports = {
}
if (durationArray.length === 0 || durationArray.length > 3) {
logger.debug(`User used command ${interaction.commandName} but entered an invalid duration.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -70,6 +73,9 @@ module.exports = {
});
if (!validElements) {
logger.debug(
`User used command ${interaction.commandName} but entered an invalid duration after parsing duration.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -90,6 +96,10 @@ module.exports = {
const isValidDuration = regex.test(durationString);
if (!isValidDuration) {
logger.debug(
`User used command ${interaction.commandName} but entered an invalid duration after regex checks.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -108,6 +118,7 @@ module.exports = {
const durationInMilliseconds = durationArray[0] * 3600000 + durationArray[1] * 60000 + durationArray[2] * 1000;
if (durationInMilliseconds > currentTrackMaxDurationInMs - 1000) {
logger.debug(`User used command ${interaction.commandName} but entered a duration longer than the track.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -120,6 +131,8 @@ module.exports = {
}
queue.node.seek(durationInMilliseconds);
logger.debug(`User used command ${interaction.commandName} and seeked to duration.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+9
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
@@ -31,6 +32,9 @@ module.exports = {
if (skipToTrack) {
if (skipToTrack > queue.tracks.data.length) {
logger.debug(
`User used command ${interaction.commandName} but track number was higher than total tracks.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -48,6 +52,7 @@ module.exports = {
: `\`${skippedTrack.duration}\``;
queue.node.skipTo(skipToTrack - 1);
logger.debug(`User used command ${interaction.commandName} and skipped to track.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -65,6 +70,9 @@ module.exports = {
}
} else {
if (queue.tracks.data.length === 0 && !queue.currentTrack) {
logger.debug(
`User used command ${interaction.commandName} but there was no tracks in queue or current track.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -92,6 +100,7 @@ module.exports = {
const loopModeUserString = loopModesFormatted.get(queue.repeatMode);
logger.debug(`User used command ${interaction.commandName} and skipped track.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+9
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist } = require('../../utils/validation/queueValidation');
@@ -32,6 +33,9 @@ module.exports = {
if (!volume && volume !== 0) {
const currentVolume = queue.node.volume;
logger.debug(
`User used command ${interaction.commandName} but no volume was provided, providing info of current volume.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -44,6 +48,9 @@ module.exports = {
]
});
} else if (volume > 100 || volume < 0) {
logger.debug(
`User used command ${interaction.commandName} but volume was higher than 100% or lower than 0%.`
);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -57,6 +64,7 @@ module.exports = {
queue.node.setVolume(volume);
if (volume === 0) {
logger.debug(`User used command ${interaction.commandName} and muted volume.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -72,6 +80,7 @@ module.exports = {
});
}
logger.debug(`User used command ${interaction.commandName} and changed volume.`);
return await interaction.editReply({
embeds: [
new EmbedBuilder()
+7
View File
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { EmbedBuilder } = require('discord.js');
@@ -12,6 +13,9 @@ exports.queueDoesNotExist = async (interaction, queue) => {
.setColor(embedOptions.colors.warning)
]
});
logger.debug(`User tried to use command ${interaction.commandName} but there was no queue.`);
return true;
}
@@ -29,6 +33,9 @@ exports.queueNoCurrentTrack = async (interaction, queue) => {
.setColor(embedOptions.colors.warning)
]
});
logger.debug(`User tried to use command ${interaction.commandName} but there was no current track.`);
return true;
}
@@ -11,6 +11,7 @@ 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}.`);
const trackId = query.match(regex)[2];
query = `https://open.spotify.com/track/${trackId}`;
}
@@ -1,3 +1,4 @@
const logger = require('../../services/logger');
const { embedOptions } = require('../../config');
const { EmbedBuilder } = require('discord.js');
@@ -12,6 +13,8 @@ exports.notInVoiceChannel = async (interaction) => {
.setColor(embedOptions.colors.warning)
]
});
logger.debug(`User tried to use command ${interaction.commandName} but was not in a voice channel.`);
return true;
}