Try out new discord voice

This commit is contained in:
Dragon Fire
2024-03-20 01:14:25 -04:00
parent 2074afc896
commit 02d0353650
6 changed files with 43 additions and 18 deletions
+26 -6
View File
@@ -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] });
+6 -1
View File
@@ -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}**!`);
}
};
+3 -2
View File
@@ -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}**...`);
}
};
+2 -1
View File
@@ -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.');
+3 -6
View File
@@ -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.');
}
};
+3 -2
View File
@@ -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.');
}
};