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

Commit

Permalink
fix: annotated the rest of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Aug 27, 2023
1 parent ef6f7be commit 45d6ffa
Show file tree
Hide file tree
Showing 22 changed files with 592 additions and 378 deletions.
2 changes: 1 addition & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module.exports.playerOptions = {
leaveOnEmpty: true,
leaveOnEmptyCooldown: 1_800_000,
leaveOnEnd: true,
leaveonEndCooldown: 1_800_000,
leaveOnEndCooldown: 1_800_000,
leaveOnStop: true,
leaveOnStopCooldown: 1_800_000,
defaultVolume: 50,
Expand Down
11 changes: 9 additions & 2 deletions src/commands/info/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { BotOptions, EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions');
const botOptions: BotOptions = config.get('botOptions');
module.exports = {
isNew: false,
isBeta: false,
data: new SlashCommandBuilder()
.setName('help')
.setDescription('Show a list of commands and their usage.')
Expand All @@ -19,10 +21,15 @@ module.exports = {
module: 'slashCommand',
name: '/help',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});

if (!client || !client.commands) {
logger.error('Client is undefined or does not have commands property.');
return;
}

const commandList = client.commands
.filter((command) => {
// don't include system commands
Expand Down
24 changes: 15 additions & 9 deletions src/commands/info/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from 'config';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { Collection, EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js';
import osu from 'node-os-utils';

// @ts-ignore
Expand All @@ -24,8 +24,8 @@ module.exports = {
module: 'slashCommand',
name: '/status',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});

const uptimeString = await getUptimeFormatted({ executionId });
Expand All @@ -38,15 +38,20 @@ module.exports = {
let totalTracks = 0;
let totalListeners = 0;

if (!client || !client.shard) {
logger.error('Client is undefined or does not have shard property.');
return;
}

await client.shard
.broadcastEval(() => {
/* eslint-disable no-undef */
return player.generateStatistics();
})
.then((results) => {
const queueCountList = [];
const trackCountList = [];
const listenerCountList = [];
const queueCountList: number[] = [];
const trackCountList: number[] = [];
const listenerCountList: number[] = [];
results.map((result) => {
queueCountList.push(result.queues.length);
if (result.queues.length > 0) {
Expand All @@ -70,10 +75,11 @@ module.exports = {
await client.shard
.fetchClientValues('guilds.cache')
.then((results) => {
results.map((guildCache) => {
const guildCaches = results as Collection<string, Guild>[];
guildCaches.map((guildCache) => {
if (guildCache) {
guildCount += guildCache.length;
memberCount += guildCache.reduce((acc, guildCache) => acc + guildCache.memberCount, 0);
guildCount += guildCache.size;
memberCount += guildCache.reduce((acc, guild) => acc + guild.memberCount, 0);
}
});

Expand Down
183 changes: 112 additions & 71 deletions src/commands/player/filters.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import config from 'config';
import { useQueue } from 'discord-player';
import { NodeResolvable, QueueFilters, useQueue } from 'discord-player';
import {
ActionRowBuilder,
APIActionRowComponent,
APIMessageActionRowComponent,
ButtonBuilder,
ButtonStyle,
ComponentType,
EmbedBuilder,
GuildMember,
Interaction,
SlashCommandBuilder,
StringSelectMenuBuilder,
StringSelectMenuOptionBuilder
} from 'discord.js';

import loggerModule from '../../services/logger';
import { CommandParams } from '../../types/commandTypes';
import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes';
import { CommandParams, CustomError } from '../../types/commandTypes';
import { EmbedOptions, FFmpegFilterOption, FFmpegFilterOptions } from '../../types/configTypes';
import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';

Expand All @@ -31,15 +36,15 @@ module.exports = {
module: 'slashCommand',
name: '/filters',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});

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

const queue = useQueue(interaction.guild.id);
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
Expand All @@ -53,12 +58,12 @@ module.exports = {
return;
}

const filterOptions = [];
const filterOptions: StringSelectMenuOptionBuilder[] = [];

ffmpegFilterOptions.availableFilters.forEach((filter) => {
ffmpegFilterOptions.availableFilters.forEach((filter: FFmpegFilterOption) => {
let isEnabled = false;

if (queue.filters.ffmpeg.filters.includes(filter.value)) {
if (queue.filters.ffmpeg.filters.includes(filter.value as keyof QueueFilters)) {
isEnabled = true;
}

Expand All @@ -77,31 +82,37 @@ module.exports = {
.setPlaceholder('Select multiple options.')
.setMinValues(0)
.setMaxValues(filterOptions.length)
.addOptions(filterOptions);

const filterActionRow = new ActionRowBuilder().addComponents(filterSelect);

const disableFiltersActionRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('disable-filters')
.setLabel('Disable all filters')
.setStyle('Secondary')
.setEmoji(embedOptions.icons.disable)
);
.addOptions(filterOptions)
.toJSON();

const filterActionRow: APIActionRowComponent<APIMessageActionRowComponent> = {
type: ComponentType.ActionRow,
components: [filterSelect]
};

const disableButton = new ButtonBuilder()
.setCustomId('disable-filters')
.setLabel('Disable all filters')
.setStyle(ButtonStyle.Secondary)
.setEmoji(embedOptions.icons.disable)
.toJSON();

const disableFiltersActionRow: APIActionRowComponent<APIMessageActionRowComponent> = {
type: ComponentType.ActionRow,
components: [disableButton]
};

logger.debug('Sending info embed with action row components.');
const response = await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**Toggle filters** ${embedOptions.icons.beta}\nEnable or disable audio filters for playback from the menu.`
)
.setDescription('**Toggle filters**\nEnable or disable audio filters for playback from the menu.')
.setColor(embedOptions.colors.info)
],
components: [filterActionRow, disableFiltersActionRow]
});

const collectorFilter = (i) => i.user.id === interaction.user.id;
const collectorFilter = (i: Interaction) => i.user.id === interaction.user.id;
try {
const confirmation = await response.awaitMessageComponent({
filter: collectorFilter,
Expand Down Expand Up @@ -130,14 +141,64 @@ module.exports = {
logger.debug('Reset queue filters.');
}

if (confirmation.customId === 'disable-filters' || confirmation.values.length === 0) {
let authorName: string;

if (interaction.member instanceof GuildMember) {
authorName = interaction.member.nickname || interaction.user.username;
} else {
authorName = interaction.user.username;
}

if ('values' in confirmation) {
// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
if (
(confirmation.values.includes('bassboost_low') || confirmation.values.includes('bassboost')) &&
!confirmation.values.includes('normalizer')
) {
confirmation.values.push('normalizer');
}

// Enable provided filters
queue.filters.ffmpeg.toggle(confirmation.values as (keyof QueueFilters)[]);
logger.debug(`Enabled filters ${confirmation.values.join(', ')}.`);

logger.debug('Responding with success embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: interaction.member.nickname || interaction.user.username,
iconURL: interaction.user.avatarURL()
name: authorName,
iconURL: interaction.user.avatarURL() || ''
})
.setDescription(
`**${
embedOptions.icons.success
} Filters toggled**\nNow using these filters:\n${confirmation.values
.map((enabledFilter: string) => {
const filter = ffmpegFilterOptions.availableFilters.find(
(filter) => enabledFilter == filter.value
);
if (!filter) {
return enabledFilter;
}
return `- **${filter.emoji} ${filter.label}**`;
})
.join('\n')}`
)
.setColor(embedOptions.colors.success)
],
components: []
});
} else if (confirmation.customId === 'disable-filters') {
logger.debug('Responding with success embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: authorName,
iconURL: interaction.user.avatarURL() || ''
})
.setDescription(
`**${embedOptions.icons.success} Disabled filters**\nAll audio filters have been disabled.`
Expand All @@ -146,53 +207,33 @@ module.exports = {
],
components: []
});
} else {
logger.warn('Unhandled component interaction response.');
}

// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
if (
(confirmation.values.includes('bassboost_low') || confirmation.values.includes('bassboost')) &&
!confirmation.values.includes('normalizer')
) {
confirmation.values.push('normalizer');
}

// Enable provided filters
queue.filters.ffmpeg.toggle(confirmation.values);
logger.debug(`Enabled filters ${confirmation.values.join(', ')}.`);

logger.debug('Responding with success embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: interaction.member.nickname || interaction.user.username,
iconURL: interaction.user.avatarURL()
})
.setDescription(
`**${
embedOptions.icons.success
} Filters toggled**\nNow using these filters:\n${confirmation.values
.map((enabledFilter) => {
const filter = ffmpegFilterOptions.availableFilters.find(
(filter) => enabledFilter == filter.value
);
return `- **${filter.emoji} ${filter.label}**`;
})
.join('\n')}`
)
.setColor(embedOptions.colors.success)
],
components: []
});
} catch (error) {
if (error.code === 'InteractionCollectorError') {
logger.debug('Interaction response timed out.');
if (error instanceof CustomError) {
if (error.code === 'InteractionCollectorError') {
logger.debug('Interaction response timed out.');
return;
}

if (error.message === 'Collector received no interactions before ending with reason: time') {
logger.debug('Interaction response timed out.');
return;
}

logger.error(error, 'Unhandled error while awaiting or handling component interaction.');
return;
} else {
if (
error instanceof Error &&
error.message === 'Collector received no interactions before ending with reason: time'
) {
logger.debug('Interaction response timed out.');
return;
}
throw error;
}

logger.error(error, 'Unhandled error while awaiting or handling component interaction.');
return;
}
}
};
Loading

0 comments on commit 45d6ffa

Please # to comment.