diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2017-05-05 22:46:34 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2017-05-05 22:46:34 +0200 |
| commit | f568816fcfd9df37e01b898a0f2b5cee58e0f9c3 (patch) | |
| tree | 32f1ba0edd66f64a23c7b39a4fce2ed12e207f67 | |
| parent | 2ad459a0910498f39c8c5abd697189b788df827a (diff) | |
| download | discordbot-f568816fcfd9df37e01b898a0f2b5cee58e0f9c3.tar.gz discordbot-f568816fcfd9df37e01b898a0f2b5cee58e0f9c3.zip | |
refactor, add beta guild member update command
| -rw-r--r-- | api.js | 15 | ||||
| -rw-r--r-- | commands/vainsocial/guild_add.js | 2 | ||||
| -rw-r--r-- | commands/vainsocial/guild_fame_calc.js | 2 | ||||
| -rw-r--r-- | commands/vainsocial/guild_update.js | 38 | ||||
| -rw-r--r-- | commands/vainsocial/me.js | 24 | ||||
| -rw-r--r-- | responses.js | 13 | ||||
| -rw-r--r-- | util.js | 5 |
7 files changed, 79 insertions, 20 deletions
@@ -170,7 +170,7 @@ module.exports.upsearchPlayer = async (name) => { // return matches module.exports.getMatches = async (name) => { - const data = await getFE("/player/" + name + "/matches/2.3.1.1", {}, + const data = await getFE("/player/" + name + "/matches/1.1.1.1", {}, 60 * 60, "matches+" + name); if (data == undefined) return []; return data[0].data; @@ -211,3 +211,16 @@ module.exports.calculateGuild = async (id, token) => { if (msg == "points_update") cache.del("guild+" + token); subscription.unsubscribe(); } + +// store Discord ID <-> IGN +module.exports.setUser = async (token, name) => { + cache.del("user+" + token); + await module.exports.post("/user", { + name: name, + user_token: token + }); +} + +// retrieve Discord ID -> IGN +module.exports.getUser = async (token) => + (await getFE("/user", { user_token: token }, 60, "user+" + token)).name; diff --git a/commands/vainsocial/guild_add.js b/commands/vainsocial/guild_add.js index b4aa864..531d3dd 100644 --- a/commands/vainsocial/guild_add.js +++ b/commands/vainsocial/guild_add.js @@ -50,7 +50,7 @@ Register IGNs to your Guild. // wait until search success while (true) { notif = await waiter.next(); - if (notif == "search_success") break; + if (notif == "stats_update") break; if (notif == undefined) { // give up await progress(`Ooops! Could not find ${user}.`, true); diff --git a/commands/vainsocial/guild_fame_calc.js b/commands/vainsocial/guild_fame_calc.js index fbd7469..e1ec912 100644 --- a/commands/vainsocial/guild_fame_calc.js +++ b/commands/vainsocial/guild_fame_calc.js @@ -11,7 +11,7 @@ module.exports = class CalculateGuildFameCommand extends Commando.Command { constructor(client) { super(client, { name: "vainsocial-guildcalc", - aliases: ["vguild-calc", "vgcalc", "vgc"], + aliases: ["vguild-calc", "vgcalc"], group: "vainsocial-guild", memberName: "vainsocial-guildcalc", description: "Update your Guild's fame.", diff --git a/commands/vainsocial/guild_update.js b/commands/vainsocial/guild_update.js new file mode 100644 index 0000000..9c9fe1d --- /dev/null +++ b/commands/vainsocial/guild_update.js @@ -0,0 +1,38 @@ +#!/usr/bin/node +/* jshint esnext:true */ +"use strict"; + +const Commando = require("discord.js-commando"), + oneLine = require("common-tags").oneLine, + Promise = require("bluebird"), + api = require("../../api"), + util = require("../../util"); + +module.exports = class UpdateGuildCommand extends Commando.Command { + constructor(client) { + super(client, { + name: "vainsocial-guildupdate", + aliases: ["vguild-update", "vgupdate"], + group: "vainsocial-guild", + memberName: "vainsocial-guildupdate", + description: "Update your Guild members' data.", + details: oneLine` +Update the match history for all your Guild members. +`, + examples: ["vgupdate"] + }); + } + // internal / premium: immediately call backend player refresh + async run(msg, args) { + util.trackAction(msg, "vainsocial-guild-update"); + const guild = await api.getGuild(msg.author.id); + if (guild == undefined) { + await msg.reply("You are not registered in any guilds."); + return; + } + // TODO progress report + await Promise.map(guild.members, + (member) => api.upsearchPlayer(member.player.name)); + await msg.reply("Your Guild members will be up to date soon."); + } +}; diff --git a/commands/vainsocial/me.js b/commands/vainsocial/me.js index 400bc70..717e36c 100644 --- a/commands/vainsocial/me.js +++ b/commands/vainsocial/me.js @@ -34,10 +34,26 @@ Store your in game name for quicker access to other commands and for Guild manag async run(msg, args) { const ign = args.name; util.trackAction(msg, "vainsocial-me", ign); - await api.post("/user", { - name: ign, - user_token: msg.author.id - }); + const player = await api.getPlayer(ign); + // TODO refactor and build a blocking function via processor + // TODO respond loading + if (player == undefined) { + const waiter = api.subscribeUpdates(ign); + await api.searchPlayer(ign); + + let success = false, notif; + // wait until search success + while (true) { + notif = await waiter.next(); + if (notif == "stats_update") break; + if (notif == undefined) { + // give up + await progress(`Ooops! Could not find ${user}.`, true); + return; + } + } + } + await api.setUser(msg.author.id, ign); await msg.reply(oneLine` You are now able to use ${util.usg(msg, "v")} to access your profile faster. You now have access to Guild management features. diff --git a/responses.js b/responses.js index a2eb83d..32ce136 100644 --- a/responses.js +++ b/responses.js @@ -116,13 +116,6 @@ Currently running on ${msg.client.guilds.size} servers.`) ); } -// return the sqlite stored ign for this user -async function nameByUser(msg) { - const user = await api.get("/user", { user_token: msg.author.id }); - if (user) return user.name; - return undefined; -} - // show player profile and last match module.exports.showUser = async (msg, args) => { let responded = false, @@ -134,7 +127,7 @@ module.exports.showUser = async (msg, args) => { // shorthand // "?" is not accepted as user input, but the default for empty if (ign == "?") { - ign = await nameByUser(msg); + ign = await api.getUser(msg.author.id); if (ign == undefined) { await msg.reply(util.formatSorryUnknown(msg)); return; @@ -214,7 +207,7 @@ module.exports.showMatch = async (msg, args) => { // shorthand if (ign == "?") { - ign = await nameByUser(msg); + ign = await api.getUser(msg.author.id); if (ign == undefined) { await msg.reply(util.formatSorryUnknown(msg)); return; @@ -288,7 +281,7 @@ module.exports.showMatches = async (msg, args) => { // shorthand if (ign == "?") { - ign = await nameByUser(msg); + ign = await api.getUser(msg.author.id); if (ign == undefined) { await msg.reply(util.formatSorryUnknown(msg)); return; @@ -114,6 +114,5 @@ module.exports.respond = async (msg, data, response) => { }; // tell the user that they need to store their name -module.exports.formatSorryUnknown = (msg) => { - return `You're unknown to our service. Try ${util.usg(msg, "help vme")}.`; -}; +module.exports.formatSorryUnknown = (msg) => + `You're unknown to our service. Try ${module.exports.usg(msg, "help vme")}.`; |
