Skip to content

Commit

Permalink
fix: User being able to check volume without being in a VC. Feedback …
Browse files Browse the repository at this point in the history
…messages.
  • Loading branch information
vxern committed Jan 2, 2023
1 parent c887fdc commit a3a2b85
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 37 deletions.
18 changes: 12 additions & 6 deletions assets/localisations/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,12 +1617,6 @@ class Commands {
'Romanian': 'Nu s-a putut găsi melodia.\n\n' +
'Încearcă să cauți melodia într-un mod diferit, sau să redai o altă melodie.',
},
mustBeInVoiceChannel: {
'English': 'To manipulate music, you must be in a voice channel.',
// TODO: Add Hungarian localisation.
'Polish': 'Aby móc kontrolować odtwarzanie muzyki, wpierw musisz się znajdywać w kanale głosowym.',
'Romanian': 'Pentru a gestiona redarea muzicii, trebuie mai întâi să întri în canal de voce.',
},
alreadyPlayingInAnotherVoiceChannel: {
'English': 'The bot is playing music in another voice channel.',
// TODO: Add Hungarian localisation.
Expand Down Expand Up @@ -2003,6 +1997,18 @@ class Commands {
'Polish': 'Bot obecnie nie odtwarza muzyki.',
'Romanian': 'Nu se redă muzică.',
},
mustBeInVoiceChannelToManipulate: {
'English': 'To manipulate music, you must be in a voice channel.',
// TODO: Add Hungarian localisation.
'Polish': 'Aby móc kontrolować odtwarzanie muzyki, wpierw musisz się znajdywać w kanale głosowym.',
'Romanian': 'Pentru a gestiona redarea muzicii, trebuie mai întâi să intri în canal de voce.',
},
mustBeInVoiceChannelToCheck: {
'English': 'To check playback-related information, you must be in a voice channel.',
// TODO: Add Hungarian localisation.
'Polish': 'Aby móc sprawdzić informacje o odtwarzaniu muzyki, wpierw musisz się znajdywać w kanale głosowym.',
'Romanian': 'Pentru a verifica informații despre redarea muzicii, trebuie mai întâi să intri în canal de voce.',
},
listings: {
'English': 'Listings',
'Hungarian': 'Elemek',
Expand Down
5 changes: 2 additions & 3 deletions src/commands/music/commands/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ function handlePausePlayback([client, bot]: [Client, Bot], interaction: Interact
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);
const isVerifiedVoiceState = verifyVoiceState(bot, interaction, controller, voiceState);
if (!isVerifiedVoiceState) return;
const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, getVoiceState(client, interaction), 'manipulate');
if (!isVoiceStateVerified) return;

if (!isOccupied(controller.player)) {
return void sendInteractionResponse(
Expand Down
4 changes: 2 additions & 2 deletions src/commands/music/commands/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function handleRequestSongListing(
const voiceState = getVoiceState(client, interaction);

const canPlay = verifyCanRequestPlayback(bot, interaction, controller, voiceState);
if (!canPlay || voiceState === undefined) return;
if (!canPlay) return;

const listing = await resolveToSongListing([client, bot], interaction, query);

Expand All @@ -99,7 +99,7 @@ async function handleRequestSongListing(
const feedbackChannelId = client.cache.channels.get(interaction.channelId!)?.id;
if (feedbackChannelId === undefined) return;

const voiceChannelId = client.cache.channels.get(voiceState.channelId!)?.id;
const voiceChannelId = client.cache.channels.get(voiceState!.channelId!)?.id;
if (voiceChannelId === undefined) return;

return void receiveNewListing(
Expand Down
4 changes: 1 addition & 3 deletions src/commands/music/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ function handleRemoveSongListing([client, bot]: [Client, Bot], interaction: Inte
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, getVoiceState(client, interaction), 'manipulate');
if (!isVoiceStateVerified) return;

if (isQueueEmpty(controller.listingQueue)) {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/music/commands/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ function handleReplayAction([client, bot]: [Client, Bot], interaction: Interacti
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, getVoiceState(client, interaction), 'manipulate');
if (!isVoiceStateVerified) return;

const [{ collection }] = parseArguments(interaction.data?.options, { collection: 'boolean' });
Expand Down
10 changes: 7 additions & 3 deletions src/commands/music/commands/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ function handleResumePlayback([client, bot]: [Client, Bot], interaction: Interac
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'manipulate',
);
if (!isVoiceStateVerified) return;

if (!isOccupied(controller.player)) {
Expand Down
10 changes: 7 additions & 3 deletions src/commands/music/commands/skip-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ async function handleSkipToTimestamp(
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'manipulate',
);
if (!isVoiceStateVerified) return;

const [{ timestamp: timestampExpression }, focused] = parseArguments(interaction.data?.options, {});
Expand Down
10 changes: 7 additions & 3 deletions src/commands/music/commands/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ function handleSkipAction([client, bot]: [Client, Bot], interaction: Interaction
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'manipulate',
);
if (!isVoiceStateVerified) return;

const data = interaction.data;
Expand Down
10 changes: 7 additions & 3 deletions src/commands/music/commands/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ function handleStopPlayback([client, bot]: [Client, Bot], interaction: Interacti
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'manipulate',
);
if (!isVoiceStateVerified) return;

if (!isOccupied(controller.player)) {
Expand Down
10 changes: 7 additions & 3 deletions src/commands/music/commands/unskip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ function handleUnskipAction([client, bot]: [Client, Bot], interaction: Interacti
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'manipulate',
);
if (!isVoiceStateVerified) return;

const data = interaction.data;
Expand Down
10 changes: 10 additions & 0 deletions src/commands/music/commands/volume/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
sendInteractionResponse,
} from 'discordeno';
import { Commands, localise } from 'logos/assets/localisations/mod.ts';
import { getVoiceState, verifyVoiceState } from 'logos/src/controllers/music.ts';
import { Client } from 'logos/src/client.ts';
import { parseArguments } from 'logos/src/interactions.ts';
import constants from 'logos/constants.ts';
Expand All @@ -20,6 +21,15 @@ function handleDisplayVolume(

const [{ show }] = parseArguments(interaction.data?.options, { show: 'boolean' });

const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'check',
);
if (!isVoiceStateVerified) return;

const locale = show ? defaultLocale : interaction.locale;

const volumeString = localise(Commands.music.options.volume.options.display.strings.volume.header, locale);
Expand Down
10 changes: 7 additions & 3 deletions src/commands/music/commands/volume/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ function handleSetVolume(
const controller = client.features.music.controllers.get(interaction.guildId!);
if (controller === undefined) return;

const voiceState = getVoiceState(client, interaction);

const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(
bot,
interaction,
controller,
getVoiceState(client, interaction),
'manipulate',
);
if (!isVoiceStateVerified) return;

const [{ volume }] = parseArguments(interaction.data?.options, { volume: 'number' });
Expand Down
12 changes: 10 additions & 2 deletions src/controllers/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,14 @@ function getVoiceState(client: Client, interaction: Interaction): VoiceState | u
return voiceState;
}

type MusicAction = 'manipulate' | 'check';

function verifyVoiceState(
bot: Bot,
interaction: Interaction,
controller: MusicController,
voiceState: VoiceState | undefined,
action: MusicAction,
): boolean {
if (voiceState === undefined || voiceState.channelId === undefined) {
sendInteractionResponse(
Expand All @@ -129,7 +132,12 @@ function verifyVoiceState(
flags: ApplicationCommandFlags.Ephemeral,
embeds: [
{
description: localise(Commands.music.options.play.strings.mustBeInVoiceChannel, interaction.locale),
description: localise(
action === 'manipulate'
? Commands.music.strings.mustBeInVoiceChannelToManipulate
: Commands.music.strings.mustBeInVoiceChannelToCheck,
interaction.locale,
),
color: constants.colors.dullYellow,
},
],
Expand Down Expand Up @@ -170,7 +178,7 @@ function verifyCanRequestPlayback(
controller: MusicController,
voiceState: VoiceState | undefined,
): boolean {
const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState);
const isVoiceStateVerified = verifyVoiceState(bot, interaction, controller, voiceState, 'manipulate');
if (!isVoiceStateVerified) return false;

if (!isQueueVacant(controller.listingQueue)) {
Expand Down

0 comments on commit a3a2b85

Please sign in to comment.