diff --git a/src/commands/admin/adminbalance.js b/src/commands/admin/adminbalance.js index e8dca2c..ccd062b 100644 --- a/src/commands/admin/adminbalance.js +++ b/src/commands/admin/adminbalance.js @@ -10,8 +10,8 @@ module.exports = class AdminBalance extends Command { async exec(message) { const response = await this.client.lbry.walletBalance(); - if (await this.handleResponse(message, response)) return; const wallet = await response.json(); + if (await this.handleResponse(message, response, wallet)) return; return message.channel.createMessage({ embed: { description: `**Available:** ${wallet.result.available} LBC\n\n` + `Reserved in Supports: ${wallet.result.reserved_subtotals.supports} LBC\n` + diff --git a/src/commands/admin/deposit.js b/src/commands/admin/deposit.js index 0e23adc..97c3b6f 100644 --- a/src/commands/admin/deposit.js +++ b/src/commands/admin/deposit.js @@ -10,8 +10,8 @@ module.exports = class Deposit extends Command { async exec(message) { const response = await this.client.lbry.listAddresses(); - if (await this.handleResponse(message, response)) return; const address = await response.json(); + if (await this.handleResponse(message, response, address)) return; return message.channel.createMessage(`Address: ${address.result.items[0].address}`); } diff --git a/src/commands/admin/withdraw.js b/src/commands/admin/withdraw.js index fa2cc92..3981542 100644 --- a/src/commands/admin/withdraw.js +++ b/src/commands/admin/withdraw.js @@ -17,16 +17,17 @@ module.exports = class Withdraw extends Command { // Check if the balance is more than requested const balance = await this.client.lbry.walletBalance(); - if (await this.handleResponse(message, balance)) return; - const availableBalance = parseFloat((await balance.json()).result.available); + const balanceJSON = await balance.json(); + if (await this.handleResponse(message, balance, balanceJSON)) return; + const availableBalance = parseFloat(balanceJSON.result.available); if (parseFloat(amount) > availableBalance) return message.channel.createMessage( 'There is not enough available LBC in the wallet to send that amount!'); // Send to wallet const response = await this.client.lbry.sendToWallet({ amount, to: args[1] }); - if (await this.handleResponse(message, response)) return; const transaction = await response.json(); + if (await this.handleResponse(message, response, transaction)) return; console.debug('withdrew from master wallet', transaction); return message.channel.createMessage(`Sent ${parseFloat(amount)} LBC to ${args[1]}.\n` + `https://explorer.lbry.com/tx/${transaction.result.txid}`); diff --git a/src/structures/Command.js b/src/structures/Command.js index 2c3f045..0432d6c 100644 --- a/src/structures/Command.js +++ b/src/structures/Command.js @@ -81,10 +81,11 @@ class Command { /** * @private */ - async handleResponse(message, response) { - if (response.status !== 200) { + async handleResponse(message, response, json) { + if (!json) json = await response.json(); + if (response.status !== 200 || json.error) { const error = response.status === 500 ? - { message: 'Internal server error' } : (await response.json()).error; + { message: 'Internal server error' } : json.error; console.error(`SDK error in ${this.name}:${message.author.id}`, response, error); await message.channel.createMessage(`LBRY-SDK returned ${response.status}.\n${error.message}`); return true;