diff --git a/src/commands/curator/support.js b/src/commands/curator/support.js new file mode 100644 index 0000000..b6d05c5 --- /dev/null +++ b/src/commands/curator/support.js @@ -0,0 +1,52 @@ +const Command = require('../../structures/Command'); +const Util = require('../../util'); + +module.exports = class Support extends Command { + get name() { return 'support'; } + + get _options() { return { + aliases: ['sup'], + permissions: ['curatorOrAdmin'], + minimumArgs: 2 + }; } + + async exec(message, { args }) { + const givenAmount = Util.LBRY.ensureDecimal(args[1]); + if (!givenAmount) + return message.channel.createMessage('The second argument must be a numeric amount of LBC to send!'); + + const givenClaim = args[0]; + if (!/^[a-f0-9]{40}$/.test(givenClaim)) + // @TODO use claim_search for invalid claim ids + return message.channel.createMessage('That Claim ID isn\'t valid.'); + + const account = await Util.LBRY.findOrCreateAccount(this.client, message.author.id); + if (account.newAccount) { + // Wait for the blockchain to complete the funding + await message.channel.sendTyping(); + await Util.halt(3000); + } + + // Get and check balance + const walletResponse = await this.client.lbry.accountBalance(account.accountID); + const wallet = await walletResponse.json(); + if (await this.handleResponse(message, walletResponse, wallet)) return; + const balance = wallet.result.available; + if (parseFloat(givenAmount) > parseFloat(balance)) + return message.channel.createMessage('You don\'t have enough LBC to do this!'); + + // Create support + const response = await this.client.lbry.createSupport({ + accountID: account, claimID: givenClaim, amount: givenAmount }); + const transaction = await response.json(); + if (await this.handleResponse(message, response, transaction)) return; + const txid = transaction.result.txid; + message.channel.createMessage(`Support successful! https://explorer.lbry.com/tx/${txid}`); + } + + get metadata() { return { + category: 'Curator', + description: 'Support a given claim.', + usage: ' ' + }; } +}; diff --git a/src/structures/LBRY.js b/src/structures/LBRY.js index d1c86a7..4ff0e9c 100644 --- a/src/structures/LBRY.js +++ b/src/structures/LBRY.js @@ -147,7 +147,7 @@ class LBRY { createSupport({ accountID, claimID, amount }) { return this._sdkRequest('support_create', { account_id: accountID, claim_id: claimID, - amount, funding_account_ids: accountID }); + amount: Util.LBRY.ensureDecimal(amount), funding_account_ids: accountID }); } /** diff --git a/src/util.js b/src/util.js index b3453f2..fe07fd8 100644 --- a/src/util.js +++ b/src/util.js @@ -145,6 +145,16 @@ Util.resolveToUserID = (arg) => { else return null; }; +/** + * Make a promise that resolves after some time + * @memberof Util. + * @param {string} arg + * @returns {?string} + */ +Util.halt = (ms) => { + return new Promise(resolve => setTimeout(resolve, ms)); +}; + /** * Hastebin-related functions * @memberof Util.