mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-02 22:16:13 +00:00
fix!: New folder structure and organize files
This commit is contained in:
1 parent
d92bd329dc
commit
39ff5ecb5d
83 files changed
+244
-246
No files matched your search
@@ -0,0 +1,137 @@
|
||||
import config from 'config';
|
||||
import {
|
||||
EmbedBuilder,
|
||||
GuildMember,
|
||||
InteractionType,
|
||||
TextBasedChannel,
|
||||
TextChannel,
|
||||
VoiceBasedChannel,
|
||||
VoiceChannel
|
||||
} from 'discord.js';
|
||||
import { Logger } from 'pino';
|
||||
import { InteractionValidationError } from '../classes/interactions';
|
||||
import loggerModule from '../services/logger';
|
||||
import { EmbedOptions } from '../../types/configTypes';
|
||||
import { ValidatorParams } from '../../types/utilTypes';
|
||||
import { useServerTranslator } from '../utils/localeUtil';
|
||||
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
export const checkVoicePermissionJoinAndTalk = async ({ interaction, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'cannotJoinVoiceOrTalk',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const channel: VoiceBasedChannel | null =
|
||||
interaction.member instanceof GuildMember ? interaction.member.voice.channel : null;
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (channel instanceof VoiceChannel && (!channel.joinable || !channel.speakable)) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.cannotJoinVoiceOrTalk', {
|
||||
icon: embedOptions.icons.warning,
|
||||
channel: `<#${channel.id}>`
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text:
|
||||
interaction.member instanceof GuildMember
|
||||
? interaction.member.nickname || interaction.user.username
|
||||
: interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(
|
||||
`User tried to use command '${interactionIdentifier}' but the bot had no permission to join/speak in the voice channel.`
|
||||
);
|
||||
throw new InteractionValidationError('Bot cannot join or speak in voice channel.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
export const checkChannelPermissionViewable = async ({ interaction, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'cannotSendMessageInChannel',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const channel: TextBasedChannel | null = interaction.channel;
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
// only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable
|
||||
if (channel instanceof TextChannel && !channel.viewable) {
|
||||
logger.info(
|
||||
`User tried to use interaction '${interactionIdentifier}' but the bot has permission to send reply in text channel.`
|
||||
);
|
||||
|
||||
try {
|
||||
// we can still send ephemeral replies in channels we can't view, so sending message to user instead
|
||||
if (!interaction.deferred && !interaction.replied) {
|
||||
logger.debug('Interaction was not deferred or replied to yet. Deferring reply as ephemeral.');
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
|
||||
logger.debug(
|
||||
'Sending ephemeral message in channel about insufficient permissions to send message in channel.'
|
||||
);
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.cannotSendMessageInChannel', {
|
||||
icon: embedOptions.icons.warning,
|
||||
channel: `<#${channel.id}>`
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text:
|
||||
interaction.member instanceof GuildMember
|
||||
? interaction.member.nickname || interaction.user.username
|
||||
: interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
}
|
||||
} catch (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.debug('Throwing error');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InteractionValidationError('Bot cannot send message in channel.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
@@ -0,0 +1,174 @@
|
||||
import config from 'config';
|
||||
import { EmbedBuilder, GuildMember, InteractionType } from 'discord.js';
|
||||
import { Logger } from 'pino';
|
||||
import { InteractionValidationError } from '../classes/interactions';
|
||||
import loggerModule from '../services/logger';
|
||||
import { EmbedOptions } from '../../types/configTypes';
|
||||
import { ValidatorParams } from '../../types/utilTypes';
|
||||
import { useServerTranslator } from '../utils/localeUtil';
|
||||
import { formatSlashCommand } from '../utils/formattingUtils';
|
||||
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
export const checkQueueExists = async ({ interaction, queue, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'queueDoesNotExist',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (!queue) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.queueDoesNotExist', {
|
||||
icon: embedOptions.icons.warning,
|
||||
playCommand: formatSlashCommand('play', translator)
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text:
|
||||
interaction.member instanceof GuildMember
|
||||
? interaction.member.nickname || interaction.user.username
|
||||
: interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no queue.`);
|
||||
throw new InteractionValidationError('Queue does not exist.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
export const checkHistoryExists = async ({ interaction, history, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'historyDoesNotExist',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (!history) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.historyDoesNotExist', {
|
||||
icon: embedOptions.icons.warning,
|
||||
playCommand: formatSlashCommand('play', translator)
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text:
|
||||
interaction.member instanceof GuildMember
|
||||
? interaction.member.nickname || interaction.user.username
|
||||
: interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no history.`);
|
||||
throw new InteractionValidationError('History does not exist.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
export const checkQueueCurrentTrack = async ({ interaction, queue, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'queueNoCurrentTrack',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (queue && !queue.currentTrack) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.queueNoCurrentTrack', {
|
||||
icon: embedOptions.icons.warning,
|
||||
playCommand: formatSlashCommand('play', translator)
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text:
|
||||
interaction.member instanceof GuildMember
|
||||
? interaction.member.nickname || interaction.user.username
|
||||
: interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no current track.`);
|
||||
throw new InteractionValidationError('Queue has no current track.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
export const checkQueueEmpty = async ({ interaction, queue, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'queueIsEmpty',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (queue && queue.tracks.data.length === 0) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.queueIsEmpty', {
|
||||
icon: embedOptions.icons.warning,
|
||||
playCommand: formatSlashCommand('play', translator)
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text:
|
||||
interaction.member instanceof GuildMember
|
||||
? interaction.member.nickname || interaction.user.username
|
||||
: interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no tracks in the queue.`);
|
||||
throw new InteractionValidationError('Queue is empty.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
import { Logger } from 'pino';
|
||||
import loggerModule from '../services/logger';
|
||||
import { TransformQueryParams } from '../../types/utilTypes';
|
||||
|
||||
export const transformQuery = ({ query, executionId }: TransformQueryParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'transformQuery',
|
||||
executionId: executionId
|
||||
});
|
||||
|
||||
if (query.startsWith('https://open.spotify.com/')) {
|
||||
try {
|
||||
// regex to check for country-based Spotify url
|
||||
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: boolean = regex.test(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 matches: RegExpMatchArray | null = query.match(regex);
|
||||
if (matches) {
|
||||
const type: string = matches[2];
|
||||
const trackId: string = matches[3];
|
||||
query = `https://open.spotify.com/${type}/${trackId}`;
|
||||
}
|
||||
}
|
||||
|
||||
// if /track/id is in url TWICE, remove the second one, e.g. https://open.spotify.com/track/123/track/123
|
||||
const trackIdRegex: RegExp = new RegExp('https://open.spotify.com/track/([a-zA-Z0-9]+)');
|
||||
const trackIdMatches: RegExpMatchArray | null = query.match(trackIdRegex);
|
||||
if (trackIdMatches) {
|
||||
logger.debug(`Transforming Spotify url to remove duplicate track id from query '${query}'.`);
|
||||
const trackId: string = trackIdMatches[1];
|
||||
query = `https://open.spotify.com/track/${trackId}`;
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(error, 'Error while validating or transforming Spotify url.');
|
||||
}
|
||||
}
|
||||
|
||||
return query;
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
import config from 'config';
|
||||
import { EmbedBuilder, InteractionType } from 'discord.js';
|
||||
import { Logger } from 'pino';
|
||||
import { InteractionValidationError } from '../classes/interactions';
|
||||
import loggerModule from '../services/logger';
|
||||
import { EmbedOptions, SystemOptions } from '../../types/configTypes';
|
||||
import { ValidatorParams } from '../../types/utilTypes';
|
||||
import { useServerTranslator } from '../utils/localeUtil';
|
||||
import { formatSlashCommand } from '../utils/formattingUtils';
|
||||
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
const systemOptions: SystemOptions = config.get('systemOptions');
|
||||
export const checkValidGuildId = async ({ interaction, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'notValidGuildId',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (interaction.guildId && !systemOptions.systemGuildIds.includes(interaction.guildId)) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.notValidGuildId', {
|
||||
icon: embedOptions.icons.warning,
|
||||
command: formatSlashCommand(interactionIdentifier, translator)
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(
|
||||
`User tried to use command '${interactionIdentifier}' but system command cannot be executed in the specified guild.`
|
||||
);
|
||||
throw new InteractionValidationError('System command cannot be executed in the specified guild.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
@@ -0,0 +1,92 @@
|
||||
import config from 'config';
|
||||
import { EmbedBuilder, GuildMember, InteractionType } from 'discord.js';
|
||||
import { Logger } from 'pino';
|
||||
import { InteractionValidationError } from '../classes/interactions';
|
||||
import loggerModule from '../services/logger';
|
||||
import { EmbedOptions } from '../../types/configTypes';
|
||||
import { ValidatorParams } from '../../types/utilTypes';
|
||||
import { useServerTranslator } from '../utils/localeUtil';
|
||||
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
export const checkInVoiceChannel = async ({ interaction, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'validator',
|
||||
name: 'notInVoiceChannel',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (interaction.member instanceof GuildMember && !interaction.member.voice.channel) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.notInVoiceChannel', {
|
||||
icon: embedOptions.icons.warning
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text: interaction.member.nickname || interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(`User tried to use command '${interactionIdentifier}' but was not in a voice channel.`);
|
||||
throw new InteractionValidationError('User not in voice channel.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
export const checkSameVoiceChannel = async ({ interaction, queue, executionId }: ValidatorParams) => {
|
||||
const logger: Logger = loggerModule.child({
|
||||
module: 'utilValidation',
|
||||
name: 'notInSameVoiceChannel',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
const translator = useServerTranslator(interaction);
|
||||
|
||||
const interactionIdentifier =
|
||||
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||
|
||||
if (!queue || !queue.dispatcher) {
|
||||
// If there is no queue or bot is not in voice channel, then there is no need to check if user is in same voice channel.
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
interaction.member instanceof GuildMember &&
|
||||
interaction.member.voice.channel?.id !== queue.dispatcher.channel.id
|
||||
) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(
|
||||
translator('validation.notInSameVoiceChannel', {
|
||||
icon: embedOptions.icons.warning,
|
||||
channel: `<#${queue.dispatcher.channel.id}>`
|
||||
})
|
||||
)
|
||||
.setColor(embedOptions.colors.warning)
|
||||
.setFooter({
|
||||
text: interaction.member.nickname || interaction.user.username,
|
||||
iconURL: interaction.user.avatarURL() || embedOptions.info.fallbackIconUrl
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
logger.debug(`User tried to use command '${interactionIdentifier}' but was not in the same voice channel.`);
|
||||
throw new InteractionValidationError('User not in same voice channel.');
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
Reference in new issue
Block a user