From 671623dca7c4ba9d74b793ceb4d1956961ab9a11 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 27 Mar 2024 18:12:32 +0200 Subject: [PATCH] SameVC inhibitor --- @types/DiscordClient.d.ts | 11 +++++-- src/client/components/commands/Queue.ts | 32 ++++++++++++++----- src/client/components/commands/Skip.ts | 9 ++---- src/client/components/commands/Volume.ts | 9 ++---- src/client/components/inhibitors/GuildOnly.ts | 3 +- src/client/components/inhibitors/SameVC.ts | 29 +++++++++++++++++ 6 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 src/client/components/inhibitors/SameVC.ts diff --git a/@types/DiscordClient.d.ts b/@types/DiscordClient.d.ts index 013f56d..5a59e5e 100644 --- a/@types/DiscordClient.d.ts +++ b/@types/DiscordClient.d.ts @@ -19,7 +19,7 @@ export type ClientOptions = { music: MusicPlayerOptions } -export type ComponentType = 'command' | 'module' | 'observer' | 'inhibitor' | 'setting'; +export type ComponentType = 'command' | 'module' | 'observer' | 'inhibitor' | 'setting' | 'downloader'; export type ComponentOptions = { type: ComponentType, @@ -35,7 +35,8 @@ export type CommandDefinition = { guildOnly?: boolean, help?: string, limited?: Snowflake[], - showUsage?: boolean + showUsage?: boolean, + sameVc?: boolean } & Omit export type ObserverOptions = { @@ -46,7 +47,11 @@ export type ObserverOptions = { export type InhibitorOptions = { priority?: number, silent?: boolean -}& Omit +} & Omit + +export type DownloaderOptions = { + // +} & Omit export type InhibitorResponse = { error: T, diff --git a/src/client/components/commands/Queue.ts b/src/client/components/commands/Queue.ts index c9c053b..bc4e247 100644 --- a/src/client/components/commands/Queue.ts +++ b/src/client/components/commands/Queue.ts @@ -1,7 +1,7 @@ import { Message } from 'discord.js'; import Command from '../../../interfaces/Command.js'; import DiscordClient from '../../DiscordClient.js'; -import { CommandOpts } from '@navy.gif/commandparser'; +import { CommandOpts, OptionType } from '@navy.gif/commandparser'; class QueueCommand extends Command { @@ -9,14 +9,16 @@ class QueueCommand extends Command { super(client, { name: 'queue', - showUsage: true, options: [{ name: 'artist', flag: true }, { - name: 'song', - required: true - }] + name: 'id', + flag: true, + type: OptionType.INTEGER + }, { + name: 'song' + }], }); } @@ -24,16 +26,30 @@ class QueueCommand extends Command { const { member, guild } = message; const { me } = guild.members; + + if (!Object.keys(args).length) + { + const { queue } = this.client.musicPlayer; + if (!queue.length) + return 'Queue empty'; + return ` + **Music queue:**\n${queue.map(entry => `\t\\- **${entry.title}** by ${entry.artist}`).join('\n')} + `; + } + if (!member?.voice || member.voice.channelId !== me?.voice.channelId) return 'Only vc participants can queue songs'; + const query = { - title: args.song!.value as string, + title: args.song?.value as string | undefined, artist: args.artist?.value as string | undefined, + id: args.id?.value as number | undefined }; - const result = this.client.musicPlayer.queue(query); + + const result = this.client.musicPlayer.queueSong(query); if (!result) return 'Query yielded no results'; - return `Song **${result.title}** by ${result.arist} queued`; + return `Song **${result.title}** by ${result.artist} queued`; } } diff --git a/src/client/components/commands/Skip.ts b/src/client/components/commands/Skip.ts index 11c16b2..17f15fa 100644 --- a/src/client/components/commands/Skip.ts +++ b/src/client/components/commands/Skip.ts @@ -9,17 +9,14 @@ class SkipCommand extends Command super(client, { name: 'skip', guildOnly: true, - restricted: true + restricted: true, + sameVc: true }); } async execute (message: Message) { - const { member, author, guild } = message; - const { me } = guild.members; - if (!member?.voice || member.voice.channelId !== me?.voice.channelId) - return 'Only vc participants can adjust volume'; - + const { author } = message; this.logger.info(`${author.username} (${author.id}) skipped a song`); this.client.musicPlayer.playNext(); return 'Song skipped'; diff --git a/src/client/components/commands/Volume.ts b/src/client/components/commands/Volume.ts index c79d389..666e7b4 100644 --- a/src/client/components/commands/Volume.ts +++ b/src/client/components/commands/Volume.ts @@ -19,7 +19,8 @@ class VolumeCommand extends Command } ], guildOnly: true, - restricted: true + restricted: true, + sameVc: true, }); } @@ -29,11 +30,7 @@ class VolumeCommand extends Command if (!volume) return `Volume is currently at ${this.client.musicPlayer.volume}`; - const { member, author, guild } = message; - const { me } = guild.members; - if (!member?.voice || member.voice.channelId !== me?.voice.channelId) - return 'Only vc participants can adjust volume'; - + const { author } = message; this.logger.info(`${author.username} (${author.id}) is adjusting volume to ${volume.value}`); this.client.musicPlayer.volume = (volume.value as number); return `Volume set to ${volume.value}`; diff --git a/src/client/components/inhibitors/GuildOnly.ts b/src/client/components/inhibitors/GuildOnly.ts index fcbf4b7..d24af5c 100644 --- a/src/client/components/inhibitors/GuildOnly.ts +++ b/src/client/components/inhibitors/GuildOnly.ts @@ -9,7 +9,8 @@ class GuildOnlyInhibitor extends Inhibitor constructor (client: DiscordClient) { super(client, { - name: 'GuildOnly' + name: 'GuildOnly', + priority: 4 }); } diff --git a/src/client/components/inhibitors/SameVC.ts b/src/client/components/inhibitors/SameVC.ts new file mode 100644 index 0000000..90c74f6 --- /dev/null +++ b/src/client/components/inhibitors/SameVC.ts @@ -0,0 +1,29 @@ +import { Message } from 'discord.js'; +import Inhibitor from '../../../interfaces/Inhibitor.js'; +import DiscordClient from '../../DiscordClient.js'; +import { InhibitorResponse } from '../../../../@types/DiscordClient.js'; + +class SameVCInhibitor extends Inhibitor +{ + constructor (client: DiscordClient) + { + super(client, { + name: 'SameVC', + }); + } + + override async execute (message: Message): Promise + { + const { member, guild } = message; + if (!guild) + return super._fail('This command cannot be executed outside of a server'); + + const { me } = guild.members; + if (!member?.voice || member.voice.channelId !== me?.voice.channelId) + super._fail('Only vc participants can queue songs'); + return super._succeed(); + } + +} + +export default SameVCInhibitor; \ No newline at end of file