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) {
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.');
+4 -9
View File
@@ -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}\`.`);
}
+2 -1
View File
@@ -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;
+4 -7
View File
@@ -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}**.`);
+3 -7
View File
@@ -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!`);
}
+3 -6
View File
@@ -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) {
+2 -5
View File
@@ -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, '🔉');
}
}
+3 -6
View File
@@ -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;
}
+3 -6
View File
@@ -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;
}
+3 -6
View File
@@ -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) {
+3 -6
View File
@@ -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;
+3 -6
View File
@@ -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)}**!`);
}
+3 -6
View File
@@ -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;
}
+3 -6
View File
@@ -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) {
+3 -6
View File
@@ -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) {
+4 -10
View File
@@ -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));
});
}