From 65da4cba0053bc6ee6f5442a45022bf5b4198e70 Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 19 Apr 2017 12:02:21 +0200 Subject: rotate IGNs in playing status --- api.js | 9 ++++++++- bot.js | 2 +- responses.js | 13 +++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/api.js b/api.js index 1672728..24c9a9a 100644 --- a/api.js +++ b/api.js @@ -74,13 +74,20 @@ async function getMappings() { } ); return mapping; - }); + }, { ttl: 60 * 30 }); } module.exports.mapGameMode = async function(id) { return (await getMappings())["gamemode"][id]; } +// return a set of IGN of supporters +module.exports.getGamers = async function() { + return await cache.wrap("gamers", async () => { + return (await getFE("/gamer")).map((gamer) => gamer.name); + }, { ttl: 60 * 30 }); +} + // be an async iterator // next() returns promises that are awaited until there is an update module.exports.subscribeUpdates = function(name, timeout=UPDATE_TIMEOUT) { diff --git a/bot.js b/bot.js index a7bd8e5..d4687b9 100644 --- a/bot.js +++ b/bot.js @@ -25,7 +25,7 @@ client .on("debug", console.log) .on("ready", () => { console.log(`Client ready; logged in as ${client.user.username}#${client.user.discriminator} (${client.user.id})`); - client.user.setGame("?v shutterfly | vainsocial.com"); + responses.rotateGameStatus(client); }) .on('disconnect', () => { console.warn('Disconnected!'); }) .on('reconnecting', () => { console.warn('Reconnecting...'); }) diff --git a/responses.js b/responses.js index d1284e5..39369ed 100644 --- a/responses.js +++ b/responses.js @@ -12,6 +12,7 @@ const Commando = require("discord.js-commando"), const PREVIEW = process.env.PREVIEW || true, MATCH_HISTORY_LEN = parseInt(process.env.MATCH_HISTORY_LEN) || 3, + IGN_ROTATE_TIMEOUT = parseInt(process.env.IGN_ROTATE_TIMEOUT) || 300, REACTION_TIMEOUT = parseInt(process.env.REACTION_TIMEOUT) || 60; // s const reactionsPipe = new Channel(); @@ -108,6 +109,18 @@ ${picks} `; } +module.exports.rotateGameStatus = (client) => { + (async function rotate() { + const gamers = await api.getGamers(), + idx = Math.floor(Math.random() * (gamers.length - 1)) + 1; + if (PREVIEW) await client.user.setGame( + `?v ${gamers[idx]} | preview.vainsocial.com`); + else await client.user.setGame( + `!v ${gamers[idx]} | vainsocial.com`); + setTimeout(rotate, IGN_ROTATE_TIMEOUT * 1000); + })(); +} + // reaction -> pipe ->>> consumers // handle new reaction event module.exports.onNewReaction = (reaction) => { -- cgit v1.3.1