From 1ddd19dbb97f77f51e30819a24cd839ace1f073e Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sun, 3 Oct 2021 12:59:26 -0400 Subject: Only add event listeners once See #372 --- src/services/player.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/services/player.ts b/src/services/player.ts index 74f409b..742da55 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -441,13 +441,17 @@ export default class { return; } - this.voiceConnection.on('disconnect', this.onVoiceConnectionDisconnect.bind(this)); + if (this.voiceConnection.listeners('disconnect').length === 0) { + this.voiceConnection.on('disconnect', this.onVoiceConnectionDisconnect.bind(this)); + } if (!this.dispatcher) { return; } - this.dispatcher.on('speaking', this.onVoiceConnectionSpeaking.bind(this)); + if (this.dispatcher.listeners('speaking').length === 0) { + this.dispatcher.on('speaking', this.onVoiceConnectionSpeaking.bind(this)); + } } private onVoiceConnectionDisconnect(): void { -- cgit v1.2.3 From fe5f9cf9a7df3b9cc42d7165ca8566e36fa70039 Mon Sep 17 00:00:00 2001 From: Hellyson Rodrigo Parteka Date: Fri, 12 Nov 2021 16:30:18 -0300 Subject: feat: discord.js v13 --- src/bot.ts | 7 +++-- src/commands/config.ts | 9 ++++--- src/commands/help.ts | 12 ++++++--- src/commands/queue.ts | 2 +- src/commands/shortcuts.ts | 4 +-- src/events/guild-create.ts | 6 ++--- src/events/voice-state-update.ts | 5 ++-- src/inversify.config.ts | 11 ++++++-- src/services/player.ts | 57 ++++++++++++++++++++++------------------ src/utils/channels.ts | 10 +++---- 10 files changed, 73 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/bot.ts b/src/bot.ts index 5d2e97e..b49c1f6 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -11,6 +11,7 @@ import handleVoiceStateUpdate from './events/voice-state-update.js'; import errorMsg from './utils/error-msg.js'; import {isUserInVoice} from './utils/channels.js'; import Config from './services/config.js'; +import {generateDependencyReport} from '@discordjs/voice'; @injectable() export default class { @@ -34,7 +35,7 @@ export default class { commandNames.forEach(commandName => this.commands.set(commandName, command)); }); - this.client.on('message', async (msg: Message) => { + this.client.on('messageCreate', async (msg: Message) => { // Get guild settings if (!msg.guild) { return; @@ -44,7 +45,8 @@ export default class { if (!settings) { // Got into a bad state, send owner welcome message - return this.client.emit('guildCreate', msg.guild); + this.client.emit('guildCreate', msg.guild); + return; } const {prefix, channel} = settings; @@ -95,6 +97,7 @@ export default class { }); this.client.on('ready', async () => { + debug(generateDependencyReport()); console.log(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot&permissions=36752448`); }); diff --git a/src/commands/config.ts b/src/commands/config.ts index 0e5728a..8f3e0aa 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -1,4 +1,4 @@ -import {TextChannel, Message, GuildChannel} from 'discord.js'; +import {TextChannel, Message, GuildChannel, ThreadChannel} from 'discord.js'; import {injectable} from 'inversify'; import {Settings} from '../models/index.js'; import errorMsg from '../utils/error-msg.js'; @@ -20,6 +20,7 @@ export default class implements Command { if (settings) { let response = `prefix: \`${settings.prefix}\`\n`; + // eslint-disable-next-line @typescript-eslint/no-base-to-string response += `channel: ${msg.guild!.channels.cache.get(settings.channel)!.toString()}`; await msg.channel.send(response); @@ -35,7 +36,7 @@ export default class implements Command { return; } - if (msg.author.id !== msg.guild!.owner!.id) { + if (msg.author.id !== msg.guild!.ownerId) { await msg.channel.send(errorMsg('not authorized')); return; } @@ -51,7 +52,7 @@ export default class implements Command { } case 'channel': { - let channel: GuildChannel | undefined; + let channel: GuildChannel | ThreadChannel | undefined; if (args[1].includes('<#') && args[1].includes('>')) { channel = msg.guild!.channels.cache.find(c => c.id === args[1].slice(2, args[1].indexOf('>'))); @@ -59,7 +60,7 @@ export default class implements Command { channel = msg.guild!.channels.cache.find(c => c.name === args[1]); } - if (channel && channel.type === 'text') { + if (channel && channel.type === 'GUILD_TEXT') { await Settings.update({channel: channel.id}, {where: {guildId: msg.guild!.id}}); await Promise.all([ diff --git a/src/commands/help.ts b/src/commands/help.ts index f150c54..7058efd 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -1,4 +1,4 @@ -import {Message} from 'discord.js'; +import {Message, Util} from 'discord.js'; import {injectable} from 'inversify'; import Command from '.'; import {TYPES} from '../types.js'; @@ -29,7 +29,7 @@ export default class implements Command { const {prefix} = settings; - const res = this.commands.sort((a, b) => a.name.localeCompare(b.name)).reduce((content, command) => { + const res = Util.splitMessage(this.commands.sort((a, b) => a.name.localeCompare(b.name)).reduce((content, command) => { const aliases = command.aliases.reduce((str, alias, i) => { str += alias; @@ -53,9 +53,13 @@ export default class implements Command { content += '\n'; return content; - }, ''); + }, '')); + + for (const r of res) { + // eslint-disable-next-line no-await-in-loop + await msg.author.send(r); + } - await msg.author.send(res, {split: true}); await msg.react('🇩'); await msg.react('🇲'); } diff --git a/src/commands/queue.ts b/src/commands/queue.ts index 85420e8..1c0735c 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -74,7 +74,7 @@ export default class implements Command { embed.addField('Page', `${queuePage} out of ${maxQueuePage}`, false); - await msg.channel.send(embed); + await msg.channel.send({embeds: [embed]}); } else { await msg.channel.send('queue empty'); } diff --git a/src/commands/shortcuts.ts b/src/commands/shortcuts.ts index 109d5fc..929a5c1 100644 --- a/src/commands/shortcuts.ts +++ b/src/commands/shortcuts.ts @@ -55,7 +55,7 @@ export default class implements Command { const newShortcut = {shortcut: shortcutName, command, guildId: msg.guild!.id, authorId: msg.author.id}; if (shortcut) { - if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.owner!.id) { + if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.ownerId) { await msg.channel.send(errorMsg('you do\'nt have permission to do that')); return; } @@ -80,7 +80,7 @@ export default class implements Command { } // Check permissions - if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.owner!.id) { + if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.ownerId) { await msg.channel.send(errorMsg('you don\'t have permission to do that')); return; } diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts index 3bcf281..01f0910 100644 --- a/src/events/guild-create.ts +++ b/src/events/guild-create.ts @@ -9,7 +9,7 @@ const DEFAULT_PREFIX = '!'; export default async (guild: Guild): Promise => { await Settings.upsert({guildId: guild.id, prefix: DEFAULT_PREFIX}); - const owner = await guild.client.users.fetch(guild.ownerID); + const owner = await guild.client.users.fetch(guild.ownerId); let firstStep = '👋 Hi!\n'; firstStep += 'I just need to ask a few questions before you start listening to music.\n\n'; @@ -27,7 +27,7 @@ export default async (guild: Guild): Promise => { const emojiChannels: EmojiChannel[] = []; for (const [channelId, channel] of guild.channels.cache) { - if (channel.type === 'text') { + if (channel.type === 'GUILD_TEXT') { emojiChannels.push({ name: channel.name, id: channelId, @@ -65,7 +65,7 @@ export default async (guild: Guild): Promise => { await owner.send(secondStep); - const prefixResponses = await firstStepMsg.channel.awaitMessages((r: Message) => r.content.length === 1, {max: 1}); + const prefixResponses = await firstStepMsg.channel.awaitMessages({filter: (r: Message) => r.content.length === 1, max: 1}); const prefixCharacter = prefixResponses.first()!.content; diff --git a/src/events/voice-state-update.ts b/src/events/voice-state-update.ts index 6440949..69b4504 100644 --- a/src/events/voice-state-update.ts +++ b/src/events/voice-state-update.ts @@ -1,4 +1,4 @@ -import {VoiceState} from 'discord.js'; +import {VoiceChannel, VoiceState} from 'discord.js'; import container from '../inversify.config.js'; import {TYPES} from '../types.js'; import PlayerManager from '../managers/player.js'; @@ -10,7 +10,8 @@ export default (oldState: VoiceState, _: VoiceState): void => { const player = playerManager.get(oldState.guild.id); if (player.voiceConnection) { - if (getSizeWithoutBots(player.voiceConnection.channel) === 0) { + const voiceChannel: VoiceChannel = oldState.guild.channels.cache.get(player.voiceConnection.joinConfig.channelId!) as VoiceChannel; + if (!voiceChannel || getSizeWithoutBots(voiceChannel) === 0) { player.disconnect(); } } diff --git a/src/inversify.config.ts b/src/inversify.config.ts index 00a2507..c6809d2 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -2,7 +2,7 @@ import 'reflect-metadata'; import {Container} from 'inversify'; import {TYPES} from './types.js'; import Bot from './bot.js'; -import {Client} from 'discord.js'; +import {Client, Intents} from 'discord.js'; import ConfigProvider from './services/config.js'; // Managers @@ -32,9 +32,16 @@ import CacheProvider from './services/cache.js'; const container = new Container(); +// Intents +const intents = new Intents(); +intents.add(Intents.FLAGS.GUILDS); // To listen for guildCreate event +intents.add(Intents.FLAGS.GUILD_MESSAGES); // To listen for messages (messageCreate event) +intents.add(Intents.FLAGS.DIRECT_MESSAGE_REACTIONS); // To listen for message reactions (messageReactionAdd event) +intents.add(Intents.FLAGS.GUILD_VOICE_STATES); // To listen for voice state changes (voiceStateUpdate event) + // Bot container.bind(TYPES.Bot).to(Bot).inSingletonScope(); -container.bind(TYPES.Client).toConstantValue(new Client()); +container.bind(TYPES.Client).toConstantValue(new Client({intents})); // Managers container.bind(TYPES.Managers.Player).to(PlayerManager).inSingletonScope(); diff --git a/src/services/player.ts b/src/services/player.ts index 742da55..9faef18 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -1,4 +1,4 @@ -import {VoiceConnection, VoiceChannel, StreamDispatcher, Snowflake, Client, TextChannel} from 'discord.js'; +import {VoiceChannel, Snowflake, Client, TextChannel} from 'discord.js'; import {promises as fs, createWriteStream} from 'fs'; import {Readable, PassThrough} from 'stream'; import path from 'path'; @@ -8,6 +8,7 @@ import {WriteStream} from 'fs-capacitor'; import ffmpeg from 'fluent-ffmpeg'; import shuffle from 'array-shuffle'; import errorMsg from '../utils/error-msg.js'; +import {AudioPlayer, AudioPlayerStatus, createAudioPlayer, createAudioResource, joinVoiceChannel, StreamType, VoiceConnection, VoiceConnectionStatus} from '@discordjs/voice'; export interface QueuedPlaylist { title: string; @@ -35,7 +36,7 @@ export default class { private queue: QueuedSong[] = []; private queuePosition = 0; private readonly cacheDir: string; - private dispatcher: StreamDispatcher | null = null; + private audioPlayer: AudioPlayer | null = null; private nowPlaying: QueuedSong | null = null; private playPositionInterval: NodeJS.Timeout | undefined; private lastSongURL = ''; @@ -50,23 +51,26 @@ export default class { } async connect(channel: VoiceChannel): Promise { - const conn = await channel.join(); + const conn = joinVoiceChannel({ + channelId: channel.id, + guildId: channel.guild.id, + adapterCreator: channel.guild.voiceAdapterCreator, + }); this.voiceConnection = conn; } - disconnect(breakConnection = true): void { + disconnect(): void { if (this.voiceConnection) { if (this.status === STATUS.PLAYING) { this.pause(); } - if (breakConnection) { - this.voiceConnection.disconnect(); - } + this.voiceConnection.destroy(); + this.audioPlayer?.stop(); this.voiceConnection = null; - this.dispatcher = null; + this.audioPlayer = null; } } @@ -88,8 +92,11 @@ export default class { } const stream = await this.getStream(currentSong.url, {seek: positionSeconds}); - this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus', bitrate: 'auto'}); - + this.audioPlayer = createAudioPlayer(); + this.voiceConnection.subscribe(this.audioPlayer); + this.audioPlayer.play(createAudioResource(stream, { + inputType: StreamType.WebmOpus, + })); this.attachListeners(); this.startTrackingPosition(positionSeconds); @@ -117,8 +124,8 @@ export default class { // Resume from paused state if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) { - if (this.dispatcher) { - this.dispatcher.resume(); + if (this.audioPlayer) { + this.audioPlayer.unpause(); this.status = STATUS.PLAYING; this.startTrackingPosition(); return; @@ -132,7 +139,11 @@ export default class { try { const stream = await this.getStream(currentSong.url); - this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); + this.audioPlayer = createAudioPlayer(); + this.voiceConnection.subscribe(this.audioPlayer); + this.audioPlayer.play(createAudioResource(stream, { + inputType: StreamType.WebmOpus, + })); this.attachListeners(); @@ -170,8 +181,8 @@ export default class { this.status = STATUS.PAUSED; - if (this.dispatcher) { - this.dispatcher.pause(); + if (this.audioPlayer) { + this.audioPlayer.pause(); } this.stopTrackingPosition(); @@ -441,26 +452,22 @@ export default class { return; } - if (this.voiceConnection.listeners('disconnect').length === 0) { - this.voiceConnection.on('disconnect', this.onVoiceConnectionDisconnect.bind(this)); - } + this.voiceConnection.on(VoiceConnectionStatus.Disconnected, this.onVoiceConnectionDisconnect.bind(this)); - if (!this.dispatcher) { + if (!this.audioPlayer) { return; } - if (this.dispatcher.listeners('speaking').length === 0) { - this.dispatcher.on('speaking', this.onVoiceConnectionSpeaking.bind(this)); - } + this.audioPlayer.on('stateChange', this.onAudioPlayerStateChange.bind(this)); } private onVoiceConnectionDisconnect(): void { - this.disconnect(false); + this.disconnect(); } - private async onVoiceConnectionSpeaking(isSpeaking: boolean): Promise { + private async onAudioPlayerStateChange(_oldState: {status: AudioPlayerStatus}, newState: {status: AudioPlayerStatus}): Promise { // Automatically advance queued song at end - if (!isSpeaking && this.status === STATUS.PLAYING) { + if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) { await this.forward(1); } } diff --git a/src/utils/channels.ts b/src/utils/channels.ts index 4c5b6b0..2d074b4 100644 --- a/src/utils/channels.ts +++ b/src/utils/channels.ts @@ -3,8 +3,8 @@ import {Guild, VoiceChannel, User, GuildMember} from 'discord.js'; export const isUserInVoice = (guild: Guild, user: User): boolean => { let inVoice = false; - guild.channels.cache.filter(channel => channel.type === 'voice').forEach(channel => { - if (channel.members.array().find(member => member.id === user.id)) { + guild.channels.cache.filter(channel => channel.type === 'GUILD_VOICE').forEach(channel => { + if ((channel as VoiceChannel).members.find(member => member.id === user.id)) { inVoice = true; } }); @@ -12,7 +12,7 @@ export const isUserInVoice = (guild: Guild, user: User): boolean => { return inVoice; }; -export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.members.array().reduce((s, member) => { +export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.members.reduce((s, member) => { if (!member.user.bot) { s++; } @@ -22,7 +22,7 @@ export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.mem export const getMemberVoiceChannel = (member?: GuildMember): [VoiceChannel, number] | null => { const channel = member?.voice?.channel; - if (channel && channel.type === 'voice') { + if (channel && channel.type === 'GUILD_VOICE') { return [ channel, getSizeWithoutBots(channel), @@ -41,7 +41,7 @@ export const getMostPopularVoiceChannel = (guild: Guild): [VoiceChannel, number] const voiceChannels: PopularResult[] = []; for (const [_, channel] of guild.channels.cache) { - if (channel.type === 'voice') { + if (channel.type === 'GUILD_VOICE') { const size = getSizeWithoutBots(channel as VoiceChannel); voiceChannels.push({ -- cgit v1.2.3 From 5cc74a3d51adf75db44d7102734ba5f95429be24 Mon Sep 17 00:00:00 2001 From: Hellyson Rodrigo Parteka Date: Wed, 17 Nov 2021 01:48:48 -0300 Subject: fix: add missing `DIRECT_MESSAGES` intent --- src/inversify.config.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/inversify.config.ts b/src/inversify.config.ts index c6809d2..b45c588 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -37,6 +37,7 @@ const intents = new Intents(); intents.add(Intents.FLAGS.GUILDS); // To listen for guildCreate event intents.add(Intents.FLAGS.GUILD_MESSAGES); // To listen for messages (messageCreate event) intents.add(Intents.FLAGS.DIRECT_MESSAGE_REACTIONS); // To listen for message reactions (messageReactionAdd event) +intents.add(Intents.FLAGS.DIRECT_MESSAGES); // To receive the prefix message intents.add(Intents.FLAGS.GUILD_VOICE_STATES); // To listen for voice state changes (voiceStateUpdate event) // Bot -- cgit v1.2.3 From 46701a8aab0247943defc3b09c719a61cb5df2b0 Mon Sep 17 00:00:00 2001 From: Hellyson Rodrigo Parteka Date: Wed, 17 Nov 2021 20:17:08 -0300 Subject: fix: change `player.add(...)` behavior --- src/services/player.ts | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/services/player.ts b/src/services/player.ts index 9faef18..0446fa8 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -241,25 +241,12 @@ export default class { } add(song: QueuedSong, {immediate = false} = {}): void { - if (song.playlist) { + if (song.playlist || !immediate) { // Add to end of queue this.queue.push(song); } else { - // Not from playlist, add immediately - let insertAt = this.queuePosition + 1; - - if (!immediate) { - // Loop until playlist song - this.queue.some(song => { - if (song.playlist) { - return true; - } - - insertAt++; - return false; - }); - } - + // Add as the next song to be played + const insertAt = this.queuePosition + 1; this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)]; } } -- cgit v1.2.3 From 5a74115bebb42df2eac3a277478f69ef706c4e83 Mon Sep 17 00:00:00 2001 From: DrunkenToast Date: Sat, 20 Nov 2021 21:10:39 +0100 Subject: feat: remove from queue + fix typo queuecommand --- src/commands/play.ts | 1 + src/commands/remove.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ src/inversify.config.ts | 6 ++-- src/services/player.ts | 4 +++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/commands/remove.ts (limited to 'src') diff --git a/src/commands/play.ts b/src/commands/play.ts index a907a40..3bf19a4 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 { const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!); diff --git a/src/commands/remove.ts b/src/commands/remove.ts new file mode 100644 index 0000000..8f2931f --- /dev/null +++ b/src/commands/remove.ts @@ -0,0 +1,77 @@ +import {Message, TextChannel} from 'discord.js'; +import {inject, injectable} from 'inversify'; +import {TYPES} from '../types.js'; +import PlayerManager from '../managers/player.js'; +import Command from '.'; +import LoadingMessage from '../utils/loading-message.js'; +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 first song in the queue (not the one thats playing, just skip it dummy)'], + ['rm 6-9', 'removes the every song in range [6-9] 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 { + const player = this.playerManager.get(msg.guild!.id); + + const res = new LoadingMessage(msg.channel as TextChannel); + await res.start(); + + if (args.length === 0) { + await res.stop('atleast give me a clue for which song you want to remove'); + 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 res.stop(errorMsg('incorrect format, just an index or start-end 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 res.stop(errorMsg('you start counting with 1')); + return; + } + + if (range[0] < range[1]) { + player.removeFromQueue(range[0], range[0] - range[1]); + } else { + await res.stop(errorMsg('range is backwards, just like you')); + return; + } + + console.log(range); + } else { // 3rd group exists -> just one song + const index = parseInt(match[3], 10); + + if (index < 1) { + await res.stop(errorMsg('it\'s got be bigger than 0, chief')); + return; + } + + if (index > player.queueSize()) { + await res.stop(errorMsg('queue isn\'t THAT big')); + return; + } + + player.removeFromQueue(index, 1); + } + + await res.stop('to the trash it goes :wastebasket:'); + } +} diff --git a/src/inversify.config.ts b/src/inversify.config.ts index b45c588..ae17791 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'; @@ -60,7 +61,8 @@ container.bind(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 0446fa8..9568ce0 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -271,6 +271,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)]; } -- cgit v1.2.3 From fe233cb98ce460dccb5d799de00671b6a57f2202 Mon Sep 17 00:00:00 2001 From: DrunkenToast Date: Sat, 20 Nov 2021 21:19:34 +0100 Subject: fix: range and q size check --- src/commands/remove.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/commands/remove.ts b/src/commands/remove.ts index 8f2931f..e098484 100644 --- a/src/commands/remove.ts +++ b/src/commands/remove.ts @@ -48,8 +48,13 @@ export default class implements Command { return; } + if (range[1] > player.queueSize()) { + await res.stop(errorMsg('queue isn\'t THAT big')); + return; + } + if (range[0] < range[1]) { - player.removeFromQueue(range[0], range[0] - range[1]); + player.removeFromQueue(range[0], range[1] - range[0] + 1); } else { await res.stop(errorMsg('range is backwards, just like you')); return; -- cgit v1.2.3 From 7538a2ebb8bc111bd974eab7d037e8a4a558a10a Mon Sep 17 00:00:00 2001 From: DrunkenToast Date: Sat, 20 Nov 2021 21:34:56 +0100 Subject: fix: loading message isn't required --- src/commands/remove.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/commands/remove.ts b/src/commands/remove.ts index e098484..bdca944 100644 --- a/src/commands/remove.ts +++ b/src/commands/remove.ts @@ -1,9 +1,8 @@ -import {Message, TextChannel} from 'discord.js'; +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 LoadingMessage from '../utils/loading-message.js'; import errorMsg from '../utils/error-msg.js'; @injectable() @@ -24,11 +23,8 @@ export default class implements Command { public async execute(msg: Message, args: string []): Promise { const player = this.playerManager.get(msg.guild!.id); - const res = new LoadingMessage(msg.channel as TextChannel); - await res.start(); - if (args.length === 0) { - await res.stop('atleast give me a clue for which song you want to remove'); + await msg.channel.send('atleast give me a clue for which song you want to remove'); return; } @@ -36,7 +32,7 @@ export default class implements Command { const match = reg.exec(args[0]); if (match === null) { - await res.stop(errorMsg('incorrect format, just an index or start-end format')); + await msg.channel.send(errorMsg('incorrect format, just an index or start-end format')); return; } @@ -44,19 +40,19 @@ export default class implements Command { const range = [parseInt(match[1], 10), parseInt(match[2], 10)]; if (range[0] < 1) { - await res.stop(errorMsg('you start counting with 1')); + await msg.channel.send(errorMsg('you start counting with 1')); return; } if (range[1] > player.queueSize()) { - await res.stop(errorMsg('queue isn\'t THAT big')); + await msg.channel.send(errorMsg('queue isn\'t THAT big')); return; } if (range[0] < range[1]) { player.removeFromQueue(range[0], range[1] - range[0] + 1); } else { - await res.stop(errorMsg('range is backwards, just like you')); + await msg.channel.send(errorMsg('range is backwards, just like you')); return; } @@ -65,18 +61,18 @@ export default class implements Command { const index = parseInt(match[3], 10); if (index < 1) { - await res.stop(errorMsg('it\'s got be bigger than 0, chief')); + await msg.channel.send(errorMsg('it\'s got be bigger than 0, chief')); return; } if (index > player.queueSize()) { - await res.stop(errorMsg('queue isn\'t THAT big')); + await msg.channel.send(errorMsg('queue isn\'t THAT big')); return; } player.removeFromQueue(index, 1); } - await res.stop('to the trash it goes :wastebasket:'); + await msg.channel.send('to the trash it goes :wastebasket:'); } } -- cgit v1.2.3 From 490b082587e6ff7428ea119b0071d8d50d92bd97 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sat, 20 Nov 2021 18:58:40 -0500 Subject: Update strings --- src/commands/play.ts | 1 - src/commands/remove.ts | 22 ++++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/commands/play.ts b/src/commands/play.ts index 3bf19a4..0582ebe 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -131,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 index bdca944..9c40a71 100644 --- a/src/commands/remove.ts +++ b/src/commands/remove.ts @@ -10,8 +10,8 @@ export default class implements Command { public name = 'remove'; public aliases = ['rm']; public examples = [ - ['remove 1', 'removes the first song in the queue (not the one thats playing, just skip it dummy)'], - ['rm 6-9', 'removes the every song in range [6-9] from the queue'], + ['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; @@ -24,7 +24,7 @@ export default class implements Command { const player = this.playerManager.get(msg.guild!.id); if (args.length === 0) { - await msg.channel.send('atleast give me a clue for which song you want to remove'); + await msg.channel.send(errorMsg('missing song position or range')); return; } @@ -32,7 +32,7 @@ export default class implements Command { const match = reg.exec(args[0]); if (match === null) { - await msg.channel.send(errorMsg('incorrect format, just an index or start-end format')); + await msg.channel.send(errorMsg('incorrect format')); return; } @@ -40,39 +40,37 @@ export default class implements Command { const range = [parseInt(match[1], 10), parseInt(match[2], 10)]; if (range[0] < 1) { - await msg.channel.send(errorMsg('you start counting with 1')); + await msg.channel.send(errorMsg('position must be greater than 0')); return; } if (range[1] > player.queueSize()) { - await msg.channel.send(errorMsg('queue isn\'t THAT big')); + 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, just like you')); + await msg.channel.send(errorMsg('range is backwards')); return; } - - console.log(range); } else { // 3rd group exists -> just one song const index = parseInt(match[3], 10); if (index < 1) { - await msg.channel.send(errorMsg('it\'s got be bigger than 0, chief')); + await msg.channel.send(errorMsg('position must be greater than 0')); return; } if (index > player.queueSize()) { - await msg.channel.send(errorMsg('queue isn\'t THAT big')); + await msg.channel.send(errorMsg('position is outside of the queue\'s range')); return; } player.removeFromQueue(index, 1); } - await msg.channel.send('to the trash it goes :wastebasket:'); + await msg.channel.send(':wastebasket: removed'); } } -- cgit v1.2.3