mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: refactor volume command
This commit is contained in:
1 parent
e3efa48ade
commit
fca1a90489
1 file changed
+67
-47
@@ -1,9 +1,10 @@
|
|||||||
import { GuildQueue, useQueue } from 'discord-player';
|
import { GuildQueue, useQueue } from 'discord-player';
|
||||||
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||||
import { checkQueueExists } from '../../../utils/validation/queueValidator';
|
import { checkQueueExists } from '../../../utils/validation/queueValidator';
|
||||||
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
||||||
|
import { Logger } from 'pino';
|
||||||
|
|
||||||
class VolumeCommand extends BaseSlashCommandInteraction {
|
class VolumeCommand extends BaseSlashCommandInteraction {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -35,67 +36,86 @@ class VolumeCommand extends BaseSlashCommandInteraction {
|
|||||||
const volume: number = interaction.options.getNumber('percentage')!;
|
const volume: number = interaction.options.getNumber('percentage')!;
|
||||||
|
|
||||||
if (!volume && volume !== 0) {
|
if (!volume && volume !== 0) {
|
||||||
const currentVolume: number = queue.node.volume;
|
return await this.handleShowCurrentVolume(queue, logger, interaction);
|
||||||
|
|
||||||
logger.debug('No volume input was provided, showing current volume.');
|
|
||||||
|
|
||||||
logger.debug('Responding with info embed.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setDescription(
|
|
||||||
`**${
|
|
||||||
currentVolume === 0
|
|
||||||
? this.embedOptions.icons.volumeIsMuted
|
|
||||||
: this.embedOptions.icons.volume
|
|
||||||
} Playback volume**\nThe playback volume is currently set to **\`${currentVolume}%\`**.`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.info)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
} else if (volume > 100 || volume < 0) {
|
} else if (volume > 100 || volume < 0) {
|
||||||
logger.debug('Volume specified was higher than 100% or lower than 0%.');
|
return await this.handleInvalidVolumeInput(volume, logger, interaction);
|
||||||
|
|
||||||
logger.debug('Responding with warning embed.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setDescription(
|
|
||||||
`**${this.embedOptions.icons.warning} Oops!**\nYou cannot set the volume to **\`${volume}%\`**, please pick a value betwen **\`1%\`** and **\`100%\`**.`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.warning)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
queue.node.setVolume(volume);
|
return await this.handleValidVolumeInput(volume, queue, logger, interaction);
|
||||||
logger.debug(`Set volume to ${volume}%.`);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (volume === 0) {
|
private async handleShowCurrentVolume(queue: GuildQueue, logger: Logger, interaction: ChatInputCommandInteraction) {
|
||||||
logger.debug('Responding with success embed.');
|
const currentVolume: number = queue.node.volume;
|
||||||
return await interaction.editReply({
|
logger.debug('No volume input was provided, showing current volume.');
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setAuthor(this.getEmbedUserAuthor(interaction))
|
|
||||||
.setDescription(
|
|
||||||
`**${this.embedOptions.icons.volumeMuted} Audio muted**\nPlayback audio has been muted, because volume was set to **\`${volume}%\`**.`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.success)
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const currentVolumeIcon =
|
||||||
|
currentVolume === 0 ? this.embedOptions.icons.volumeIsMuted : this.embedOptions.icons.volume;
|
||||||
|
|
||||||
|
logger.debug('Responding with info embed.');
|
||||||
|
return await interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
new EmbedBuilder()
|
||||||
|
.setDescription(
|
||||||
|
`**${currentVolumeIcon} Playback volume**\n` +
|
||||||
|
`The playback volume is currently set to **\`${currentVolume}%\`**.`
|
||||||
|
)
|
||||||
|
.setColor(this.embedOptions.colors.info)
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleInvalidVolumeInput(volume: number, logger: Logger, interaction: ChatInputCommandInteraction) {
|
||||||
|
logger.debug('Volume specified was higher than 100% or lower than 0%.');
|
||||||
|
|
||||||
|
logger.debug('Responding with warning embed.');
|
||||||
|
return await interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
new EmbedBuilder()
|
||||||
|
.setDescription(
|
||||||
|
`**${this.embedOptions.icons.warning} Oops!**\n` +
|
||||||
|
`You cannot set the volume to **\`${volume}%\`**, please pick a value betwen **\`1%\`** and **\`100%\`**.`
|
||||||
|
)
|
||||||
|
.setColor(this.embedOptions.colors.warning)
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleValidVolumeInput(
|
||||||
|
volume: number,
|
||||||
|
queue: GuildQueue,
|
||||||
|
logger: Logger,
|
||||||
|
interaction: ChatInputCommandInteraction
|
||||||
|
) {
|
||||||
|
queue.node.setVolume(volume);
|
||||||
|
logger.debug(`Set volume to ${volume}%.`);
|
||||||
|
|
||||||
|
if (volume === 0) {
|
||||||
logger.debug('Responding with success embed.');
|
logger.debug('Responding with success embed.');
|
||||||
return await interaction.editReply({
|
return await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
.setAuthor(this.getEmbedUserAuthor(interaction))
|
.setAuthor(this.getEmbedUserAuthor(interaction))
|
||||||
.setDescription(
|
.setDescription(
|
||||||
`**${this.embedOptions.icons.volumeChanged} Volume changed**\nPlayback volume has been changed to **\`${volume}%\`**.`
|
`**${this.embedOptions.icons.volumeMuted} Audio muted**\n` +
|
||||||
|
`Playback audio has been muted, because volume was set to **\`${volume}%\`**.`
|
||||||
)
|
)
|
||||||
.setColor(this.embedOptions.colors.success)
|
.setColor(this.embedOptions.colors.success)
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.debug('Responding with success embed.');
|
||||||
|
return await interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
new EmbedBuilder()
|
||||||
|
.setAuthor(this.getEmbedUserAuthor(interaction))
|
||||||
|
.setDescription(
|
||||||
|
`**${this.embedOptions.icons.volumeChanged} Volume changed**\n` +
|
||||||
|
`Playback volume has been changed to **\`${volume}%\`**.`
|
||||||
|
)
|
||||||
|
.setColor(this.embedOptions.colors.success)
|
||||||
|
]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user