mirror of
https://github.com/LBRYFoundation/curate.git
synced 2025-08-23 17:37:25 +00:00
Correct AbandonAll Command; Fixes on supports command; Tweaks to util and sqlitedb for expanded functionality of returning claim amounts.
This commit is contained in:
parent
44fcbe589f
commit
95abb1429d
4 changed files with 63 additions and 81 deletions
|
@ -12,24 +12,41 @@ module.exports = class AbaondonAll extends Command {
|
|||
|
||||
// @TODO: Refactor this command to be able to abandon all supports on the bot.
|
||||
async exec(message, { args }) {
|
||||
const discordID = Util.resolveToUserID(args[0]);
|
||||
if (!discordID)
|
||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||
const account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
||||
if (!account.accountID)
|
||||
return message.channel.createMessage('That user does not have an account.');
|
||||
if (args.length) {
|
||||
const discordID = Util.resolveToUserID(args[0]);
|
||||
if (!discordID)
|
||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||
|
||||
const supportsCount = await Util.LBRY.getSupportsCount(this.client, account.accountID);
|
||||
if (supportsCount <= 0)
|
||||
return message.channel.createMessage('That user does not have any supports.');
|
||||
const account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
||||
if (!account.accountID)
|
||||
return message.channel.createMessage('That user does not have an account.');
|
||||
|
||||
if (!await this.client.messageAwaiter.confirm(message, {
|
||||
header:
|
||||
`Are you sure you want to abandon all supports from that account? *(${
|
||||
supportsCount.toLocaleString()} support[s])*`
|
||||
})) return;
|
||||
await Util.LBRY.abandonAllClaims(this.client, account.accountID);
|
||||
return message.channel.createMessage('Abandoned all claims.');
|
||||
const supportsCount = await Util.LBRY.getSupportsCount(this.client, account.accountID);
|
||||
if (supportsCount <= 0)
|
||||
return message.channel.createMessage('That user does not have any supports.');
|
||||
|
||||
if (!await this.client.messageAwaiter.confirm(message, {
|
||||
header:
|
||||
`Are you sure you want to abandon **all supports** from that account? *(${
|
||||
supportsCount.toLocaleString()} support[s])*`
|
||||
})) return;
|
||||
await Util.LBRY.abandonAllClaims(this.client, account.accountID);
|
||||
return message.channel.createMessage(`Abandoned ${supportsCount.toLocaleString()} claim(s).`);
|
||||
} else {
|
||||
if (!await this.client.messageAwaiter.confirm(message, {
|
||||
header: 'Are you sure you want to abandon **all supports** from **all accounts**?'
|
||||
})) return;
|
||||
await this.client.startTyping(message.channel);
|
||||
const pairs = await this.client.sqlite.getAll();
|
||||
const count = 0;
|
||||
for (let i = 0, len = pairs.length; i < len; i++) {
|
||||
const pair = pairs[i];
|
||||
const result = await Util.LBRY.abandonAllClaims(this.client, pair.lbryID);
|
||||
count += result.count;
|
||||
}
|
||||
this.client.stopTyping(message.channel);
|
||||
return message.channel.createMessage(`Abandoned ${count.toLocaleString()} claim(s).`);
|
||||
}
|
||||
}
|
||||
|
||||
get metadata() { return {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const Command = require('../../structures/Command');
|
||||
const GenericPager = require('../../structures/GenericPager');
|
||||
const Util = require('../../util');
|
||||
|
||||
module.exports = class Supports extends Command {
|
||||
|
@ -9,20 +10,20 @@ module.exports = class Supports extends Command {
|
|||
minimumArgs: 0
|
||||
}; }
|
||||
async exec(message, { args }) {
|
||||
let account, givenClaim;
|
||||
let account, givenClaim, discordID;
|
||||
if (args.length === 2) {
|
||||
// Check for if claim ID and discord user is given
|
||||
givenClaim = args[0];
|
||||
givenClaim = args[1];
|
||||
if (!/^[a-f0-9]{40}$/.test(givenClaim))
|
||||
return message.channel.createMessage('That Claim ID isn\'t valid.');
|
||||
|
||||
const discordID = Util.resolveToUserID(args[1]);
|
||||
discordID = Util.resolveToUserID(args[0]);
|
||||
if (!discordID)
|
||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||
account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
||||
} else if (args.length === 1) {
|
||||
// Check for only if a discord user is given
|
||||
const discordID = Util.resolveToUserID(args[0]);
|
||||
discordID = Util.resolveToUserID(args[0]);
|
||||
if (!discordID)
|
||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||
account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
||||
|
@ -35,34 +36,24 @@ module.exports = class Supports extends Command {
|
|||
return message.channel.createMessage('That Discord user does not have an account.');
|
||||
|
||||
const supportsCount = await Util.LBRY.getSupportsCount(this.client, account.accountID);
|
||||
if (!givenClaim) {
|
||||
const supportsResponse = await this.client.lbry.listSupports({
|
||||
accountID: account.accountID, page_size: supportsCount });
|
||||
console.debug(`Displaying supports for ${account.accountID} (${supportsCount})`);
|
||||
const supports = (await supportsResponse.json()).result.items;
|
||||
// @TODO use pagination
|
||||
for (let i = 0, len = supports.length; i < len; i++) {
|
||||
const support = supports[i];
|
||||
message.channel.createMessage(`ClaimID: \`${support.claim_id}\`\nClaim Name: \`${support.name}\`\n
|
||||
Claim URL: \`${support.permanent_url}\`\nSupport Ammount: \`${support.amount}\`\n`);
|
||||
}
|
||||
} else {
|
||||
const supportsResponse = await this.client.lbry.listSupports({
|
||||
accountID: account.accountID, page_size: supportsCount, claim_id: givenClaim });
|
||||
console.debug(
|
||||
`Displaying supports for ${account.accountID} and claimID ${givenClaim}, (${supportsCount})`);
|
||||
const supports = (await supportsResponse.json()).result.items;
|
||||
// @TODO use pagination
|
||||
for (let i = 0, len = supports.length; i < len; i++) {
|
||||
const support = supports[i];
|
||||
message.channel.createMessage(`ClaimID: \`${support.claim_id}\`\nClaim Name: \`${support.name}\`\n
|
||||
Claim URL: \`${support.permanent_url}\`\nSupport Ammount: \`${support.amount}\`\n`);
|
||||
}
|
||||
}
|
||||
if (supportsCount <= 0)
|
||||
return message.channel.createMessage('No supports found.');
|
||||
|
||||
const supportsResponse = await this.client.lbry.listSupports({
|
||||
accountID: account.accountID, page_size: supportsCount, claimID: givenClaim });
|
||||
console.debug(
|
||||
`Displaying supports for ${account.accountID}${givenClaim ? ` and claimID ${givenClaim}` : ''}, (${supportsCount})`);
|
||||
const supports = (await supportsResponse.json()).result.items;
|
||||
const paginator = new GenericPager(this.client, message, {
|
||||
items: supports,
|
||||
header: `All supports for <@${discordID || message.author.id}>${givenClaim ? ` on claim \`${givenClaim}\`` : ''}`, itemTitle: 'Supports',
|
||||
display: item => `[${item.name}](${item.permanent_url})#\`${item.claim_id}\` (${item.amount} LBC)`
|
||||
});
|
||||
return paginator.start(message.channel.id, message.author.id);
|
||||
}
|
||||
get metadata() { return {
|
||||
category: 'Curator',
|
||||
description: 'Shows the user\'s list of supports.',
|
||||
usage: '[claimID] [mention/discordID]'
|
||||
usage: '[id/@mention] [claimID]'
|
||||
}; }
|
||||
};
|
||||
|
|
|
@ -52,4 +52,12 @@ module.exports = class SQLiteDB {
|
|||
remove(discordID) {
|
||||
return this.model.destroy({ where: { discordID } });
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all pairs in the database
|
||||
*/
|
||||
async getAll() {
|
||||
const items = await this.model.findAll();
|
||||
return items.map(item => item.get({ plain: true }));
|
||||
}
|
||||
};
|
38
src/util.js
38
src/util.js
|
@ -149,7 +149,7 @@ Util.resolveToUserID = (arg) => {
|
|||
* Make a promise that resolves after some time
|
||||
* @memberof Util.
|
||||
* @param {string} arg
|
||||
* @returns {Promise}
|
||||
* @returns {?string}
|
||||
*/
|
||||
Util.halt = (ms) => {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
@ -201,12 +201,6 @@ Util.Hastebin = {
|
|||
* @memberof Util.
|
||||
*/
|
||||
Util.LBRY = {
|
||||
/**
|
||||
* Find or create a user's account
|
||||
* @param {Client} client
|
||||
* @param {string} discordID
|
||||
* @param {boolean} [create=true] Whether or not to create the account
|
||||
*/
|
||||
async findOrCreateAccount(client, discordID, create = true) {
|
||||
// Check SQLite
|
||||
const pair = await client.sqlite.get(discordID);
|
||||
|
@ -232,28 +226,14 @@ Util.LBRY = {
|
|||
};
|
||||
} else return { accountID: null };
|
||||
},
|
||||
/**
|
||||
* Gets the amount of accounts on the SDK
|
||||
* @param {Client} client
|
||||
*/
|
||||
async getAccountCount(client) {
|
||||
const response = await client.lbry.listAccounts({ page_size: 1 }).then(r => r.json());
|
||||
return response.result.total_items;
|
||||
},
|
||||
/**
|
||||
* Gets the amount of supports from an account
|
||||
* @param {Client} client
|
||||
* @param {string} accountID
|
||||
*/
|
||||
async getSupportsCount(client, accountID) {
|
||||
const response = await client.lbry.listSupports({ accountID, page_size: 1 }).then(r => r.json());
|
||||
return response.result.total_items;
|
||||
},
|
||||
/**
|
||||
* Creates an account
|
||||
* @param {Client} client
|
||||
* @param {string} discordID
|
||||
*/
|
||||
async createAccount(client, discordID) {
|
||||
console.info('Creating account for user', discordID);
|
||||
const account = await client.lbry.createAccount(discordID).then(r => r.json());
|
||||
|
@ -264,21 +244,11 @@ Util.LBRY = {
|
|||
console.info('Funded account', account.result.id, transaction.result.txid);
|
||||
return { account, transaction };
|
||||
},
|
||||
/**
|
||||
* Ensures a decimal can be used by the SDK
|
||||
* @param {string} str
|
||||
*/
|
||||
ensureDecimal(str) {
|
||||
const num = parseFloat(str);
|
||||
if (isNaN(num)) return null;
|
||||
return Number.isInteger(num) ? `${num}.0` : num.toString();
|
||||
},
|
||||
/**
|
||||
* Deletes an account
|
||||
* @param {Client} client
|
||||
* @param {string} discordID
|
||||
* @param {string} lbryID
|
||||
*/
|
||||
async deleteAccount(client, discordID, lbryID) {
|
||||
// Abandon supports
|
||||
await Util.LBRY.abandonAllClaims(client, lbryID);
|
||||
|
@ -293,11 +263,6 @@ Util.LBRY = {
|
|||
await client.lbry.removeAccount(lbryID);
|
||||
await client.sqlite.remove(discordID);
|
||||
},
|
||||
/**
|
||||
* Abandons all claims from an account
|
||||
* @param {Client} client
|
||||
* @param {string} lbryID
|
||||
*/
|
||||
async abandonAllClaims(client, lbryID) {
|
||||
if (!lbryID)
|
||||
throw new Error('lbryID must be defined!');
|
||||
|
@ -310,5 +275,6 @@ Util.LBRY = {
|
|||
const support = supports[i];
|
||||
await client.lbry.abandonSupport({ claimID: support.claim_id, accountID: lbryID });
|
||||
}
|
||||
return { count: supports.length };
|
||||
}
|
||||
};
|
Loading…
Add table
Reference in a new issue