Skip to content

Commit

Permalink
fix: process per-shard instead of all shards at once
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno committed Oct 20, 2020
1 parent 72f9a22 commit 71753cb
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 91 deletions.
33 changes: 0 additions & 33 deletions .eslintrc.json

This file was deleted.

37 changes: 36 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,40 @@
"engines": {
"node": ">=12.16.1"
},
"snyk": true
"snyk": true,
"eslintConfig": {
"extends": "airbnb-base",
"parserOptions": {
"sourceType": "script"
},
"rules": {
"valid-jsdoc": ["error",
{
"requireReturn": false,
"requireReturnDescription": false,
"preferType": {
"String": "string",
"Number": "number",
"Boolean": "boolean",
"Function": "function",
"object": "Object",
"date": "Date",
"error": "Error"
},
"prefer": {
"return": "returns"
}
}],
"strict": ["error", "safe"],
"linebreak-style": "off",
"no-restricted-syntax": ["off"],
"no-await-in-loop": "off",
"import/no-unresolved": 0,
"no-useless-escape": 0,
"arrow-parens": [2, "as-needed", { "requireForBlockBody": true }],
"global-require": 0,
"no-param-reassign": "off",
"no-continue": "off"
}
}
}
11 changes: 2 additions & 9 deletions src/embeds/DarvoEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ const { assetBase } = require('../CommonFunctions');

const darvo = `${assetBase}/img/darvo-md.png`;

/**
* Generates daily deal embeds
*/
class DarvoEmbed extends BaseEmbed {
/**
* @param {Genesis} bot - An instance of Genesis
* @param {DailyDeal} deal - The deal to be included in the embed
* @param {string} platform - platform
*/
constructor(bot, deal, platform) {
super();

Expand All @@ -24,10 +16,11 @@ class DarvoEmbed extends BaseEmbed {
};
this.fields = [
{
name: `${deal.item}, ${deal.salePrice}p - ${deal.total - deal.sold}/${deal.total} left`,
name: `${deal.item}, ${deal.salePrice}p`,
value: `Original price: ${deal.originalPrice}p, expires in ${deal.eta}`,
},
];
this.footer.text = `${deal.total - deal.sold}/${deal.total} left`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/embeds/FrameEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FrameEmbed extends BaseEmbed {
} : false,
{
name: 'Minimum Mastery',
value: frame.masteryReq || 'N/A',
value: `${frame.masteryReq || 'N/A'} ${emojify('mastery_rank')}`,
inline: true,
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/embeds/WeaponEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WeaponEmbed extends BaseEmbed {
this.title = weapon.name;
this.url = weapon.wikiaUrl || '';
this.thumbnail = { url: weapon.wikiaThumbnail || '' };
this.description = `${weapon.type} ${`• MR ${weapon.masteryReq}`}`;
this.description = `${weapon.type} ${weapon.masteryReq} ${emojify('mastery_rank')}`;
this.color = weapon.color || 0x7C0A02;
this.fields = [];

Expand Down
72 changes: 33 additions & 39 deletions src/notifications/Broadcaster.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const logger = require('../Logger');

/**
* Broadcast updates out to subscribing channels
* @param {Discord.Client} client bot client
Expand All @@ -13,54 +15,46 @@ class Broadcaster {
}) {
this.client = client;
this.settings = settings;
this.messageManager = messageManager;
this.webhook = messageManager.webhook;
this.wrap = messageManager.webhookWrapEmbed;
this.shards = Number.parseInt(process.env.SHARDS || '1', 10);
}

/**
* Broadcast embed to all channels for a platform and type
* @param {Object} embed Embed to send to a channel
* @param {string} platform Platform of worldstate
* @param {string} type Type of new data to notify
* @param {Array} [items=[]] Items to broadcast
* @returns {Array.<Object>} values for successes
*/
* Broadcast embed to all channels for a platform and type
* @param {Object} embed Embed to send to a channel
* @param {string} platform Platform of worldstate
* @param {string} type Type of new data to notify
* @param {Array} [items=[]] Items to broadcast
* @returns {Array.<Object>} values for successes
*/
async broadcast(embed, platform, type, items = []) {
const channels = await this.settings.getNotifications(type, platform, items);
embed.bot = undefined; // eslint-disable-line no-param-reassign

delete embed.bot;
const guilds = await this.settings.getGuilds();

return Promise.all(channels.map(async (result) => {
// need some way to do this other than getting all channels....
// let's try looping over a list of ids
const ctx = await this.settings.getCommandContext(result.channelId);

if (embed.locale && ctx.language.toLowerCase() !== embed.locale.toLowerCase()) {
return false;
}

let guild;

Object.entries(guilds).forEach(([, g]) => {
if (g.channels.includes(result.channelId)) {
guild = g;
for (let shard = 0; shard < this.shards; shard += 1) {
const channels = await this.settings
.getAgnosticNotifications(type, platform, items, { shard, shards: this.shards });
for (const result of channels) {
const ctx = await this.settings.getCommandContext(result.channelId);
if (embed.locale && ctx.language.toLowerCase() !== embed.locale.toLowerCase()) {
continue;
}
});

const prepend = await this.settings.getPing(guild, (items || []).concat([type]));

if (!embed.embeds) {
return this.messageManager.webhook(
ctx,
{ text: prepend, embed: this.messageManager.webhookWrapEmbed(embed, ctx) },
);
const guild = Object.entries(guilds)
.filter(([, g]) => g.channels.includes(result.channelId))[0];
try {
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 {
await this.webhook(ctx, { text: prepend, embed });
}
} catch (e) {
logger.error(e);
}
}

return this.messageManager.webhook(
ctx,
{ text: prepend, embed },
);
}));
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/notifications/TwitchNotifier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const TwitchClient = require('twitch').default;
const { ApiClient } = require('twitch');
const WebHookListener = require('twitch-webhooks').default;

const TwitchEmbed = require('../embeds/TwitchEmbed');
Expand All @@ -26,7 +26,7 @@ class TwitchNotifier {
if (process.env.TWITCH_CLIENT_ID && process.env.TWITCH_CLIENT_SECRET) {
const id = process.env.TWITCH_CLIENT_ID;
const secret = process.env.TWITCH_CLIENT_SECRET;
this.client = TwitchClient.withClientCredentials(id, secret);
this.client = ApiClient.withClientCredentials(id, secret);
} else {
logger.debug('[Twitch] Cannot initialize Twitch Notifier... invalid credentials');
}
Expand Down
3 changes: 2 additions & 1 deletion src/resources/emoji.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"credits": "<:credits:668499068342763573>",
"ducats": "<:ducats:668499069169041420>",
"green_tick": "<:green_tick:326794577006690305>",
"red_tick":"<:red_tick:326794577426251796>"
"red_tick":"<:red_tick:326794577426251796>",
"mastery_rank": "<:mastery:768133694253826084>"
}
8 changes: 4 additions & 4 deletions src/settings/DatabaseQueries/PingsQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ class PingsQueries {
* @returns {Promise.<Array.<{channel_id: string, webhook: string}>>}
*/
async getNotifications(type, platform, items) {
if (this.scope === 'worker') {
return this.getAgnosticNotifications(type, platform, items);
}
try {
const query = SQL`SELECT DISTINCT channels.id as channelId
FROM type_notifications`
Expand All @@ -131,9 +128,11 @@ class PingsQueries {
* @param {string} type The type of the event
* @param {string} platform The platform of the event
* @param {Array.<string>} items The items in the reward that is being notified
* @param {number} shard Shard id to notify
* @param {number} shards Total number of shards
* @returns {Promise.<Array.<{channel_id: string, webhook: string}>>}
*/
async getAgnosticNotifications(type, platform, items) {
async getAgnosticNotifications(type, platform, items, { shard, shards }) {
if (this.scope !== 'worker') {
return this.getNotifications(type, platform, items);
}
Expand All @@ -146,6 +145,7 @@ class PingsQueries {
.append(SQL` INNER JOIN settings ON channels.id = settings.channel_id`)
.append(SQL`
WHERE type_notifications.type = ${String(type)}
AND MOD(IFNULL(channels.guild_id, 0) >> 22, ${shards}) = ${shard}
AND settings.setting = "platform" AND (settings.val = ${platform || 'pc'} OR settings.val IS NULL) `)
.append(items && items.length ? SQL`AND item_notifications.item IN (${items})
AND item_notifications.channel_id = settings.channel_id;` : SQL`;`);
Expand Down

0 comments on commit 71753cb

Please sign in to comment.