From bd6fe1d4d730b09064833493bafa1f3d786d2f50 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 20 Mar 2024 16:42:17 -0400 Subject: [PATCH] Fix all audio commands --- commands/games-mp/mafia.js | 4 ++-- commands/games-sp/guess-song.js | 13 ++++--------- commands/games-sp/hearing-test.js | 3 ++- commands/games-sp/jeopardy.js | 11 ++++------- commands/games-sp/whos-that-pokemon-cry.js | 10 +++------- commands/pokedex/pokedex-cry.js | 9 +++------ commands/pokedex/pokedex.js | 7 ++----- commands/voice/airhorn.js | 9 +++------ commands/voice/animalese.js | 9 +++------ commands/voice/dec-talk.js | 9 +++------ commands/voice/mindfulness.js | 9 +++------ commands/voice/play.js | 9 +++------ commands/voice/soundboard.js | 9 +++------ commands/voice/tts.js | 9 +++------ commands/voice/vocodes.js | 9 +++------ structures/mafia/Game.js | 14 ++++---------- 16 files changed, 48 insertions(+), 95 deletions(-) diff --git a/commands/games-mp/mafia.js b/commands/games-mp/mafia.js index 19023715..0e1d46da 100644 --- a/commands/games-mp/mafia.js +++ b/commands/games-mp/mafia.js @@ -17,12 +17,12 @@ module.exports = class MafiaCommand extends Command { async run(msg) { const current = this.client.games.get(msg.channel.id); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); for (const member of connection.channel.members.values()) await msg.guild.members.fetch(member.id); if (connection.channel.members.size > 16) { return msg.reply('Please do not have more than 15 users in this voice channel.'); diff --git a/commands/games-sp/guess-song.js b/commands/games-sp/guess-song.js index 352b524e..a77d900c 100644 --- a/commands/games-sp/guess-song.js +++ b/commands/games-sp/guess-song.js @@ -52,24 +52,21 @@ module.exports = class GuessSongCommand extends Command { } async run(msg, { chart }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } const current = this.client.games.get(msg.channel.id); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); this.client.games.set(msg.channel.id, { name: this.name }); let songID; try { if (!this.token) await this.fetchToken(); const data = await this.fetchRandomSong(chart); const { body: previewBody } = await request.get(data.preview); - const dispatcher = connection.play(Readable.from([previewBody])); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(Readable.from([previewBody])); await reactIfAble(msg, this.client.user, 'πŸ”‰'); await msg.reply('**You have 30 seconds, what song is this?**'); const msgs = await msg.channel.awaitMessages({ @@ -78,8 +75,7 @@ module.exports = class GuessSongCommand extends Command { time: 30000 }); this.client.games.delete(msg.channel.id); - dispatcher.end(); - this.client.dispatchers.delete(msg.guild.id); + connection.stop(); if (!msgs.size) return msg.reply(`Time! It's **${data.name}** by **${data.artist}**!`); const guess = msgs.first().content.toLowerCase(); if (!guess.includes(data.name.toLowerCase()) && !guess.includes(data.shortName.toLowerCase())) { @@ -87,7 +83,6 @@ module.exports = class GuessSongCommand extends Command { } return msg.reply(`Nice! It's **${data.name}** by **${data.artist}**!`); } catch (err) { - this.client.dispatchers.delete(msg.guild.id); this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Song ID: \`${songID}\`.`); } diff --git a/commands/games-sp/hearing-test.js b/commands/games-sp/hearing-test.js index bec791c7..2ab4adda 100644 --- a/commands/games-sp/hearing-test.js +++ b/commands/games-sp/hearing-test.js @@ -30,11 +30,12 @@ module.exports = class HearingTestCommand extends Command { } async run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); try { let age; let range; diff --git a/commands/games-sp/jeopardy.js b/commands/games-sp/jeopardy.js index 1a3ecedb..ea5ceda6 100644 --- a/commands/games-sp/jeopardy.js +++ b/commands/games-sp/jeopardy.js @@ -44,14 +44,11 @@ module.exports = class JeopardyCommand extends Command { this.client.games.set(msg.channel.id, { name: this.name }); const question = await this.fetchQuestion(); const clueCard = await this.generateClueCard(question.question.replace(/<\/?i>/gi, '')); - const connection = msg.guild ? this.client.voice.connections.get(msg.guild.id) : null; + const connection = msg.guild ? this.client.dispatchers.get(msg.guild.id) : null; let playing = false; - if (msg.guild && connection && !this.client.dispatchers.has(msg.guild.id)) { + if (msg.guild && connection && connection.canPlay) { playing = true; - const dispatcher = connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'jeopardy.mp3')); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'jeopardy.mp3')); await reactIfAble(msg, this.client.user, 'πŸ”‰'); } const category = question.category ? question.category.title.toUpperCase() : ''; @@ -63,7 +60,7 @@ module.exports = class JeopardyCommand extends Command { max: 1, time: 30000 }); - if (playing) connection.dispatcher.end(); + if (playing) connection.stop(); const answer = question.answer.replace(/<\/?i>/gi, '*'); this.client.games.delete(msg.channel.id); if (!msgs.size) return msg.reply(`Time's up, the answer was **${answer}**.`); diff --git a/commands/games-sp/whos-that-pokemon-cry.js b/commands/games-sp/whos-that-pokemon-cry.js index 82197d16..a9f85990 100644 --- a/commands/games-sp/whos-that-pokemon-cry.js +++ b/commands/games-sp/whos-that-pokemon-cry.js @@ -78,23 +78,20 @@ module.exports = class WhosThatPokemonCryCommand extends Command { } async run(msg, { pokemon }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } const current = this.client.games.get(msg.channel.id); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); this.client.games.set(msg.channel.id, { name: this.name }); try { const data = await this.client.pokemon.fetch(pokemon.toString()); const names = data.names.map(name => name.name.toLowerCase()); const attachment = await this.client.registry.commands.get('whos-that-pokemon').createImage(data, false); - const dispatcher = connection.play(data.cry); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(data.cry); await reactIfAble(msg, this.client.user, 'πŸ”‰'); await msg.reply('**You have 15 seconds, who\'s that PokΓ©mon?**'); const msgs = await msg.channel.awaitMessages({ @@ -112,7 +109,6 @@ module.exports = class WhosThatPokemonCryCommand extends Command { } return msg.reply(`Nice! It's **${data.name}**!`, { files: [attachment] }); } catch (err) { - this.client.dispatchers.delete(msg.guild.id); this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } diff --git a/commands/pokedex/pokedex-cry.js b/commands/pokedex/pokedex-cry.js index 39780f6f..ffe850a5 100644 --- a/commands/pokedex/pokedex-cry.js +++ b/commands/pokedex/pokedex-cry.js @@ -56,17 +56,14 @@ module.exports = class PokedexCryCommand extends Command { } async run(msg, { pokemon }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); try { - const dispatcher = connection.play(pokemon.cry); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(pokemon.cry); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } catch (err) { diff --git a/commands/pokedex/pokedex.js b/commands/pokedex/pokedex.js index 1f74b716..377d564d 100644 --- a/commands/pokedex/pokedex.js +++ b/commands/pokedex/pokedex.js @@ -113,12 +113,9 @@ module.exports = class PokedexCommand extends Command { ? 'Genderless' : `♂️ ${pokemon.genderRate.male}% ♀️ ${pokemon.genderRate.female}%`); if (msg.guild && pokemon.cry && !this.client.dispatchers.has(msg.guild.id)) { - const connection = msg.guild ? this.client.voice.connections.get(msg.guild.id) : null; + const connection = msg.guild ? this.client.dispatchers.get(msg.guild.id) : null; if (connection) { - const dispatcher = connection.play(pokemon.cry); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(pokemon.cry); await reactIfAble(msg, this.client.user, 'πŸ”‰'); } } diff --git a/commands/voice/airhorn.js b/commands/voice/airhorn.js index 310792d3..3b754ec4 100644 --- a/commands/voice/airhorn.js +++ b/commands/voice/airhorn.js @@ -29,17 +29,14 @@ module.exports = class AirhornCommand extends Command { } async run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); const airhorn = sounds[Math.floor(Math.random() * sounds.length)]; - const dispatcher = connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'airhorn', airhorn)); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'airhorn', airhorn)); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } diff --git a/commands/voice/animalese.js b/commands/voice/animalese.js index c248845d..8727d731 100644 --- a/commands/voice/animalese.js +++ b/commands/voice/animalese.js @@ -64,17 +64,14 @@ module.exports = class AnimaleseCommand extends Command { } async run(msg, { pitch, text }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, 'πŸ’¬'); - const dispatcher = connection.play(Readable.from([this.animalese(text, pitch)])); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(Readable.from([this.animalese(text, pitch)])); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } diff --git a/commands/voice/dec-talk.js b/commands/voice/dec-talk.js index 35c7e57e..c48274f7 100644 --- a/commands/voice/dec-talk.js +++ b/commands/voice/dec-talk.js @@ -47,19 +47,16 @@ module.exports = class DECTalkCommand extends Command { } async run(msg, { text }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); try { await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, 'πŸ’¬'); const body = await this.tts(msg.guild.id, text); - const dispatcher = connection.play(Readable.from([body])); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(Readable.from([body])); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } catch (err) { diff --git a/commands/voice/mindfulness.js b/commands/voice/mindfulness.js index f39206df..0ec455a9 100644 --- a/commands/voice/mindfulness.js +++ b/commands/voice/mindfulness.js @@ -29,20 +29,17 @@ module.exports = class MindfulnessCommand extends Command { } async run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); try { await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, 'πŸ’¬'); const flow = await this.fetchFlow(); const { body } = await request.get(flow.mp3); - const dispatcher = connection.play(Readable.from([body])); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(Readable.from([body])); await reactIfAble(msg, this.client.user, 'πŸ”‰'); for (const item of flow.data) { if (item.type !== 'quote') continue; diff --git a/commands/voice/play.js b/commands/voice/play.js index 398d258d..3fa34ebb 100644 --- a/commands/voice/play.js +++ b/commands/voice/play.js @@ -38,12 +38,12 @@ module.exports = class PlayCommand extends Command { } async run(msg, { query }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); const result = await this.searchForVideo(query, msg.channel.nsfw || false); if (!result) return msg.reply('Could not find any results for your query.'); const data = await ytdl.getInfo(result); @@ -55,10 +55,7 @@ module.exports = class PlayCommand extends Command { }); const verification = await verify(msg.channel, msg.author); if (!verification) return msg.reply('Aborting playback.'); - const dispatcher = connection.play(ytdl(result, { filter: 'audioonly' })); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(ytdl(result, { filter: 'audioonly' })); return msg.reply(`πŸ”‰ Now playing **${shorten(data.videoDetails.title, 70)}**!`); } diff --git a/commands/voice/soundboard.js b/commands/voice/soundboard.js index bf46e365..29060542 100644 --- a/commands/voice/soundboard.js +++ b/commands/voice/soundboard.js @@ -128,16 +128,13 @@ module.exports = class SoundboardCommand extends Command { } async run(msg, { sound }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); - const dispatcher = connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', ...sound)); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); + connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', ...sound)); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } diff --git a/commands/voice/tts.js b/commands/voice/tts.js index 2b0833be..09b847f0 100644 --- a/commands/voice/tts.js +++ b/commands/voice/tts.js @@ -45,12 +45,12 @@ module.exports = class TtsCommand extends Command { } async run(msg, { accent, text }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); try { await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, 'πŸ’¬'); const { body } = await request @@ -66,10 +66,7 @@ module.exports = class TtsCommand extends Command { prev: 'input', ttsspeed: 1 }); - const dispatcher = connection.play(Readable.from([body])); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(Readable.from([body])); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } catch (err) { diff --git a/commands/voice/vocodes.js b/commands/voice/vocodes.js index a6ead19f..3596d2d4 100644 --- a/commands/voice/vocodes.js +++ b/commands/voice/vocodes.js @@ -46,12 +46,12 @@ module.exports = class VocodesCommand extends Command { } async run(msg, { voice, text }) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = this.client.dispatchers.get(msg.guild.id); if (!connection) { const usage = this.client.registry.commands.get('join').usage(); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } - if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.'); + if (!connection.canPlay) return msg.reply('I am already playing audio in this server.'); try { await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, 'πŸ’¬'); const { body } = await request @@ -60,10 +60,7 @@ module.exports = class VocodesCommand extends Command { speaker: voice, text }); - const dispatcher = connection.play(Readable.from([Buffer.from(body.audio_base64, 'base64')])); - this.client.dispatchers.set(msg.guild.id, dispatcher); - dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id)); - dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id)); + connection.play(Readable.from([Buffer.from(body.audio_base64, 'base64')])); await reactIfAble(msg, this.client.user, 'πŸ”‰'); return null; } catch (err) { diff --git a/structures/mafia/Game.js b/structures/mafia/Game.js index b780c27f..376e8051 100644 --- a/structures/mafia/Game.js +++ b/structures/mafia/Game.js @@ -3,6 +3,7 @@ const path = require('path'); const { stripIndents } = require('common-tags'); const Player = require('./Player'); const { shuffle, reactIfAble } = require('../../util/Util'); +const { AudioPlayerStatus } = require('@discordjs/voice'); const { SUCCESS_EMOJI_ID } = process.env; module.exports = class Game { @@ -41,19 +42,12 @@ module.exports = class Game { } playAudio(id) { - const dispatcher = this.connection.play( + this.connection.play( path.join(__dirname, '..', '..', 'assets', 'sounds', 'mafia', `${id}.mp3`), { volume: 2 } ); - this.client.dispatchers.set(this.channel.guild.id, dispatcher); return new Promise((res, rej) => { - this.dispatcher.once('finish', () => { - this.client.dispatchers.delete(this.channel.guild.id); - return res(true); - }); - this.dispatcher.once('error', err => { - this.client.dispatchers.delete(this.channel.guild.id); - return rej(err); - }); + this.dispatcher.once(AudioPlayerStatus.Idle, () => res(true)); + this.dispatcher.once('error', err => rej(err)); }); }