fix: annotated util validation functions

This commit is contained in:
Marius Begby committed 2023-08-27 01:36:58 +02:00
1 parent d38ed18842
commit 1ae9503477
5 files changed
+64 -50

No files matched your search

+25 -21
View File
@@ -1,22 +1,23 @@
import config from 'config';
import { EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions');
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, GuildMember, VoiceChannel, TextChannel } from 'discord.js';
import loggerModule from '../../services/logger';
import { CannotJoinVoiceOrTalkParams, CannotSendMessageInChannelParams } from '../../types/utilTypes';
export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }) => {
export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: CannotJoinVoiceOrTalkParams) => {
const logger = loggerModule.child({
source: 'permissionValidator.js',
module: 'utilValidation',
name: 'cannotJoinVoiceOrTalk',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
const channel = interaction.member.voice.channel;
const channel = interaction.member instanceof GuildMember ? interaction.member.voice.channel : null;
if (!channel.joinable || !channel.speakable) {
if (channel instanceof VoiceChannel && (!channel.joinable || !channel.speakable)) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -36,20 +37,20 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }) => {
return false;
};
export const cannotSendMessageInChannel = async ({ interaction, executionId }) => {
export const cannotSendMessageInChannel = async ({ interaction, executionId }: CannotSendMessageInChannelParams) => {
const logger = loggerModule.child({
source: 'permissionValidator.js',
module: 'utilValidation',
name: 'cannotSendMessageInChannel',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
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) {
if (channel instanceof TextChannel && !channel.viewable) {
logger.info(
`User tried to use command '${interaction.commandName}' but the bot had no permission to send reply in text channel.`
);
@@ -66,20 +67,23 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }) =
`**${embedOptions.icons.warning} Oops!**\nI do not have permission to send message replies in the channel you are in.\n\nPlease make sure I have the **\`View Channel\`** permission in this text channel.`
)
.setColor(embedOptions.colors.warning)
],
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.'
);
logger.debug(error);
if (error instanceof 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.'
);
logger.debug(error);
} else {
logger.error(
error,
'Failed to send ephemeral reply to user in channel that bot cannot view/send message in.'
);
}
} else {
logger.error(
error,
'Failed to send ephemeral reply to user in channel that bot cannot view/send message in.'
);
throw error;
}
}
+10 -9
View File
@@ -3,15 +3,16 @@ import { EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions');
import { EmbedBuilder } from 'discord.js';
import loggerModule from '../../services/logger';
import { QueueDoesNotExistParams, QueueNoCurrentTrackParams, QueueIsEmptyParams } from '../../types/utilTypes';
export const queueDoesNotExist = async ({ interaction, queue, executionId }) => {
export const queueDoesNotExist = async ({ interaction, queue, executionId }: QueueDoesNotExistParams) => {
const logger = loggerModule.child({
source: 'queueValidator.js',
module: 'utilValidation',
name: 'queueDoesNotExist',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
if (!queue) {
@@ -32,14 +33,14 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }) =>
return false;
};
export const queueNoCurrentTrack = async ({ interaction, queue, executionId }) => {
export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: QueueNoCurrentTrackParams) => {
const logger = loggerModule.child({
source: 'queueValidator.js',
module: 'utilValidation',
name: 'queueNoCurrentTrack',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
if (!queue.currentTrack) {
@@ -60,14 +61,14 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }) =
return false;
};
export const queueIsEmpty = async ({ interaction, queue, executionId }) => {
export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsEmptyParams) => {
const logger = loggerModule.child({
source: 'queueValidator.js',
module: 'utilValidation',
name: 'queueIsEmpty',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
if (queue.tracks.data.length === 0) {
+9 -5
View File
@@ -1,6 +1,7 @@
import loggerModule from '../../services/logger';
import { TransformQueryParams } from '../../types/utilTypes';
export const transformQuery = async ({ query, executionId }) => {
export const transformQuery = async ({ query, executionId }: TransformQueryParams) => {
const logger = loggerModule.child({
source: 'searchQueryValidator.js',
module: 'utilValidation',
@@ -11,7 +12,7 @@ export const transformQuery = async ({ query, executionId }) => {
if (query.startsWith('https://open.spotify.com/')) {
try {
// regex to check for country-based Spotify url
const regex = new RegExp(
const regex: RegExp = new RegExp(
'https://open.spotify.com/((?:[a-z]{2,4}-[a-z]{2,4}/)|(?:[a-z]{2,4}/))(track|playlist)/([a-zA-Z0-9]+)'
);
const isCountryBased = regex.test(query);
@@ -19,9 +20,12 @@ export const transformQuery = async ({ query, executionId }) => {
// 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 type = query.match(regex)[2];
const trackId = query.match(regex)[3];
query = `https://open.spotify.com/${type}/${trackId}`;
const matches = query.match(regex);
if (matches) {
const type = matches[2];
const trackId = matches[3];
query = `https://open.spotify.com/${type}/${trackId}`;
}
}
} catch (error) {
logger.error(error, 'Error while validating or transforming Spotify url.');
@@ -1,22 +1,23 @@
import loggerModule from '../../services/logger';
import config from 'config';
import { EmbedOptions } from '../../types/configTypes';
import { EmbedOptions, SystemOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions');
const systemOptions = config.get('systemOptions');
const systemOptions: SystemOptions = config.get('systemOptions');
import { EmbedBuilder } from 'discord.js';
import { NotValidGuildIdParams } from '../../types/utilTypes';
export const notValidGuildId = async ({ interaction, executionId }) => {
export const notValidGuildId = async ({ interaction, executionId }: NotValidGuildIdParams) => {
const logger = loggerModule.child({
source: 'systemCommandValidator.js',
module: 'utilValidation',
name: 'notValidGuildId',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
if (!systemOptions.systemGuildIds.includes(interaction.guildId)) {
if (interaction.guildId && !systemOptions.systemGuildIds.includes(interaction.guildId)) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
+13 -9
View File
@@ -2,19 +2,20 @@ import { EmbedOptions } from '../../types/configTypes';
import loggerModule from '../../services/logger';
import config from 'config';
const embedOptions: EmbedOptions = config.get('embedOptions');
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, GuildMember } from 'discord.js';
import { NotInVoiceChannelParams, NotInSameVoiceChannelParams } from '../../types/utilTypes';
export const notInVoiceChannel = async ({ interaction, executionId }) => {
export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoiceChannelParams) => {
const logger = loggerModule.child({
source: 'voiceChannelValidator.js',
module: 'validator',
name: 'notInVoiceChannel',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
if (!interaction.member.voice.channel) {
if (interaction.member instanceof GuildMember && !interaction.member.voice.channel) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -32,14 +33,14 @@ export const notInVoiceChannel = async ({ interaction, executionId }) => {
return false;
};
export const notInSameVoiceChannel = async ({ interaction, queue, executionId }) => {
export const notInSameVoiceChannel = async ({ interaction, queue, executionId }: NotInSameVoiceChannelParams) => {
const logger = loggerModule.child({
source: 'voiceChannelValidator.js',
module: 'utilValidation',
name: 'notInSameVoiceChannel',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
if (!queue || !queue.dispatcher) {
@@ -47,7 +48,10 @@ export const notInSameVoiceChannel = async ({ interaction, queue, executionId })
return false;
}
if (interaction.member.voice.channel.id !== queue.dispatcher.channel.id) {
if (
interaction.member instanceof GuildMember &&
interaction.member.voice.channel?.id !== queue.dispatcher.channel.id
) {
await interaction.editReply({
embeds: [
new EmbedBuilder()