Rewrite DECTalk Handling

This commit is contained in:
Daniel Odendahl Jr
2017-10-02 13:35:18 +00:00
parent 622390f5c7
commit a3428b0231
2 changed files with 20 additions and 20 deletions
+18 -19
View File
@@ -1,11 +1,8 @@
const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const path = require('path');
const { promisify } = require('util');
const fs = require('fs');
const writeFileAsync = promisify(fs.writeFile);
const unlinkAsync = promisify(fs.unlink);
const crypto = require('crypto');
const { promisifyAll } = require('tsubaki');
const fs = promisifyAll(require('fs'));
module.exports = class DECTalkCommand extends Command {
constructor(client) {
@@ -43,30 +40,32 @@ module.exports = class DECTalkCommand extends Command {
}
if (!channel.joinable) return msg.say('Your voice channel is not joinable.');
if (this.client.voiceConnections.has(channel.guild.id)) return msg.say('I am already playing a sound.');
const file = path.join(__dirname, '..', '..', 'assets', `${crypto.randomBytes(5).toString('hex')}.mp3`);
const file = path.join(__dirname, '..', '..', 'assets', `${channel.guild.id}.mp3`);
try {
const connection = await channel.join();
const { body } = await snekfetch
.get('http://tts.cyzon.us/tts', { followRedirects: true })
.query({ text });
await writeFileAsync(file, body, { encoding: 'binary' });
await fs.writeFileAsync(file, body, { encoding: 'binary' });
await msg.react('🔉');
const dispatcher = connection.playFile(file);
dispatcher.once('end', async () => {
await unlinkAsync(file);
channel.leave();
await msg.react('✅');
});
dispatcher.once('error', async () => {
await unlinkAsync(file);
channel.leave();
await msg.react('⚠');
});
dispatcher.once('end', async () => this.finish(file, channel, msg));
dispatcher.once('error', async () => this.finish(file, channel, msg, true));
return null;
} catch (err) {
channel.leave();
if (fs.existsSync(file)) await unlinkAsync(file);
this.finish(file, channel, msg, true);
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
async finish(file, channel, msg, fail = false) {
try {
if (fs.existsSync(file)) await fs.unlinkAsync(file);
await msg.react(fail ? '⚠' : '✅');
} catch (err) {
await msg.react('⚠');
} finally {
channel.leave();
}
}
};
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "44.0.1",
"version": "44.0.2",
"description": "Your personal server companion.",
"main": "Shard.js",
"scripts": {
@@ -40,6 +40,7 @@
"mathjs": "^3.16.4",
"node-opus": "^0.2.7",
"snekfetch": "^3.4.2",
"tsubaki": "^1.2.0",
"uws": "^8.14.1",
"xml-js": "^1.4.2"
},