diff options
| -rw-r--r-- | api.js | 2 | ||||
| -rw-r--r-- | commands/vainsocial/guild_add.js | 2 | ||||
| -rw-r--r-- | commands/vainsocial/guild_update.js | 33 | ||||
| -rw-r--r-- | commands/vainsocial/guild_view.js | 2 | ||||
| -rw-r--r-- | commands/vainsocial/me.js | 3 | ||||
| -rw-r--r-- | views/guild.js | 11 | ||||
| -rw-r--r-- | views/guild_create.js | 2 | ||||
| -rw-r--r-- | views/guild_progress.js | 33 | ||||
| -rw-r--r-- | views/guild_update.js | 34 | ||||
| -rw-r--r-- | views/matches.js | 7 | ||||
| -rw-r--r-- | views/player.js | 19 | ||||
| -rw-r--r-- | views/register.js | 6 |
12 files changed, 117 insertions, 37 deletions
@@ -200,7 +200,7 @@ module.exports.getGuild = async (token) => { // add user to guild module.exports.addToGuild = async (token, member) => { - const membership = await postFE("/guild/members", { + const membership = await api.postFE("/guild/members", { user_token: token, member_name: member }, "guild+" + token); diff --git a/commands/vainsocial/guild_add.js b/commands/vainsocial/guild_add.js index 8474724..8fe952c 100644 --- a/commands/vainsocial/guild_add.js +++ b/commands/vainsocial/guild_add.js @@ -33,7 +33,7 @@ Register IGNs to your Guild. await Promise.map(playersWaiters, async (waiter, idx) => { await api.upsearchPlayer(args[idx]); let success = false; - while (await waiter.next() != undefined) { + while (["stats_update", undefined].indexOf(await waiter.next())) { playersData[args[idx]] = await api.getPlayer(args[idx]); await guildAddView.respond(); success = true; diff --git a/commands/vainsocial/guild_update.js b/commands/vainsocial/guild_update.js index 66e8a5f..7d36b0e 100644 --- a/commands/vainsocial/guild_update.js +++ b/commands/vainsocial/guild_update.js @@ -26,21 +26,42 @@ Update the match history for all your Guild members. // internal / premium: immediately call backend player refresh async run(msg, args) { util.trackAction(msg, "vainsocial-guild-update"); + // obj of ign: player let playersData = {}; - const playersWaiters = args.map((name) => api.subscribeUpdates(name)), - guildUpdateView = new GuildMembersProgressView(msg, playersData); + // collect an array of IGNs + let names, guild; + const guildUpdateView = new GuildMembersProgressView(msg, + playersData); + try { + guild = await api.getGuild(msg.author.id); + names = guild.members.map((m) => m.player.name); + } catch (err) { + console.log(err); + return await guildUpdateView.error(err.error.err); + } + // update all the IGNs + const playersWaiters = names.map((name) => api.subscribeUpdates(name)); // create waiter dict & data dict await Promise.map(playersWaiters, async (waiter, idx) => { - await api.updatePlayer(args[idx]); + await api.updatePlayer(names[idx]); let success = false; - while (await waiter.next() != undefined) { - playersData[args[idx]] = await api.getPlayer(args[idx]); + while (["stats_update", undefined].indexOf(await waiter.next())) { + try { + playersData[names[idx]] = await api.getPlayer(names[idx]); + } catch (err) { + playersData[names[idx]] = undefined; + } await guildUpdateView.respond(); success = true; } }); await guildUpdateView.respond("Your Guild's fame is being updated…"); - await api.calculateGuild(guild.id, msg.author.id); + try { + await api.calculateGuild(guild.id, msg.author.id); + } catch (err) { + console.log(err); + await guildUpdateView.error(err.error.err); + } await guildUpdateView.respond("Your Guild was updated."); } }; diff --git a/commands/vainsocial/guild_view.js b/commands/vainsocial/guild_view.js index 472b284..c8375d4 100644 --- a/commands/vainsocial/guild_view.js +++ b/commands/vainsocial/guild_view.js @@ -5,6 +5,7 @@ const Commando = require("discord.js-commando"), oneLine = require("common-tags").oneLine, util = require("../../util"), + api = require("../../api"), GuildOverviewView = require("../../views/guild"); module.exports = class ViewGuildCommand extends Commando.Command { @@ -38,6 +39,7 @@ Show a summary of your Guild. const guild = await api.getGuild(msg.author.id); await guildOverviewView.respond(guild); } catch (err) { + console.log(err); return await guildOverviewView.error(err.error.err); } } diff --git a/commands/vainsocial/me.js b/commands/vainsocial/me.js index d2afb24..de87b4c 100644 --- a/commands/vainsocial/me.js +++ b/commands/vainsocial/me.js @@ -37,7 +37,8 @@ Store your in game name for quicker access to other commands and for Guild manag const registerView = new RegisterView(msg, args.name); await api.upsearchPlayer(args.name); try { - await api.subscribeUpdates(args.name).next(); + const waiter = api.subscribeUpdates(args.name); + while (await waiter.next() != "stats_update"); await api.setUser(msg.author.id, args.name); } catch (err) { console.log(err); diff --git a/views/guild.js b/views/guild.js index a5eedef..23c2afe 100644 --- a/views/guild.js +++ b/views/guild.js @@ -24,16 +24,11 @@ module.exports = class extends View { "", "vainsocial-guild-view") .setDescription(await this.text(guild.members)); return embed; - }; + } - async respond(guild) { - if (guild == undefined) { - this.response = await util.respond(this.msg, - strings.notRegistered, this.response); - return this.response; - } + async respond(guild, extra="") { this.response = await util.respond(this.msg, await this.embed(guild), this.response); return this.response; - }; + } } diff --git a/views/guild_create.js b/views/guild_create.js index 8e7be81..38720e1 100644 --- a/views/guild_create.js +++ b/views/guild_create.js @@ -38,7 +38,7 @@ module.exports = class extends View { // TODO move to super class async respond() { this.response = await util.respond(this.msg, - await this.text(), this.response); + await this.text() + "\n" + await this.help(), this.response); if (!this.hasButtons) { await util.reactionButtons(this.response, await this.buttons()); diff --git a/views/guild_progress.js b/views/guild_progress.js new file mode 100644 index 0000000..163c627 --- /dev/null +++ b/views/guild_progress.js @@ -0,0 +1,33 @@ +#!/usr/bin/node +/* jshint esnext:true */ +"use strict"; + +const View = require("./view"), + util = require("../util"), + api = require("../api"), + strings = require("../strings"), + oneLine = require("common-tags").oneLine; + +const GuildMembersProgressView = module.exports; + +module.exports = class extends View { + constructor(msg, players) { + super(msg); + this.players = players; + } + + // players: obj, key=ign, value=player + async text(players) { + return Object.entries(players).map((tuple) => + (tuple[1] == undefined)? + `Loading ${tuple[0]}…` + : `Loaded ${tuple[0]}.` + ).join("\n"); + } + + async respond(extra="") { + this.response = await util.respond(this.msg, + await this.text(this.players) + "\n" + extra, this.response); + return this.response; + }; +} diff --git a/views/guild_update.js b/views/guild_update.js new file mode 100644 index 0000000..cb32dc3 --- /dev/null +++ b/views/guild_update.js @@ -0,0 +1,34 @@ +#!/usr/bin/node +/* jshint esnext:true */ +"use strict"; + +const View = require("./view"), + util = require("../util"), + api = require("../api"), + strings = require("../strings"), + oneLine = require("common-tags").oneLine; + +const GuildAddView = module.exports; + +// match detail view +module.exports = class extends View { + constructor(msg, players) { + super(msg); + this.players = players; + } + + // players: obj, key=ign, value=player + async text(players) { + return Object.entries(players).map((tuple) => + (tuple[1] == undefined)? + `Loading ${tuple[0]}…` + : `Loaded ${tuple[0]}.` + ).join("\n"); + } + + async respond() { + this.response = await util.respond(this.msg, + await this.text(this.players), this.response); + return this.response; + }; +} diff --git a/views/matches.js b/views/matches.js index 279bf9c..a79654b 100644 --- a/views/matches.js +++ b/views/matches.js @@ -39,8 +39,8 @@ Score | ${emojiScore} \`${Math.floor(100 * participant.stats.impact_score)}%\` const matchesPart = matches.slice(0, MATCH_HISTORY_LEN); // build embed - let embed = util.vainsocialEmbed( - this.ign, "player/" + this.ign, "vainsocial-matches") + let embed = util.vainsocialEmbed(this.ign, + "player/" + this.ign, "vainsocial-matches") .setDescription(`Last ${matchesPart.length} casual and ranked matches.\n` + await this.help()) .setTimestamp(new Date(matchesPart[0].created_at)); @@ -60,9 +60,8 @@ Score | ${emojiScore} \`${Math.floor(100 * participant.stats.impact_score)}%\` let reactions = {}; matchesPart.forEach((m, idx) => reactions[strings.emojiCount[idx]] = async () => { - console.log("react"); util.trackAction(this.msg, "reaction-match", m.match_api_id); - await new MatchView(this.msg, m.match_api_id).respond(); + await new MatchView(this.msg).respond(m.match_api_id); }); return reactions; } diff --git a/views/player.js b/views/player.js index ec5d38b..a481c69 100644 --- a/views/player.js +++ b/views/player.js @@ -72,7 +72,7 @@ ${emoji.symbols["1234"]} or ${util.usg(this.msg, "vh " + this.ign)} for more*`; let reactions = {}; reactions[emoji.symbols.information_source] = async () => { util.trackAction(this.msg, "reaction-match", player.name); - await new MatchView(this.msg, matches[0].match_api_id).respond(); + await new MatchView(this.msg).respond(matches[0].match_api_id); }; reactions[emoji.symbols["1234"]] = async () => { util.trackAction(this.msg, "reaction-matches", player.name); @@ -82,16 +82,13 @@ ${emoji.symbols["1234"]} or ${util.usg(this.msg, "vh " + this.ign)} for more*`; } async respond() { - const [player, matches] = await Promise.all([ - api.getPlayer(this.ign), - api.getMatches(this.ign) - ]); - if (player == undefined) { - this.response = await util.respond(this.msg, - strings.loading(this.ign), this.response); - return this.response; - } - if (matches.length == 0) { + let player, matches; + try { + [player, matches] = await Promise.all([ + api.getPlayer(this.ign), + api.getMatches(this.ign) + ]); + } catch (err) { this.response = await util.respond(this.msg, strings.loading(this.ign), this.response); return this.response; diff --git a/views/register.js b/views/register.js index 9943299..1df3bf1 100644 --- a/views/register.js +++ b/views/register.js @@ -19,7 +19,7 @@ module.exports = class extends View { } async text() { - return `You are now registered at VainSocial, ${this.msg.author.mention}.`; + return `You are now registered at VainSocial, @${this.msg.author.tag}.`; } async help() { return `*${emoji.symbols.repeat} or ${util.usg(this.msg, "v")} to view your profile, ${util.usg(this.msg, "vgcreate")} to create a Guild*` @@ -33,10 +33,8 @@ module.exports = class extends View { return reactions; } async respond() { - console.log("************************"); - console.log(await this.text()); this.response = await util.respond(this.msg, - await this.text(), this.response); + await this.text() + "\n" + await this.help(), this.response); if (!this.hasButtons) { await util.reactionButtons(this.response, await this.buttons()); |
