From c7f7abe49f0347e9832945218e863bf6ba1e8b48 Mon Sep 17 00:00:00 2001 From: Snazzah Date: Wed, 12 Aug 2020 02:03:39 -0500 Subject: [PATCH] Add withdraw command --- src/commands/admin/adminbalance.js | 5 +--- src/commands/admin/deposit.js | 5 +--- src/commands/admin/withdraw.js | 38 ++++++++++++++++++++++++++++++ src/structures/Command.js | 14 +++++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/commands/admin/withdraw.js diff --git a/src/commands/admin/adminbalance.js b/src/commands/admin/adminbalance.js index 4ef0455..411edf9 100644 --- a/src/commands/admin/adminbalance.js +++ b/src/commands/admin/adminbalance.js @@ -10,10 +10,7 @@ module.exports = class AdminBalance extends Command { async exec(message) { const response = await this.client.lbry.walletBalance(); - if (response.status !== 200) { - console.error('SDK error in adminbalance', response, await response.text()); - return message.channel.createMessage(`LBRY-SDK returned ${response.status}, check console.`); - } + if (this.handleResponse(message, response)) return; const wallet = await response.json(); return message.channel.createMessage({ embed: { description: `**Available:** ${wallet.result.available} LBC\n\n` + diff --git a/src/commands/admin/deposit.js b/src/commands/admin/deposit.js index a6740c7..fc43858 100644 --- a/src/commands/admin/deposit.js +++ b/src/commands/admin/deposit.js @@ -10,10 +10,7 @@ module.exports = class Deposit extends Command { async exec(message) { const response = await this.client.lbry.listAddresses(); - if (response.status !== 200) { - console.error('SDK error in deposit', response, await response.text()); - return message.channel.createMessage(`LBRY-SDK returned ${response.status}, check console.`); - } + if (this.handleResponse(message, response)) return; const address = await response.json(); return message.channel.createMessage(`Address: ${address.result.items[0].address}`); } diff --git a/src/commands/admin/withdraw.js b/src/commands/admin/withdraw.js new file mode 100644 index 0000000..b07dc25 --- /dev/null +++ b/src/commands/admin/withdraw.js @@ -0,0 +1,38 @@ +const Command = require('../../structures/Command'); + +module.exports = class Withdraw extends Command { + get name() { return 'withdraw'; } + + get _options() { return { + aliases: ['wd'], + permissions: ['admin'], + minimumArgs: 2 + }; } + + async exec(message, { args }) { + const amount = args[0]; + if (isNaN(parseFloat(amount))) + return message.channel.createMessage('The first argument must be a numeric amount of LBC to send!'); + + // Check if the balance is more than requested + const balance = await this.client.lbry.walletBalance(); + if (this.handleResponse(message, balance)) return; + const availableBalance = parseFloat((await balance.json()).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 (this.handleResponse(message, response)) return; + const txid = (await response.json()).result.inputs[0].txid; + return message.channel.createMessage(`Sent ${parseFloat(amount)} LBC to ${args[1]}.\n` + + `https://explorer.lbry.com/tx/${txid}`); + } + + get metadata() { return { + category: 'Admin', + description: 'Sends funds to an address from the master wallet.', + usage: '
' + }; } +}; diff --git a/src/structures/Command.js b/src/structures/Command.js index f711c84..2c3f045 100644 --- a/src/structures/Command.js +++ b/src/structures/Command.js @@ -78,6 +78,20 @@ class Command { // eslint-disable-next-line no-empty-function, no-unused-vars exec(Message, opts) { } + /** + * @private + */ + async handleResponse(message, response) { + if (response.status !== 200) { + const error = response.status === 500 ? + { message: 'Internal server error' } : (await response.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; + } + return false; + } + /** * The options for the command * @type {Object}