Fix all audio commands

This commit is contained in:
Dragon Fire
2024-03-20 16:42:17 -04:00
parent e09d761463
commit bd6fe1d4d7
16 changed files with 48 additions and 95 deletions
+2 -2
View File
@@ -17,12 +17,12 @@ module.exports = class MafiaCommand extends Command {
async run(msg) { async run(msg) {
const current = this.client.games.get(msg.channel.id); 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 (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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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); for (const member of connection.channel.members.values()) await msg.guild.members.fetch(member.id);
if (connection.channel.members.size > 16) { if (connection.channel.members.size > 16) {
return msg.reply('Please do not have more than 15 users in this voice channel.'); return msg.reply('Please do not have more than 15 users in this voice channel.');
+4 -9
View File
@@ -52,24 +52,21 @@ module.exports = class GuessSongCommand extends Command {
} }
async run(msg, { chart }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`);
} }
const current = this.client.games.get(msg.channel.id); 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 (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 }); this.client.games.set(msg.channel.id, { name: this.name });
let songID; let songID;
try { try {
if (!this.token) await this.fetchToken(); if (!this.token) await this.fetchToken();
const data = await this.fetchRandomSong(chart); const data = await this.fetchRandomSong(chart);
const { body: previewBody } = await request.get(data.preview); const { body: previewBody } = await request.get(data.preview);
const dispatcher = connection.play(Readable.from([previewBody])); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
await msg.reply('**You have 30 seconds, what song is this?**'); await msg.reply('**You have 30 seconds, what song is this?**');
const msgs = await msg.channel.awaitMessages({ const msgs = await msg.channel.awaitMessages({
@@ -78,8 +75,7 @@ module.exports = class GuessSongCommand extends Command {
time: 30000 time: 30000
}); });
this.client.games.delete(msg.channel.id); this.client.games.delete(msg.channel.id);
dispatcher.end(); connection.stop();
this.client.dispatchers.delete(msg.guild.id);
if (!msgs.size) return msg.reply(`Time! It's **${data.name}** by **${data.artist}**!`); if (!msgs.size) return msg.reply(`Time! It's **${data.name}** by **${data.artist}**!`);
const guess = msgs.first().content.toLowerCase(); const guess = msgs.first().content.toLowerCase();
if (!guess.includes(data.name.toLowerCase()) && !guess.includes(data.shortName.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}**!`); return msg.reply(`Nice! It's **${data.name}** by **${data.artist}**!`);
} catch (err) { } catch (err) {
this.client.dispatchers.delete(msg.guild.id);
this.client.games.delete(msg.channel.id); this.client.games.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Song ID: \`${songID}\`.`); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Song ID: \`${songID}\`.`);
} }
+2 -1
View File
@@ -30,11 +30,12 @@ module.exports = class HearingTestCommand extends Command {
} }
async run(msg) { async run(msg) {
const connection = this.client.voice.connections.get(msg.guild.id); const connection = this.client.dispatchers.get(msg.guild.id);
if (!connection) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 { try {
let age; let age;
let range; let range;
+4 -7
View File
@@ -44,14 +44,11 @@ module.exports = class JeopardyCommand extends Command {
this.client.games.set(msg.channel.id, { name: this.name }); this.client.games.set(msg.channel.id, { name: this.name });
const question = await this.fetchQuestion(); const question = await this.fetchQuestion();
const clueCard = await this.generateClueCard(question.question.replace(/<\/?i>/gi, '')); 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; let playing = false;
if (msg.guild && connection && !this.client.dispatchers.has(msg.guild.id)) { if (msg.guild && connection && connection.canPlay) {
playing = true; playing = true;
const dispatcher = connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'jeopardy.mp3')); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
} }
const category = question.category ? question.category.title.toUpperCase() : ''; const category = question.category ? question.category.title.toUpperCase() : '';
@@ -63,7 +60,7 @@ module.exports = class JeopardyCommand extends Command {
max: 1, max: 1,
time: 30000 time: 30000
}); });
if (playing) connection.dispatcher.end(); if (playing) connection.stop();
const answer = question.answer.replace(/<\/?i>/gi, '*'); const answer = question.answer.replace(/<\/?i>/gi, '*');
this.client.games.delete(msg.channel.id); this.client.games.delete(msg.channel.id);
if (!msgs.size) return msg.reply(`Time's up, the answer was **${answer}**.`); if (!msgs.size) return msg.reply(`Time's up, the answer was **${answer}**.`);
+3 -7
View File
@@ -78,23 +78,20 @@ module.exports = class WhosThatPokemonCryCommand extends Command {
} }
async run(msg, { pokemon }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`);
} }
const current = this.client.games.get(msg.channel.id); 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 (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 }); this.client.games.set(msg.channel.id, { name: this.name });
try { try {
const data = await this.client.pokemon.fetch(pokemon.toString()); const data = await this.client.pokemon.fetch(pokemon.toString());
const names = data.names.map(name => name.name.toLowerCase()); const names = data.names.map(name => name.name.toLowerCase());
const attachment = await this.client.registry.commands.get('whos-that-pokemon').createImage(data, false); const attachment = await this.client.registry.commands.get('whos-that-pokemon').createImage(data, false);
const dispatcher = connection.play(data.cry); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
await msg.reply('**You have 15 seconds, who\'s that Pokémon?**'); await msg.reply('**You have 15 seconds, who\'s that Pokémon?**');
const msgs = await msg.channel.awaitMessages({ 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] }); return msg.reply(`Nice! It's **${data.name}**!`, { files: [attachment] });
} catch (err) { } catch (err) {
this.client.dispatchers.delete(msg.guild.id);
this.client.games.delete(msg.channel.id); this.client.games.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
} }
+3 -6
View File
@@ -56,17 +56,14 @@ module.exports = class PokedexCryCommand extends Command {
} }
async run(msg, { pokemon }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 { try {
const dispatcher = connection.play(pokemon.cry); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} catch (err) { } catch (err) {
+2 -5
View File
@@ -113,12 +113,9 @@ module.exports = class PokedexCommand extends Command {
? 'Genderless' ? 'Genderless'
: `♂️ ${pokemon.genderRate.male}% ♀️ ${pokemon.genderRate.female}%`); : `♂️ ${pokemon.genderRate.male}% ♀️ ${pokemon.genderRate.female}%`);
if (msg.guild && pokemon.cry && !this.client.dispatchers.has(msg.guild.id)) { 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) { if (connection) {
const dispatcher = connection.play(pokemon.cry); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
} }
} }
+3 -6
View File
@@ -29,17 +29,14 @@ module.exports = class AirhornCommand extends Command {
} }
async run(msg) { async run(msg) {
const connection = this.client.voice.connections.get(msg.guild.id); const connection = this.client.dispatchers.get(msg.guild.id);
if (!connection) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 airhorn = sounds[Math.floor(Math.random() * sounds.length)];
const dispatcher = connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'airhorn', airhorn)); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} }
+3 -6
View File
@@ -64,17 +64,14 @@ module.exports = class AnimaleseCommand extends Command {
} }
async run(msg, { pitch, text }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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, '💬'); await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬');
const dispatcher = connection.play(Readable.from([this.animalese(text, pitch)])); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} }
+3 -6
View File
@@ -47,19 +47,16 @@ module.exports = class DECTalkCommand extends Command {
} }
async run(msg, { text }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 { try {
await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬');
const body = await this.tts(msg.guild.id, text); const body = await this.tts(msg.guild.id, text);
const dispatcher = connection.play(Readable.from([body])); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} catch (err) { } catch (err) {
+3 -6
View File
@@ -29,20 +29,17 @@ module.exports = class MindfulnessCommand extends Command {
} }
async run(msg) { async run(msg) {
const connection = this.client.voice.connections.get(msg.guild.id); const connection = this.client.dispatchers.get(msg.guild.id);
if (!connection) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 { try {
await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬');
const flow = await this.fetchFlow(); const flow = await this.fetchFlow();
const { body } = await request.get(flow.mp3); const { body } = await request.get(flow.mp3);
const dispatcher = connection.play(Readable.from([body])); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
for (const item of flow.data) { for (const item of flow.data) {
if (item.type !== 'quote') continue; if (item.type !== 'quote') continue;
+3 -6
View File
@@ -38,12 +38,12 @@ module.exports = class PlayCommand extends Command {
} }
async run(msg, { query }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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); const result = await this.searchForVideo(query, msg.channel.nsfw || false);
if (!result) return msg.reply('Could not find any results for your query.'); if (!result) return msg.reply('Could not find any results for your query.');
const data = await ytdl.getInfo(result); const data = await ytdl.getInfo(result);
@@ -55,10 +55,7 @@ module.exports = class PlayCommand extends Command {
}); });
const verification = await verify(msg.channel, msg.author); const verification = await verify(msg.channel, msg.author);
if (!verification) return msg.reply('Aborting playback.'); if (!verification) return msg.reply('Aborting playback.');
const dispatcher = connection.play(ytdl(result, { filter: 'audioonly' })); 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));
return msg.reply(`🔉 Now playing **${shorten(data.videoDetails.title, 70)}**!`); return msg.reply(`🔉 Now playing **${shorten(data.videoDetails.title, 70)}**!`);
} }
+3 -6
View File
@@ -128,16 +128,13 @@ module.exports = class SoundboardCommand extends Command {
} }
async run(msg, { sound }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 dispatcher = connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', ...sound)); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} }
+3 -6
View File
@@ -45,12 +45,12 @@ module.exports = class TtsCommand extends Command {
} }
async run(msg, { accent, text }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 { try {
await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬');
const { body } = await request const { body } = await request
@@ -66,10 +66,7 @@ module.exports = class TtsCommand extends Command {
prev: 'input', prev: 'input',
ttsspeed: 1 ttsspeed: 1
}); });
const dispatcher = connection.play(Readable.from([body])); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} catch (err) { } catch (err) {
+3 -6
View File
@@ -46,12 +46,12 @@ module.exports = class VocodesCommand extends Command {
} }
async run(msg, { voice, text }) { 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) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); 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 { try {
await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬');
const { body } = await request const { body } = await request
@@ -60,10 +60,7 @@ module.exports = class VocodesCommand extends Command {
speaker: voice, speaker: voice,
text text
}); });
const dispatcher = connection.play(Readable.from([Buffer.from(body.audio_base64, 'base64')])); 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));
await reactIfAble(msg, this.client.user, '🔉'); await reactIfAble(msg, this.client.user, '🔉');
return null; return null;
} catch (err) { } catch (err) {
+4 -10
View File
@@ -3,6 +3,7 @@ const path = require('path');
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
const Player = require('./Player'); const Player = require('./Player');
const { shuffle, reactIfAble } = require('../../util/Util'); const { shuffle, reactIfAble } = require('../../util/Util');
const { AudioPlayerStatus } = require('@discordjs/voice');
const { SUCCESS_EMOJI_ID } = process.env; const { SUCCESS_EMOJI_ID } = process.env;
module.exports = class Game { module.exports = class Game {
@@ -41,19 +42,12 @@ module.exports = class Game {
} }
playAudio(id) { playAudio(id) {
const dispatcher = this.connection.play( this.connection.play(
path.join(__dirname, '..', '..', 'assets', 'sounds', 'mafia', `${id}.mp3`), { volume: 2 } path.join(__dirname, '..', '..', 'assets', 'sounds', 'mafia', `${id}.mp3`), { volume: 2 }
); );
this.client.dispatchers.set(this.channel.guild.id, dispatcher);
return new Promise((res, rej) => { return new Promise((res, rej) => {
this.dispatcher.once('finish', () => { this.dispatcher.once(AudioPlayerStatus.Idle, () => res(true));
this.client.dispatchers.delete(this.channel.guild.id); this.dispatcher.once('error', err => rej(err));
return res(true);
});
this.dispatcher.once('error', err => {
this.client.dispatchers.delete(this.channel.guild.id);
return rej(err);
});
}); });
} }