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,9 +12,11 @@ module.exports = class AbaondonAll extends Command {
|
||||||
|
|
||||||
// @TODO: Refactor this command to be able to abandon all supports on the bot.
|
// @TODO: Refactor this command to be able to abandon all supports on the bot.
|
||||||
async exec(message, { args }) {
|
async exec(message, { args }) {
|
||||||
|
if (args.length) {
|
||||||
const discordID = Util.resolveToUserID(args[0]);
|
const discordID = Util.resolveToUserID(args[0]);
|
||||||
if (!discordID)
|
if (!discordID)
|
||||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||||
|
|
||||||
const account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
const account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
||||||
if (!account.accountID)
|
if (!account.accountID)
|
||||||
return message.channel.createMessage('That user does not have an account.');
|
return message.channel.createMessage('That user does not have an account.');
|
||||||
|
@ -25,11 +27,26 @@ module.exports = class AbaondonAll extends Command {
|
||||||
|
|
||||||
if (!await this.client.messageAwaiter.confirm(message, {
|
if (!await this.client.messageAwaiter.confirm(message, {
|
||||||
header:
|
header:
|
||||||
`Are you sure you want to abandon all supports from that account? *(${
|
`Are you sure you want to abandon **all supports** from that account? *(${
|
||||||
supportsCount.toLocaleString()} support[s])*`
|
supportsCount.toLocaleString()} support[s])*`
|
||||||
})) return;
|
})) return;
|
||||||
await Util.LBRY.abandonAllClaims(this.client, account.accountID);
|
await Util.LBRY.abandonAllClaims(this.client, account.accountID);
|
||||||
return message.channel.createMessage('Abandoned all claims.');
|
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 {
|
get metadata() { return {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
|
const GenericPager = require('../../structures/GenericPager');
|
||||||
const Util = require('../../util');
|
const Util = require('../../util');
|
||||||
|
|
||||||
module.exports = class Supports extends Command {
|
module.exports = class Supports extends Command {
|
||||||
|
@ -9,20 +10,20 @@ module.exports = class Supports extends Command {
|
||||||
minimumArgs: 0
|
minimumArgs: 0
|
||||||
}; }
|
}; }
|
||||||
async exec(message, { args }) {
|
async exec(message, { args }) {
|
||||||
let account, givenClaim;
|
let account, givenClaim, discordID;
|
||||||
if (args.length === 2) {
|
if (args.length === 2) {
|
||||||
// Check for if claim ID and discord user is given
|
// Check for if claim ID and discord user is given
|
||||||
givenClaim = args[0];
|
givenClaim = args[1];
|
||||||
if (!/^[a-f0-9]{40}$/.test(givenClaim))
|
if (!/^[a-f0-9]{40}$/.test(givenClaim))
|
||||||
return message.channel.createMessage('That Claim ID isn\'t valid.');
|
return message.channel.createMessage('That Claim ID isn\'t valid.');
|
||||||
|
|
||||||
const discordID = Util.resolveToUserID(args[1]);
|
discordID = Util.resolveToUserID(args[0]);
|
||||||
if (!discordID)
|
if (!discordID)
|
||||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||||
account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
||||||
} else if (args.length === 1) {
|
} else if (args.length === 1) {
|
||||||
// Check for only if a discord user is given
|
// Check for only if a discord user is given
|
||||||
const discordID = Util.resolveToUserID(args[0]);
|
discordID = Util.resolveToUserID(args[0]);
|
||||||
if (!discordID)
|
if (!discordID)
|
||||||
return message.channel.createMessage('That Discord user isn\'t valid.');
|
return message.channel.createMessage('That Discord user isn\'t valid.');
|
||||||
account = await Util.LBRY.findOrCreateAccount(this.client, discordID, false);
|
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.');
|
return message.channel.createMessage('That Discord user does not have an account.');
|
||||||
|
|
||||||
const supportsCount = await Util.LBRY.getSupportsCount(this.client, account.accountID);
|
const supportsCount = await Util.LBRY.getSupportsCount(this.client, account.accountID);
|
||||||
if (!givenClaim) {
|
if (supportsCount <= 0)
|
||||||
|
return message.channel.createMessage('No supports found.');
|
||||||
|
|
||||||
const supportsResponse = await this.client.lbry.listSupports({
|
const supportsResponse = await this.client.lbry.listSupports({
|
||||||
accountID: account.accountID, page_size: supportsCount });
|
accountID: account.accountID, page_size: supportsCount, claimID: givenClaim });
|
||||||
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(
|
console.debug(
|
||||||
`Displaying supports for ${account.accountID} and claimID ${givenClaim}, (${supportsCount})`);
|
`Displaying supports for ${account.accountID}${givenClaim ? ` and claimID ${givenClaim}` : ''}, (${supportsCount})`);
|
||||||
const supports = (await supportsResponse.json()).result.items;
|
const supports = (await supportsResponse.json()).result.items;
|
||||||
// @TODO use pagination
|
const paginator = new GenericPager(this.client, message, {
|
||||||
for (let i = 0, len = supports.length; i < len; i++) {
|
items: supports,
|
||||||
const support = supports[i];
|
header: `All supports for <@${discordID || message.author.id}>${givenClaim ? ` on claim \`${givenClaim}\`` : ''}`, itemTitle: 'Supports',
|
||||||
message.channel.createMessage(`ClaimID: \`${support.claim_id}\`\nClaim Name: \`${support.name}\`\n
|
display: item => `[${item.name}](${item.permanent_url})#\`${item.claim_id}\` (${item.amount} LBC)`
|
||||||
Claim URL: \`${support.permanent_url}\`\nSupport Ammount: \`${support.amount}\`\n`);
|
});
|
||||||
}
|
return paginator.start(message.channel.id, message.author.id);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
get metadata() { return {
|
get metadata() { return {
|
||||||
category: 'Curator',
|
category: 'Curator',
|
||||||
description: 'Shows the user\'s list of supports.',
|
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) {
|
remove(discordID) {
|
||||||
return this.model.destroy({ where: { 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
|
* Make a promise that resolves after some time
|
||||||
* @memberof Util.
|
* @memberof Util.
|
||||||
* @param {string} arg
|
* @param {string} arg
|
||||||
* @returns {Promise}
|
* @returns {?string}
|
||||||
*/
|
*/
|
||||||
Util.halt = (ms) => {
|
Util.halt = (ms) => {
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
@ -201,12 +201,6 @@ Util.Hastebin = {
|
||||||
* @memberof Util.
|
* @memberof Util.
|
||||||
*/
|
*/
|
||||||
Util.LBRY = {
|
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) {
|
async findOrCreateAccount(client, discordID, create = true) {
|
||||||
// Check SQLite
|
// Check SQLite
|
||||||
const pair = await client.sqlite.get(discordID);
|
const pair = await client.sqlite.get(discordID);
|
||||||
|
@ -232,28 +226,14 @@ Util.LBRY = {
|
||||||
};
|
};
|
||||||
} else return { accountID: null };
|
} else return { accountID: null };
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* Gets the amount of accounts on the SDK
|
|
||||||
* @param {Client} client
|
|
||||||
*/
|
|
||||||
async getAccountCount(client) {
|
async getAccountCount(client) {
|
||||||
const response = await client.lbry.listAccounts({ page_size: 1 }).then(r => r.json());
|
const response = await client.lbry.listAccounts({ page_size: 1 }).then(r => r.json());
|
||||||
return response.result.total_items;
|
return response.result.total_items;
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* Gets the amount of supports from an account
|
|
||||||
* @param {Client} client
|
|
||||||
* @param {string} accountID
|
|
||||||
*/
|
|
||||||
async getSupportsCount(client, accountID) {
|
async getSupportsCount(client, accountID) {
|
||||||
const response = await client.lbry.listSupports({ accountID, page_size: 1 }).then(r => r.json());
|
const response = await client.lbry.listSupports({ accountID, page_size: 1 }).then(r => r.json());
|
||||||
return response.result.total_items;
|
return response.result.total_items;
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* Creates an account
|
|
||||||
* @param {Client} client
|
|
||||||
* @param {string} discordID
|
|
||||||
*/
|
|
||||||
async createAccount(client, discordID) {
|
async createAccount(client, discordID) {
|
||||||
console.info('Creating account for user', discordID);
|
console.info('Creating account for user', discordID);
|
||||||
const account = await client.lbry.createAccount(discordID).then(r => r.json());
|
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);
|
console.info('Funded account', account.result.id, transaction.result.txid);
|
||||||
return { account, transaction };
|
return { account, transaction };
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* Ensures a decimal can be used by the SDK
|
|
||||||
* @param {string} str
|
|
||||||
*/
|
|
||||||
ensureDecimal(str) {
|
ensureDecimal(str) {
|
||||||
const num = parseFloat(str);
|
const num = parseFloat(str);
|
||||||
if (isNaN(num)) return null;
|
if (isNaN(num)) return null;
|
||||||
return Number.isInteger(num) ? `${num}.0` : num.toString();
|
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) {
|
async deleteAccount(client, discordID, lbryID) {
|
||||||
// Abandon supports
|
// Abandon supports
|
||||||
await Util.LBRY.abandonAllClaims(client, lbryID);
|
await Util.LBRY.abandonAllClaims(client, lbryID);
|
||||||
|
@ -293,11 +263,6 @@ Util.LBRY = {
|
||||||
await client.lbry.removeAccount(lbryID);
|
await client.lbry.removeAccount(lbryID);
|
||||||
await client.sqlite.remove(discordID);
|
await client.sqlite.remove(discordID);
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* Abandons all claims from an account
|
|
||||||
* @param {Client} client
|
|
||||||
* @param {string} lbryID
|
|
||||||
*/
|
|
||||||
async abandonAllClaims(client, lbryID) {
|
async abandonAllClaims(client, lbryID) {
|
||||||
if (!lbryID)
|
if (!lbryID)
|
||||||
throw new Error('lbryID must be defined!');
|
throw new Error('lbryID must be defined!');
|
||||||
|
@ -310,5 +275,6 @@ Util.LBRY = {
|
||||||
const support = supports[i];
|
const support = supports[i];
|
||||||
await client.lbry.abandonSupport({ claimID: support.claim_id, accountID: lbryID });
|
await client.lbry.abandonSupport({ claimID: support.claim_id, accountID: lbryID });
|
||||||
}
|
}
|
||||||
|
return { count: supports.length };
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Add table
Reference in a new issue