Skip to content

Commit

Permalink
feature flag twitch & rss notifiers (since they're unstable)
Browse files Browse the repository at this point in the history
ping queries try/catch around odd guild structs
  • Loading branch information
TobiTenno committed Feb 10, 2021
1 parent a4acf0d commit 83e2c97
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 48 deletions.
31 changes: 17 additions & 14 deletions src/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ if (process.env.CONTROL_WH_ID) {
errorHook = new WebhookClient(process.env.CONTROL_WH_ID, process.env.CONTROL_WH_TOKEN);
}

/**
* A collection of methods for logging
* @property {function} silly - silly level of debugging
* @property {function} debug - Logs a debug message
* @property {function} info - Logs an info message
* @property {function} warning - Logs a warning message
* @property {function} error - Logs an error message
* @property {function} fatal - Logs a fatal message. The program should terminate after such
* an error
*/
class Logger {}

const l = {
get logLevel() {
return process.env.LOG_LEVEL || 'ERROR';
Expand All @@ -47,15 +35,30 @@ const scopes = {
WORKER: 'grey',
};

/**
* A collection of methods for logging
* @property {function} silly - silly level of debugging
* @property {function} debug - Logs a debug message
* @property {function} info - Logs an info message
* @property {function} warning - Logs a warning message
* @property {function} error - Logs an error message
* @property {function} fatal - Logs a fatal message. The program should terminate after such
* an error
*/
class Logger {
isLoggable(level) {
return Object.keys(levels).indexOf(level.toUpperCase()) >= Object.keys(levels).indexOf(l.logLevel)
}
}

const colorify = (level, map) => level[map[level] || 'red'];
const fmt = (level, msg) => `[${colorify(scope, scopes)}] ${(colorify(level, levels) || 'ukn').toLowerCase()}: ${msg}`;

Object.keys(levels).forEach((level) => {
Logger.prototype[level.toLowerCase()] = (message) => {
const simple = fmt(level, message);
const isActive = Object.keys(levels).indexOf(level) >= Object.keys(levels).indexOf(l.logLevel);
const nonError = Object.keys(levels).indexOf(level) < Object.keys(levels).indexOf('ERROR');
if (isActive && nonError) {
if (this.isLoggable(level) && nonError) {
console.log(simple);
}

Expand Down
1 change: 1 addition & 0 deletions src/commands/Tracking/Track.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Track extends Command {
}
this.bot.settings.setChannelWebhook(message.channel, webhook);
await message.channel.send(`${emojify('green_tick')} Webhook setup complete.`);
await webhook.send(':diamond_shape_with_a_dot_inside: Webhook initialized');
} catch (e) {
await message.channel.send(`${emojify('red_tick')} Cannot set up webhooks: failed to look up.`);
}
Expand Down
7 changes: 5 additions & 2 deletions src/notifications/Broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ class Broadcaster {
.filter(([, g]) => g.channels && g.channels.includes(channelId))[0];
const guild = glist && glist.length ? glist[1] : null;

if (!guild) continue;
if (!guild) {
logger.info(`couldn't find guild for ${type} on ${channelId}`);
continue;
}

try {
const prepend = await this.settings.getPing(guild, (items || []).concat([type]));
const prepend = ''; // await this.settings.getPing(guild, (items || []).concat([type]));
if (!embed.embeds) {
await this.webhook(ctx, { text: prepend, embed: this.wrap(embed, ctx) });
} else {
Expand Down
16 changes: 1 addition & 15 deletions src/notifications/Notifier.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
'use strict';

/* eslint-disable no-unused-vars */

const Wikia = require('node-wikia');
const util = require('util');

const exists = util.promisify(require('url-exists'));
const fetch = require('../resources/Fetcher');
const { embeds } = require('./NotifierUtils');
const Broadcaster = require('./Broadcaster');
Expand All @@ -15,8 +10,6 @@ const {
createGroupedArray, apiBase, apiCdnBase, platforms, captures,
} = require('../CommonFunctions');

const warframe = new Wikia('warframe');

const syndicates = require('../resources/syndicates.json');
const I18n = require('../settings/I18n');

Expand Down Expand Up @@ -51,14 +44,6 @@ async function getThumbnailForItem(query, fWiki) {
return url;
}
}
// try {
// const articles = await warframe.getSearchList({ query: fq, limit: 1 });
// const details = await warframe.getArticleDetails({ ids: articles.items.map(i => i.id) });
// const item = Object.values(details.items)[0];
// return item && item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
// } catch (e) {
// logger.error(e);
// }
}
return '';
}
Expand Down Expand Up @@ -290,6 +275,7 @@ class Notifier {
];

await this.settings.setNotifiedIds(platform, alreadyNotified);
logger.silly(`completed sending notifications for ${platform}`);
}

async sendAcolytes(newAcolytes, platform) {
Expand Down
1 change: 1 addition & 0 deletions src/notifications/NotifierUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ const logger = require('../Logger');
module.exports = {
embeds,
logger,
platforms: process.env.PLATFORMS,
};
33 changes: 22 additions & 11 deletions src/notifications/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const MessageManager = require('../settings/MessageManager');
const Rest = require('../tools/RESTWrapper');
const Database = require('../settings/Database');

const { logger } = require('./NotifierUtils');
const { logger, platforms } = require('./NotifierUtils');

const { emojify, games } = require('../CommonFunctions');

const cachedEvents = require('../resources/cachedEvents');

const activePlatforms = (process.env.PLATFORMS || 'pc').split(',');
Expand Down Expand Up @@ -103,19 +106,27 @@ class Worker {
deps.messageManager = this.messageManager;

this.notifier = new Notifier(deps);
this.feedNotifier = new FeedsNotifier(deps);
this.twitchNotifier = new TwitchNotifier(deps);

this.feedNotifier.start();
this.twitchNotifier.start();
if (games.includes("RSS")) {
this.feedNotifier = new FeedsNotifier(deps);
this.feedNotifier.start();
}

if (games.includes("TWITCH")) {
this.twitchNotifier = new TwitchNotifier(deps);
this.twitchNotifier.start();
}

await this.notifier.start();

rest.controlMessage({
embeds: [{
description: 'Worker ready!',
color: 0x2B90EC,
}],
});
if (logger.isLoggable('debug')) {
rest.controlMessage({
embeds: [{
description: `Worker ready on ${platforms}`,
color: 0x2B90EC,
}],
});
}
} catch (e) {
logger.error(e);
}
Expand Down
6 changes: 5 additions & 1 deletion src/resources/emoji.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"red_tick":"<:red_tick:326794577426251796>",
"mastery_rank": "<:mastery:768133694253826084>",
"green_tick_id": "326794577006690305",
"red_tick_id": "326794577426251796"
"red_tick_id": "326794577426251796",
"pc": "<:pc:428183152490577928>",
"ps4": "<:ps4:428183152448503808>",
"swi": "<:switch:471082770974900244>",
"xb1": "<:xb1:428183151467298817>"
}
16 changes: 11 additions & 5 deletions src/settings/DatabaseQueries/PingsQueries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const SQL = require('sql-template-strings');
const logger = require('../../Logger');

class PingsQueries {
constructor(db) {
Expand Down Expand Up @@ -66,11 +67,16 @@ class PingsQueries {
if (!guild.id) {
guild = { id: guild };
}
const query = SQL`SELECT text FROM pings WHERE guild_id=${guild.id} AND item_or_type in (${itemsOrTypes})`;
const res = await this.query(query);
if (!res[0].length) return '';
return res[0]
.map(result => result.text).join(', ');
try {
const query = SQL`SELECT text FROM pings WHERE guild_id=${guild.id} AND item_or_type in (${itemsOrTypes})`;
const res = await this.query(query);
if (!res[0].length) return '';
return res[0]
.map(result => result.text).join(', ');
} catch (e) {
logger.error(e);
return '';
}
}

async getPingsForGuild(guild) {
Expand Down

0 comments on commit 83e2c97

Please sign in to comment.