Merge pull request #192 from mariusbegby/small-improvements

Small improvements
This commit is contained in:
Marius Begby authored and GitHub committed 2023-08-29 15:52:58 +02:00
commit 0d93396667
32 files changed
+318 -331

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@ export const handleCommand = async (
}
if (await cannotSendMessageInChannel({ interaction, executionId })) {
return Promise.resolve();
return;
}
logger.debug('Executing command interaction.');
@@ -1,5 +1,6 @@
import { MessageComponentInteraction } from 'discord.js';
import loggerModule from '../services/logger';
import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator';
const logger = loggerModule.child({
source: 'interactionComponentHandler.js',
@@ -16,6 +17,10 @@ export const handleComponent = async (interaction: MessageComponentInteraction,
logger.debug(`Parsed componentId: ${componentId}`);
if (await cannotSendMessageInChannel({ interaction, executionId })) {
return;
}
const componentModule = await import(`../interactions/components/${componentId}.js`);
const { default: component } = componentModule;
+9 -18
View File
@@ -39,37 +39,28 @@ const command: CustomSlashCommandInteraction = {
let totalTracks: number = 0;
let totalListeners: number = 0;
await client!.shard!
.broadcastEval(() => {
await client!
.shard!.broadcastEval(() => {
/* eslint-disable no-undef */
return player.generateStatistics();
})
.then((results) => {
const queueCountList: number[] = [];
const trackCountList: number[] = [];
const listenerCountList: number[] = [];
results.map((result) => {
queueCountList.push(result.queues.length);
if (result.queues.length > 0) {
result.queues.map((queue) => {
trackCountList.push(queue.status.playing ? queue.tracksCount + 1 : queue.tracksCount);
listenerCountList.push(queue.listeners);
});
}
activeVoiceConnections += result.queues.length;
result.queues.map((queue) => {
totalTracks += queue.status.playing ? queue.tracksCount + 1 : queue.tracksCount;
totalListeners += queue.listeners;
});
});
activeVoiceConnections = queueCountList.reduce((acc, queueAmount) => acc + queueAmount, 0);
totalTracks = trackCountList.reduce((acc, trackAmount) => acc + trackAmount, 0);
totalListeners = listenerCountList.reduce((acc, listenerAmount) => acc + listenerAmount, 0);
logger.debug('Successfully fetched player statistics from shards.');
})
.catch((error) => {
logger.error(error, 'Failed to fetch player statistics from shards.');
});
await client!.shard!
.fetchClientValues('guilds.cache')
await client!
.shard!.fetchClientValues('guilds.cache')
.then((results) => {
const guildCaches = results as Guild[][];
guildCaches.map((guildCache: Guild[]) => {
+12 -15
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, QueueFilters, useQueue } from 'discord-player';
import { GuildQueue, QueueFilters, useQueue } from 'discord-player';
import {
APIActionRowComponent,
APIMessageActionRowComponent,
@@ -39,22 +39,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const filterOptions: StringSelectMenuOptionBuilder[] = [];
+12 -22
View File
@@ -1,11 +1,12 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
import { EmbedOptions } from '../../../types/configTypes';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../../utils/validation/queueValidator';
const embedOptions: EmbedOptions = config.get('embedOptions');
@@ -27,29 +28,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
if (!queue) {
logger.debug('There is already no queue.');
logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\n_Hmm.._ It seems I am not in a voice channel!`
)
.setColor(embedOptions.colors.warning)
]
});
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
if (!queue.deleted) {
+12 -11
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder, SlashCommandStringOption } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -41,18 +41,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const loopModesFormatted = new Map([
@@ -132,6 +132,7 @@ const command: CustomSlashCommandInteraction = {
if (queue.repeatMode === 0) {
logger.debug('Disabled loop mode.');
// TODO: Different text when disabling autoplay.
logger.debug('Responding with success embed.');
return await interaction.editReply({
embeds: [
+14 -16
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, QueryType, useMainPlayer, useQueue } from 'discord-player';
import { GuildQueue, QueryType, useMainPlayer, useQueue } from 'discord-player';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { lyricsExtractor } from '@discord-player/extractor';
@@ -43,25 +43,23 @@ const command: CustomSlashCommandInteraction = {
});
const query = interaction.options.getString('query');
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
let geniusSearchQuery = '';
if (!query) {
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
for (const validator of validators) {
if (await validator()) {
return;
}
}
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return Promise.resolve();
}
geniusSearchQuery = queue.currentTrack!.title.slice(0, 50);
logger.debug(
@@ -188,7 +186,7 @@ const command: CustomSlashCommandInteraction = {
}
}
return Promise.resolve();
return;
}
logger.debug('Responding with info embed.');
+12 -15
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, Track, useQueue } from 'discord-player';
import { GuildQueue, Track, useQueue } from 'discord-player';
import {
APIActionRowComponent,
APIMessageActionRowComponent,
@@ -37,22 +37,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const sourceStringsFormatted = new Map([
+12 -15
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -28,22 +28,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const currentTrack = queue.currentTrack!;
+16 -13
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { useMainPlayer, useQueue } from 'discord-player';
import { GuildQueue, useMainPlayer, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -43,17 +43,20 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
if (await cannotJoinVoiceOrTalk({ interaction, executionId })) {
return Promise.resolve();
}
let queue = useQueue(interaction.guild!.id);
let queue: GuildQueue = useQueue(interaction.guild!.id)!;
if (queue && (await notInSameVoiceChannel({ interaction, queue, executionId }))) {
return Promise.resolve();
return;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => cannotJoinVoiceOrTalk({ interaction, executionId })
];
for (const validator of validators) {
if (await validator()) {
return;
}
}
const player = useMainPlayer()!;
@@ -86,7 +89,7 @@ const command: CustomSlashCommandInteraction = {
});
}
queue = useQueue(interaction.guild!.id);
queue = useQueue(interaction.guild!.id)!;
const queueSize = queue?.size ?? 0;
if ((searchResult.playlist! && searchResult.tracks.length) > playerOptions.maxQueueSize - queueSize) {
@@ -211,7 +214,7 @@ const command: CustomSlashCommandInteraction = {
logger.debug(`Successfully added track with player.play(). Query: '${query}'.`);
queue = useQueue(interaction.guild!.id);
queue = useQueue(interaction.guild!.id)!;
if (!queue || !track) {
logger.warn(`After player.play(), queue is undefined. Query: '${query}'.`);
+12 -45
View File
@@ -1,11 +1,12 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
import { EmbedOptions, PlayerOptions } from '../../../types/configTypes';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../../utils/validation/queueValidator';
const embedOptions: EmbedOptions = config.get('embedOptions');
const playerOptions: PlayerOptions = config.get('playerOptions');
@@ -29,57 +30,23 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const pageIndex = (interaction.options.getNumber('page') || 1) - 1;
let queueString = '';
if (!queue) {
if (pageIndex >= 1) {
logger.debug(`There is no queue and user tried to access page ${pageIndex + 1}.`);
logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nPage **\`${
pageIndex + 1
}\`** is not a valid page number.\n\nThe queue is currently empty, first add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
}
logger.debug('There is no queue, displaying empty queue.');
queueString = 'The queue is empty, add some tracks with **`/play`**!';
logger.debug('Responding with info embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: interaction.guild!.name,
iconURL: interaction.guild!.iconURL() || ''
})
.setDescription(`**${embedOptions.icons.queue} Tracks in queue**\n${queueString}`)
.setColor(embedOptions.colors.info)
.setFooter({
text: 'Page 1 of 1 (0 tracks)'
})
]
});
}
const queueLength = queue.tracks.data.length;
const totalPages = Math.ceil(queueLength / 10) || 1;
+11 -11
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -35,18 +35,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const removeTrackNumber = interaction.options.getNumber('tracknumber')!;
+12 -15
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -34,22 +34,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const durationInput = interaction.options.getString('duration');
+12 -15
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -28,22 +28,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueIsEmpty({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueIsEmpty({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
queue.tracks.shuffle();
+12 -15
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -31,22 +31,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const skipToTrack = interaction.options.getNumber('tracknumber');
+12 -22
View File
@@ -1,11 +1,12 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
import { EmbedOptions } from '../../../types/configTypes';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../../utils/validation/queueValidator';
const embedOptions: EmbedOptions = config.get('embedOptions');
@@ -27,29 +28,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
if (!queue) {
logger.debug('There is no queue.');
logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\n_Hmm.._ It seems I am not in a voice channel!`
)
.setColor(embedOptions.colors.warning)
]
});
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
if (!queue.deleted) {
+11 -11
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';
import loggerModule from '../../../services/logger';
@@ -35,18 +35,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const volume = interaction.options.getNumber('percentage');
+1 -1
View File
@@ -28,7 +28,7 @@ const command: CustomSlashCommandInteraction = {
});
if (await notValidGuildId({ interaction, executionId })) {
return Promise.resolve();
return;
}
let shardGuilds: Guild[] = [];
+1 -1
View File
@@ -29,7 +29,7 @@ const command: CustomSlashCommandInteraction = {
});
if (await notValidGuildId({ interaction, executionId })) {
return Promise.resolve();
return;
}
try {
+1 -1
View File
@@ -46,7 +46,7 @@ const command: CustomSlashCommandInteraction = {
});
if (await notValidGuildId({ interaction, executionId })) {
return Promise.resolve();
return;
}
let shardInfoList: ShardInfo[] = [];
@@ -32,7 +32,7 @@ const command: CustomSlashCommandInteraction = {
});
if (await notValidGuildId({ interaction, executionId })) {
return Promise.resolve();
return;
}
// from normal /status command
@@ -67,24 +67,18 @@ const command: CustomSlashCommandInteraction = {
return player.generateStatistics();
})
.then((results) => {
const queueCountList: number[] = [];
const trackCountList: number[] = [];
const listenerCountList: number[] = [];
results.map((result) => {
queueCountList.push(result.queues.length);
if (result.queues.length > 0) {
result.queues.map((queue) => {
trackCountList.push(queue.status.playing ? queue.tracksCount + 1 : queue.tracksCount);
listenerCountList.push(queue.listeners);
});
}
activeVoiceConnections += result.queues.length;
result.queues.map((queue) => {
totalTracks += queue.status.playing ? queue.tracksCount + 1 : queue.tracksCount;
totalListeners += queue.listeners;
});
});
activeVoiceConnections = queueCountList.reduce((acc, queueAmount) => acc + queueAmount, 0);
totalTracks = trackCountList.reduce((acc, trackAmount) => acc + trackAmount, 0);
totalListeners = listenerCountList.reduce((acc, listenerAmount) => acc + listenerAmount, 0);
logger.debug('Successfully fetched player statistics from shards.');
})
.catch((error) => {
logger.error(error, 'Failed to fetch player statistics from shards.');
});
logger.debug('Fetching client values from all shards.');
@@ -3,8 +3,10 @@ import config from 'config';
import loggerModule from '../../services/logger';
import { EmbedOptions } from '../../types/configTypes';
import { CustomComponentInteraction } from '../../types/interactionTypes';
import { useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember } from 'discord.js';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../utils/validation/queueValidator';
const embedOptions: EmbedOptions = config.get('embedOptions');
const component: CustomComponentInteraction = {
@@ -20,7 +22,19 @@ const component: CustomComponentInteraction = {
logger.debug('Received disable confirmation.');
const queue = useQueue(interaction.guild!.id)!;
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
for (const validator of validators) {
if (await validator()) {
return;
}
}
// Reset filters before enabling provided filters
if (queue.filters.ffmpeg.filters.length > 0) {
@@ -3,8 +3,10 @@ import config from 'config';
import loggerModule from '../../services/logger';
import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes';
import { CustomComponentInteraction } from '../../types/interactionTypes';
import { QueueFilters, useQueue } from 'discord-player';
import { GuildQueue, QueueFilters, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, StringSelectMenuInteraction } from 'discord.js';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../utils/validation/queueValidator';
const embedOptions: EmbedOptions = config.get('embedOptions');
const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');
@@ -22,7 +24,19 @@ const component: CustomComponentInteraction = {
logger.debug('Received select menu confirmation.');
const selectMenuInteraction = interaction as StringSelectMenuInteraction;
const queue = useQueue(selectMenuInteraction.guild!.id)!;
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
for (const validator of validators) {
if (await validator()) {
return;
}
}
queue.filters.ffmpeg.setInputArgs([
'-threads',
@@ -42,10 +56,10 @@ const component: CustomComponentInteraction = {
logger.debug('Reset queue filters.');
}
// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
if (
(selectMenuInteraction.values.includes('bassboost_low') || selectMenuInteraction.values.includes('bassboost')) &&
(selectMenuInteraction.values.includes('bassboost_low') ||
selectMenuInteraction.values.includes('bassboost')) &&
!selectMenuInteraction.values.includes('normalizer')
) {
selectMenuInteraction.values.push('normalizer');
@@ -1,10 +1,12 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember } from 'discord.js';
import loggerModule from '../../services/logger';
import { EmbedOptions } from '../../types/configTypes';
import { CustomComponentInteraction } from '../../types/interactionTypes';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator';
const embedOptions: EmbedOptions = config.get('embedOptions');
const component: CustomComponentInteraction = {
@@ -20,7 +22,20 @@ const component: CustomComponentInteraction = {
logger.debug(`Received skip confirmation for track id ${referenceId}.`);
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
for (const validator of validators) {
if (await validator()) {
return;
}
}
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
logger.debug('Tried skipping track but there was no queue.');
+9 -9
View File
@@ -1,5 +1,5 @@
import { GuildQueue, Player } from 'discord-player';
import { ChatInputCommandInteraction } from 'discord.js';
import { ChatInputCommandInteraction, MessageComponentInteraction } from 'discord.js';
import { ExtendedClient } from './clientTypes';
@@ -24,18 +24,18 @@ export interface CustomEvent {
}
export interface NotInVoiceChannelParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
executionId: string;
}
export interface NotInSameVoiceChannelParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
queue: GuildQueue;
executionId: string;
}
export interface NotValidGuildIdParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
executionId: string;
}
@@ -45,30 +45,30 @@ export interface TransformQueryParams {
}
export interface QueueDoesNotExistParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
queue: GuildQueue;
executionId: string;
}
export interface QueueNoCurrentTrackParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
queue: GuildQueue;
executionId: string;
}
export interface QueueIsEmptyParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
queue: GuildQueue;
executionId: string;
}
export interface CannotJoinVoiceOrTalkParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
executionId: string;
}
export interface CannotSendMessageInChannelParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
executionId: string;
}
+18 -14
View File
@@ -20,22 +20,11 @@ const logger = loggerModule.child({
executionId: executionId
});
//TODO: Fix deploying not working with require()
const slashCommands: SlashCommandBuilder[] = [];
const systemCommands: SlashCommandBuilder[] = [];
const commandFolders = fs.readdirSync(path.resolve('./dist/commands'));
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(path.resolve(`./dist/commands/${folder}`))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
/* eslint-disable @typescript-eslint/no-var-requires */
const command = require(`../commands/${folder}/${file}`);
command.isSystemCommand
? systemCommands.push(command.data.toJSON())
: slashCommands.push(command.data.toJSON());
}
}
const commandFolders = fs.readdirSync(path.resolve('./dist/interactions/commands'));
if (!process.env.DISCORD_BOT_TOKEN) {
throw new Error('DISCORD_BOT_TOKEN environment variable is not set.');
@@ -52,6 +41,21 @@ const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_TOKEN)
logger.error(
'Missing required environment variables for deployment.\nPlease provide valid DISCORD_APPLICATION_ID and DISCORD_BOT_TOKEN in .env file.'
);
process.exit(1);
}
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(path.resolve(`./dist/interactions/commands/${folder}`))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const commandModule = await import(`../interactions/commands/${folder}/${file}`);
const command = commandModule.default;
command.isSystemCommand
? systemCommands.push(command.data.toJSON())
: slashCommands.push(command.data.toJSON());
}
}
try {
+2 -2
View File
@@ -15,7 +15,7 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
try {
if (client.shard?.ids[0] !== 0) {
return Promise.resolve();
return;
}
let guildCount = 0;
@@ -129,7 +129,7 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
sites.map((site) => {
if (!site.enabled) {
// return if site is disabled
return Promise.resolve();
return;
}
const options = {
+1 -1
View File
@@ -18,7 +18,7 @@ export const startLoadTest = async ({ client, executionId }: StartLoadTestParams
if (!loadTestOptions.enabled) {
logger.debug('Load test is disabled in options, cancelling.');
return Promise.resolve();
return;
}
const channelIds = loadTestOptions.channelIdsToJoin;
+9 -6
View File
@@ -1,5 +1,5 @@
import config from 'config';
import { EmbedBuilder, GuildMember, TextChannel, VoiceChannel } from 'discord.js';
import { EmbedBuilder, GuildMember, InteractionType, TextChannel, VoiceChannel } from 'discord.js';
import loggerModule from '../../services/logger';
import { EmbedOptions } from '../../types/configTypes';
@@ -17,6 +17,8 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Cannot
});
const channel = 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({
@@ -30,7 +32,7 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Cannot
});
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 '${interactionIdentifier}' but the bot had no permission to join/speak in the voice channel.`
);
return true;
}
@@ -50,10 +52,13 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }: C
const channel = 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 command '${interaction.commandName}' but the bot had no permission to send reply in text channel.`
`User tried to use interaction '${interactionIdentifier}' but the bot has permission to send reply in text channel.`
);
try {
@@ -65,7 +70,7 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }: C
logger.debug(
'Sending ephemeral message in channel about insufficient permissions to send message in channel.'
);
return await interaction.editReply({
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
@@ -74,8 +79,6 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }: C
.setColor(embedOptions.colors.warning)
]
});
} else {
return Promise.resolve();
}
} catch (error) {
if (error instanceof Error) {
+14 -7
View File
@@ -1,11 +1,9 @@
import config from 'config';
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, InteractionType } from 'discord.js';
import loggerModule from '../../services/logger';
import { EmbedOptions } from '../../types/configTypes';
import {
QueueDoesNotExistParams, QueueIsEmptyParams, QueueNoCurrentTrackParams
} from '../../types/utilTypes';
import { QueueDoesNotExistParams, QueueIsEmptyParams, QueueNoCurrentTrackParams } from '../../types/utilTypes';
const embedOptions: EmbedOptions = config.get('embedOptions');
export const queueDoesNotExist = async ({ interaction, queue, executionId }: QueueDoesNotExistParams) => {
@@ -18,6 +16,9 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }: Que
guildId: interaction.guild?.id
});
const interactionIdentifier =
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
if (!queue) {
await interaction.editReply({
embeds: [
@@ -29,7 +30,7 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }: Que
]
});
logger.debug(`User tried to use command '${interaction.commandName}' but there was no queue.`);
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no queue.`);
return true;
}
@@ -46,6 +47,9 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: Q
guildId: interaction.guild?.id
});
const interactionIdentifier =
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
if (!queue.currentTrack) {
await interaction.editReply({
embeds: [
@@ -57,7 +61,7 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: Q
]
});
logger.debug(`User tried to use command '${interaction.commandName}' but there was no current track.`);
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no current track.`);
return true;
}
@@ -74,6 +78,9 @@ export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsE
guildId: interaction.guild?.id
});
const interactionIdentifier =
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
if (queue.tracks.data.length === 0) {
await interaction.editReply({
embeds: [
@@ -85,7 +92,7 @@ export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsE
]
});
logger.debug(`User tried to use command '${interaction.commandName}' but there was no tracks in the queue.`);
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no tracks in the queue.`);
return true;
}
@@ -1,5 +1,5 @@
import config from 'config';
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, InteractionType } from 'discord.js';
import loggerModule from '../../services/logger';
import { EmbedOptions, SystemOptions } from '../../types/configTypes';
@@ -17,19 +17,22 @@ export const notValidGuildId = async ({ interaction, executionId }: NotValidGuil
guildId: interaction.guild?.id
});
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(
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command **\`/${interaction.commandName}\`** cannot be executed in this server.`
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command **\`/${interactionIdentifier}\`** cannot be executed in this server.`
)
.setColor(embedOptions.colors.warning)
]
});
logger.debug(
`User tried to use command '${interaction.commandName}' but system command cannot be executed in the specified guild.`
`User tried to use command '${interactionIdentifier}' but system command cannot be executed in the specified guild.`
);
return true;
}
@@ -1,5 +1,5 @@
import config from 'config';
import { EmbedBuilder, GuildMember } from 'discord.js';
import { EmbedBuilder, GuildMember, InteractionType } from 'discord.js';
import loggerModule from '../../services/logger';
import { EmbedOptions } from '../../types/configTypes';
@@ -16,6 +16,9 @@ export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoice
guildId: interaction.guild?.id
});
const interactionIdentifier =
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
if (interaction.member instanceof GuildMember && !interaction.member.voice.channel) {
await interaction.editReply({
embeds: [
@@ -27,7 +30,7 @@ export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoice
]
});
logger.debug(`User tried to use command '${interaction.commandName}' but was not in a voice channel.`);
logger.debug(`User tried to use command '${interactionIdentifier}' but was not in a voice channel.`);
return true;
}
@@ -44,6 +47,9 @@ export const notInSameVoiceChannel = async ({ interaction, queue, executionId }:
guildId: interaction.guild?.id
});
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 false;
@@ -63,7 +69,7 @@ export const 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 '${interactionIdentifier}' but was not in the same voice channel.`);
return true;
}