Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: updated logging for validators
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Aug 26, 2023
1 parent 2210982 commit 57fc1e3
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/commands/info/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/player/loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/lyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ module.exports = {
return;
}

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}
geniusSearchQuery = queue.currentTrack.title.slice(0, 50);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/nowplaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = {
return;
}

if (await cannotJoinVoiceOrTalk(interaction)) {
if (await cannotJoinVoiceOrTalk({ interaction, executionId })) {
return;
}

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/player/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/seek.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/shuffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueIsEmpty(interaction, queue)) {
if (await queueIsEmpty({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/skip.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/player/volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {

const queue = useQueue(interaction.guild.id);

if (await queueDoesNotExist(interaction, queue)) {
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/system/guilds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/system/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/system/shards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/system/systemstatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
23 changes: 6 additions & 17 deletions src/events/interactions/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
try {
await interaction.deferReply();

if (await cannotSendMessageInChannel(interaction)) {
if (await cannotSendMessageInChannel({ interaction, executionId })) {
return;
}

Expand All @@ -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,
Expand All @@ -86,19 +84,15 @@ 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;
}

if (interaction.replied) {
// 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()
Expand All @@ -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()
Expand All @@ -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.');
}
}
};
33 changes: 26 additions & 7 deletions src/utils/validation/permissionValidator.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -18,21 +26,30 @@ 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;
}

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 {
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 57fc1e3

Please sign in to comment.