Add withdraw command

This commit is contained in:
Snazzah 2020-08-12 02:03:39 -05:00
parent fb536a7529
commit c7f7abe49f
No known key found for this signature in database
GPG key ID: 5E71D54F3D86282E
4 changed files with 54 additions and 8 deletions

View file

@ -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` +

View file

@ -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}`);
}

View file

@ -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: '<amount> <address>'
}; }
};

View file

@ -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}