From 02d0353650a24b992b7d3c75bfed02b6672142f7 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 20 Mar 2024 01:14:25 -0400 Subject: [PATCH] Try out new discord voice --- commands/games-sp/whos-that-pokemon.js | 32 +++++++++++++++++++++----- commands/util-voice/join.js | 7 +++++- commands/util-voice/leave.js | 5 ++-- commands/util-voice/pause.js | 3 ++- commands/util-voice/resume.js | 9 +++----- commands/util-voice/stop.js | 5 ++-- 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/commands/games-sp/whos-that-pokemon.js b/commands/games-sp/whos-that-pokemon.js index 0d5b3c1b..d9631a44 100644 --- a/commands/games-sp/whos-that-pokemon.js +++ b/commands/games-sp/whos-that-pokemon.js @@ -1,4 +1,5 @@ const Command = require('../../framework/Command'); +const { getVoiceConnection, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice'); const { createCanvas, loadImage } = require('canvas'); const request = require('node-superfetch'); const { reactIfAble } = require('../../util/Util'); @@ -93,14 +94,23 @@ module.exports = class WhosThatPokemonCommand extends Command { const names = data.names.map(name => name.name.toLowerCase()); const attachment = await this.createImage(data, true); const answerAttachment = await this.createImage(data, false); - const connection = msg.guild ? this.client.voice.connections.get(msg.guild.id) : null; + const connection = msg.guild ? getVoiceConnection(msg.guild.id) : null; if (msg.guild && connection && !this.client.dispatchers.has(msg.guild.id)) { - const dispatcher = connection.play( + const resource = createAudioResource( path.join(__dirname, '..', '..', 'assets', 'sounds', 'whos-that-pokemon.mp3') ); + const dispatcher = createAudioPlayer(); + connection.subscribe(dispatcher); + dispatcher.play(resource); 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)); + dispatcher.once(AudioPlayerStatus.Idle, () => { + this.client.dispatchers.get(msg.guild.id).stop(); + this.client.dispatchers.delete(msg.guild.id); + }); + dispatcher.once('error', () => { + this.client.dispatchers.get(msg.guild.id).stop(); + this.client.dispatchers.delete(msg.guild.id); + }); await reactIfAble(msg, this.client.user, '🔉'); } await msg.reply('**You have 15 seconds, who\'s that Pokémon?**', { files: [attachment] }); @@ -110,8 +120,18 @@ module.exports = class WhosThatPokemonCommand extends Command { time: 15000 }); if (connection && data.cry) { - if (connection.dispatcher) connection.dispatcher.end(); - connection.play(data.cry); + const dispatcher = createAudioPlayer(); + connection.subscribe(dispatcher); + dispatcher.play(resource); + this.client.dispatchers.set(msg.guild.id, dispatcher); + dispatcher.once(AudioPlayerStatus.Idle, () => { + this.client.dispatchers.get(msg.guild.id).stop(); + this.client.dispatchers.delete(msg.guild.id); + }); + dispatcher.once('error', () => { + this.client.dispatchers.get(msg.guild.id).stop(); + this.client.dispatchers.delete(msg.guild.id); + }); } this.client.games.delete(msg.channel.id); if (!msgs.size) return msg.reply(`Time! It's **${data.name}**!`, { files: [answerAttachment] }); diff --git a/commands/util-voice/join.js b/commands/util-voice/join.js index 33d886ab..e07a2022 100644 --- a/commands/util-voice/join.js +++ b/commands/util-voice/join.js @@ -1,4 +1,5 @@ const Command = require('../../framework/Command'); +const { joinVoiceChannel } = require('@discordjs/voice'); module.exports = class JoinCommand extends Command { constructor(client) { @@ -24,7 +25,11 @@ module.exports = class JoinCommand extends Command { if (this.client.voice.connections.has(voiceChannel.guild.id)) { return msg.reply('I am already in a voice channel.'); } - await voiceChannel.join(); + joinVoiceChannel({ + channelId: voiceChannel.id, + guildId: voiceChannel.guild.id, + adapterCreator: voiceChannel.guild.voiceAdapterCreator, + }); return msg.reply(`Joined **${voiceChannel.name}**!`); } }; diff --git a/commands/util-voice/leave.js b/commands/util-voice/leave.js index ea5fdd07..243aedd5 100644 --- a/commands/util-voice/leave.js +++ b/commands/util-voice/leave.js @@ -1,4 +1,5 @@ const Command = require('../../framework/Command'); +const { getVoiceConnection } = require('@discordjs/voice'); module.exports = class LeaveCommand extends Command { constructor(client) { @@ -14,7 +15,7 @@ module.exports = class LeaveCommand extends Command { } run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = getVoiceConnection(msg.guild.id); if (!connection) return msg.reply('I am not in a voice channel.'); if (!connection.channel.members.has(msg.author.id)) { return msg.reply('You must be in the voice channel to remove me from it.'); @@ -26,7 +27,7 @@ module.exports = class LeaveCommand extends Command { const usage = this.client.registry.commands.get('stop').usage(); return msg.reply(`I am currently playing audio in this server. Please use ${usage} first.`); } - connection.channel.leave(); + connection.destroy(); return msg.reply(`Left **${connection.channel.name}**...`); } }; diff --git a/commands/util-voice/pause.js b/commands/util-voice/pause.js index a30c506b..1dbeb9c7 100644 --- a/commands/util-voice/pause.js +++ b/commands/util-voice/pause.js @@ -1,4 +1,5 @@ const Command = require('../../framework/Command'); +const { getVoiceConnection } = require('@discordjs/voice'); module.exports = class PauseCommand extends Command { constructor(client) { @@ -14,7 +15,7 @@ module.exports = class PauseCommand extends Command { } run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = getVoiceConnection(msg.guild.id); if (!connection) return msg.reply('I am not in a voice channel.'); if (!msg.channel.permissionsFor(msg.author).has('MOVE_MEMBERS') && connection.channel.members.size > 2) { return msg.reply('You need the "Move members" permission to stop playing audio.'); diff --git a/commands/util-voice/resume.js b/commands/util-voice/resume.js index 91867ef6..cfa83d93 100644 --- a/commands/util-voice/resume.js +++ b/commands/util-voice/resume.js @@ -1,4 +1,5 @@ const Command = require('../../framework/Command'); +const { getVoiceConnection } = require('@discordjs/voice'); module.exports = class ResumeCommand extends Command { constructor(client) { @@ -14,7 +15,7 @@ module.exports = class ResumeCommand extends Command { } run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = getVoiceConnection(msg.guild.id); if (!connection) return msg.reply('I am not in a voice channel.'); if (!msg.channel.permissionsFor(msg.author).has('MOVE_MEMBERS') && connection.channel.members.size > 2) { return msg.reply('You need the "Move members" permission to stop playing audio.'); @@ -22,11 +23,7 @@ module.exports = class ResumeCommand extends Command { if (!this.client.dispatchers.has(msg.guild.id)) { return msg.reply(`I am not currently playing audio in this server.`); } - // Temporary workaround: https://github.com/discordjs/discord.js/issues/5300 - this.client.dispatchers.get(msg.guild.id).pause(); - this.client.dispatchers.get(msg.guild.id).resume(); - this.client.dispatchers.get(msg.guild.id).pause(); - this.client.dispatchers.get(msg.guild.id).resume(); + this.client.dispatchers.get(msg.guild.id).unpause(); return msg.reply('Resumed playing.'); } }; diff --git a/commands/util-voice/stop.js b/commands/util-voice/stop.js index a7a39bef..a2590a46 100644 --- a/commands/util-voice/stop.js +++ b/commands/util-voice/stop.js @@ -1,4 +1,5 @@ const Command = require('../../framework/Command'); +const { getVoiceConnection } = require('@discordjs/voice'); module.exports = class StopCommand extends Command { constructor(client) { @@ -14,7 +15,7 @@ module.exports = class StopCommand extends Command { } run(msg) { - const connection = this.client.voice.connections.get(msg.guild.id); + const connection = getVoiceConnection(msg.guild.id); if (!connection) return msg.reply('I am not in a voice channel.'); if (!msg.channel.permissionsFor(msg.author).has('MOVE_MEMBERS') && connection.channel.members.size > 2) { return msg.reply('You need the "Move members" permission to stop playing audio.'); @@ -22,7 +23,7 @@ module.exports = class StopCommand extends Command { if (!this.client.dispatchers.has(msg.guild.id)) { return msg.reply(`I am not currently playing audio in this server.`); } - this.client.dispatchers.get(msg.guild.id).end(); + this.client.dispatchers.get(msg.guild.id).stop(); return msg.reply('Stopped playing.'); } };