diff options
| author | Max Isom <[email protected]> | 2021-11-24 11:42:38 -0600 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2021-11-24 11:42:38 -0600 |
| commit | 71b62cb3aa7cd0b5d128ce9e2b666ff182a83225 (patch) | |
| tree | de50243cb877448b17d579020a04e216e67bb356 /src | |
| parent | 34e45d6273463e232168c2f5e273728d594e4d77 (diff) | |
| parent | f146a2a57c83aa37b4601cf093acb1cfa700a41e (diff) | |
| download | muse-71b62cb3aa7cd0b5d128ce9e2b666ff182a83225.tar.xz muse-71b62cb3aa7cd0b5d128ce9e2b666ff182a83225.zip | |
Merge branch 'master' into feature/better-file-caching
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands/play.ts | 2 | ||||
| -rw-r--r-- | src/commands/remove.ts | 76 | ||||
| -rw-r--r-- | src/inversify.config.ts | 6 | ||||
| -rw-r--r-- | src/services/player.ts | 4 |
4 files changed, 85 insertions, 3 deletions
diff --git a/src/commands/play.ts b/src/commands/play.ts index a907a40..0582ebe 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -37,6 +37,7 @@ export default class implements Command { this.getSongs = getSongs; } + // eslint-disable-next-line complexity public async execute(msg: Message, args: string[]): Promise<void> { const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!); @@ -130,7 +131,6 @@ export default class implements Command { if (song) { newSongs.push(song); } else { - console.log(_); await res.stop(errorMsg('that doesn\'t exist')); return; } diff --git a/src/commands/remove.ts b/src/commands/remove.ts new file mode 100644 index 0000000..9c40a71 --- /dev/null +++ b/src/commands/remove.ts @@ -0,0 +1,76 @@ +import {Message} from 'discord.js'; +import {inject, injectable} from 'inversify'; +import {TYPES} from '../types.js'; +import PlayerManager from '../managers/player.js'; +import Command from '.'; +import errorMsg from '../utils/error-msg.js'; + +@injectable() +export default class implements Command { + public name = 'remove'; + public aliases = ['rm']; + public examples = [ + ['remove 1', 'removes the next song in the queue'], + ['rm 5-7', 'remove every song in range 5 - 7 (inclusive) from the queue'], + ]; + + private readonly playerManager: PlayerManager; + + constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) { + this.playerManager = playerManager; + } + + public async execute(msg: Message, args: string []): Promise<void> { + const player = this.playerManager.get(msg.guild!.id); + + if (args.length === 0) { + await msg.channel.send(errorMsg('missing song position or range')); + return; + } + + const reg = /^(\d+)-(\d+)$|^(\d+)$/g; // Expression has 3 groups: x-y or z. x-y is range, z is a single digit. + const match = reg.exec(args[0]); + + if (match === null) { + await msg.channel.send(errorMsg('incorrect format')); + return; + } + + if (match[3] === undefined) { // 3rd group (z) doesn't exist -> a range + const range = [parseInt(match[1], 10), parseInt(match[2], 10)]; + + if (range[0] < 1) { + await msg.channel.send(errorMsg('position must be greater than 0')); + return; + } + + if (range[1] > player.queueSize()) { + await msg.channel.send(errorMsg('position is outside of the queue\'s range')); + return; + } + + if (range[0] < range[1]) { + player.removeFromQueue(range[0], range[1] - range[0] + 1); + } else { + await msg.channel.send(errorMsg('range is backwards')); + return; + } + } else { // 3rd group exists -> just one song + const index = parseInt(match[3], 10); + + if (index < 1) { + await msg.channel.send(errorMsg('position must be greater than 0')); + return; + } + + if (index > player.queueSize()) { + await msg.channel.send(errorMsg('position is outside of the queue\'s range')); + return; + } + + player.removeFromQueue(index, 1); + } + + await msg.channel.send(':wastebasket: removed'); + } +} diff --git a/src/inversify.config.ts b/src/inversify.config.ts index c277f7a..6ec1ba2 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -21,7 +21,8 @@ import ForwardSeek from './commands/fseek.js'; import Help from './commands/help.js'; import Pause from './commands/pause.js'; import Play from './commands/play.js'; -import QueueCommad from './commands/queue.js'; +import QueueCommand from './commands/queue.js'; +import Remove from './commands/remove.js'; import Seek from './commands/seek.js'; import Shortcuts from './commands/shortcuts.js'; import Shuffle from './commands/shuffle.js'; @@ -61,7 +62,8 @@ container.bind<NaturalLanguage>(TYPES.Services.NaturalLanguage).to(NaturalLangua Help, Pause, Play, - QueueCommad, + QueueCommand, + Remove, Seek, Shortcuts, Shuffle, diff --git a/src/services/player.ts b/src/services/player.ts index 2738a91..5f4d368 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -270,6 +270,10 @@ export default class { this.queue = newQueue; } + removeFromQueue(index: number, amount = 1): void { + this.queue.splice(this.queuePosition + index, amount); + } + removeCurrent(): void { this.queue = [...this.queue.slice(0, this.queuePosition), ...this.queue.slice(this.queuePosition + 1)]; } |
