From a3428b0231068da72e80c0cbe856ba5c8129cf1f Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 2 Oct 2017 13:35:18 +0000 Subject: [PATCH] Rewrite DECTalk Handling --- commands/text-edit/dec-talk.js | 37 +++++++++++++++++----------------- package.json | 3 ++- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/commands/text-edit/dec-talk.js b/commands/text-edit/dec-talk.js index dc76bfc3..a86337e5 100644 --- a/commands/text-edit/dec-talk.js +++ b/commands/text-edit/dec-talk.js @@ -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(); + } + } }; diff --git a/package.json b/package.json index ecbb066a..0cb816f4 100644 --- a/package.json +++ b/package.json @@ -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" },