Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async fixes #126

Merged
merged 11 commits into from
Sep 21, 2017
8 changes: 3 additions & 5 deletions src/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

/**
* A collection of methods for logging
* @typedef {Object.<function>} Logger
* @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
* an error
*/
class Logger {
/**
Expand All @@ -28,9 +27,8 @@ const levels = [
];

levels.forEach((level) => {
// eslint-disable-next-line func-names
Logger.prototype[level.toLowerCase()] = function (message) {
if (levels.indexOf(level) >= levels.indexOf(logLevel)) {
Logger.prototype[level.toLowerCase()] = (message) => {
if ((levels.indexOf(level) >= levels.indexOf(logLevel)) && levels.indexOf(level) < 3) {
// eslint-disable-next-line no-console
console.log(`[${level}] ${message}`);
}
Expand Down
8 changes: 3 additions & 5 deletions src/embeds/ConstructionEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ class ConstructionEmbed extends BaseEmbed {
this.color = 0xff6961;
this.fields = [{
name: `[${platform.toUpperCase()}] Construction Status:`,
value: '```' +
`Razorback: ${constructionProgress.razorbackProgress}` +
`Fomorian: ${constructionProgress.fomorianProgress}` +
`Unknown: ${constructionProgress.unknwonProgress}` +
'```',
value: `\`Razorback: ${constructionProgress.razorbackProgress}\`\n` +
`\`Fomorian: ${constructionProgress.fomorianProgress}\`\n` +
`\`Unknown: ${constructionProgress.unknownProgress}\``,
}];
}
}
Expand Down
128 changes: 31 additions & 97 deletions src/notifications/Notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ function fromNow(d, now = Date.now) {
return new Date(d).getTime() - now();
}

async function getThumbnailForItem(query) {
const articles = await warframe.getSearchList({ query, limit: 1 });
const details = await warframe.getArticleDetails({ ids: articles.items.map(i => i.id) });
const item = Object.values(details.items)[0];
return item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
}

/**
* Notifier for alerts, invasions, etc.
*/
Expand All @@ -46,7 +53,10 @@ class Notifier {
*/
async start() {
for (const k of Object.keys(this.bot.worldStates)) {
this.bot.worldStates[k].on('newData', (platform, newData) => this.onNewData(platform, newData));
this.bot.worldStates[k].on('newData', async (platform, newData) => {
this.logger.debug(`Processing new data for ${platform}`);
await this.onNewData(platform, newData);
});
}
}

Expand Down Expand Up @@ -156,7 +166,7 @@ class Notifier {
// eslint-disable-next-line no-await-in-loop
const prepend = await this.settings
.getPing(channel.guild, (items || []).concat([type]));
this.bot.messageManager.embedToChannel(channel, embed, prepend, deleteAfter);
await this.bot.messageManager.embedToChannel(channel, embed, prepend, deleteAfter);
} else if (channel.type === 'dm') {
// eslint-disable-next-line no-await-in-loop
await this.bot.messageManager.embedToChannel(channel, embed, '', deleteAfter);
Expand All @@ -181,36 +191,25 @@ class Notifier {
* @param {string} platform platform corresponding to notified ids
*/
async updateNotified(ids, platform) {
await this.settings.setNotifiedIds(platform, this.bot.shardId, ids, platform);
await this.settings.setNotifiedIds(platform, this.bot.shardId, ids);
}

async sendAcolytes(newAcolytes, platform) {
const results = [];
for (const a of newAcolytes) {
const embed = new EnemyEmbed(this.bot, [a], platform);
results.push(this.broadcast(embed, platform, 'enemies', null, 3600000));
}
Promise.all(results);
await Promise.all(newAcolytes.map(a => this.broadcast(new EnemyEmbed(this.bot,
[a], platform), platform, 'enemies', null, 3600000)));
}

async sendAlerts(newAlerts, platform) {
const results = [];
for (const a of newAlerts) {
results.push(this.sendAlert(a, platform));
if (newAlerts.length) {
this.logger.debug(`New Alerts! ${newAlerts.length}`);
}
Promise.all(results);
await Promise.all(newAlerts.map(a => this.sendAlert(a, platform)));
}

async sendAlert(a, platform) {
const embed = new AlertEmbed(this.bot, [a], platform);
try {
const articles = await warframe.getSearchList({
query: a.mission.reward.itemString,
limit: 1,
});
const details = await warframe.getArticleDetails({ ids: articles.items.map(i => i.id) });
const item = Object.values(details.items)[0];
const thumb = item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
const thumb = await getThumbnailForItem(a.mission.reward.itemString);
if (thumb && !a.rewardTypes.includes('reactor') && !a.rewardTypes.includes('catalyst')) {
embed.thumbnail.url = thumb;
}
Expand Down Expand Up @@ -241,61 +240,29 @@ class Notifier {
}

async sendDarvo(newDarvoDeals, platform) {
const results = [];
for (const d of newDarvoDeals) {
const embed = new DarvoEmbed(this.bot, d, platform);
results.push(this.broadcast(embed, platform, 'darvo', null, fromNow(d.expiry)));
}
Promise.all(results);
await Promise.all(newDarvoDeals.map(d => this.broadcast(new DarvoEmbed(this.bot, d, platform), platform, 'darvo', null, fromNow(d.expiry))));
}

async sendEvent(newEvents, platform) {
const results = [];
for (const e of newEvents) {
const embed = new EventEmbed(this.bot, [e], platform);
results.push(this.broadcast(embed, platform, 'events', null, fromNow(e.expiry)));
}
Promise.all(results);
await Promise.all(newEvents.map(e => this.broadcast(new EventEmbed(this.bot, [e], platform), platform, 'events', null, fromNow(e.expiry))));
}

async sendFeaturedDeals(newFeaturedDeals, platform) {
const results = [];
for (const d of newFeaturedDeals) {
const embed = new SalesEmbed(this.bot, [d], platform);
results.push(this.broadcast(embed, platform, 'deals.featured', null, fromNow(d.expiry)));
}
Promise.all(results);
await Promise.all(newFeaturedDeals.map(d => this.broadcast(new SalesEmbed(this.bot, [d], platform), platform, 'deals.featured', null, fromNow(d.expiry))));
}

async sendFissures(newFissures, platform) {
const results = [];
for (const f of newFissures) {
const embed = new FissureEmbed(this.bot, [f], platform);
results.push(this.broadcast(embed, platform, `fissures.t${f.tierNum}`, null, fromNow(f.expiry)));
}
Promise.all(results);
await Promise.all(newFissures.map(f => this.broadcast(new FissureEmbed(this.bot, [f], platform), platform, `fissures.t${f.tierNum}`, null, fromNow(f.expiry))));
}

async sendInvasions(newInvasions, platform) {
const results = [];
for (const invasion of newInvasions) {
results.push(this.sendInvasion(invasion, platform));
}
Promise.all(results);
await Promise.all(newInvasions.map(invasion => this.sendInvasion(invasion, platform)));
}

async sendInvasion(invasion, platform) {
const embed = new InvasionEmbed(this.bot, [invasion], platform);
try {
const articles = await warframe.getSearchList({
query: invasion.attackerReward.itemString,
limit: 1,
});
const details = await warframe.getArticleDetails({
ids: articles.items.map(i => i.id),
});
const item = Object.values(details.items)[0];
const thumb = item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
const thumb = await getThumbnailForItem(invasion.attackerReward.itemString);
if (thumb && !invasion.rewardTypes.includes('reactor') && !invasion.rewardTypes.includes('catalyst')) {
embed.thumbnail.url = thumb;
}
Expand All @@ -307,62 +274,29 @@ class Notifier {
}

async sendNews(newNews, platform) {
const results = [];
for (const i of newNews) {
const embed = new NewsEmbed(this.bot, [i], undefined, platform);
results.push(this.broadcast(embed, platform, 'news'));
}
Promise.all(results);
await Promise.all(newNews.map(i => this.broadcast(new NewsEmbed(this.bot, [i], undefined, platform), platform, 'news')));
}

async sendStreams(newStreams, platform) {
const results = [];
for (const i of newStreams) {
const embed = new NewsEmbed(this.bot, [i], undefined, platform);
results.push(this.broadcast(embed, platform, 'streams'));
}
Promise.all(results);
await Promise.all(newStreams.map(i => this.broadcast(new NewsEmbed(this.bot, [i], undefined, platform), platform, 'streams')));
}

async sendPopularDeals(newPopularDeals, platform) {
const results = [];
for (const d of newPopularDeals) {
const embed = new SalesEmbed(this.bot, [d], platform);
results.push(this.broadcast(embed, platform, 'deals.popular', null, 86400000));
}
Promise.all(results);
await Promise.all(newPopularDeals.map(d => this.broadcast(new SalesEmbed(this.bot, [d], platform), platform, 'deals.popular', null, 86400000)));
}

async sendPrimeAccess(newNews, platform) {
const results = [];
for (const i of newNews) {
const embed = new NewsEmbed(this.bot, [i], 'primeaccess', platform);
results.push(this.broadcast(embed, platform, 'primeaccess', null));
}
Promise.all(results);
await Promise.all(newNews.map(i => this.broadcast(new NewsEmbed(this.bot, [i], 'primeaccess', platform), platform, 'primeaccess')));
}

async sendUpdates(newNews, platform) {
const results = [];
for (const i of newNews) {
const embed = new NewsEmbed(this.bot, [i], 'updates', platform);
results.push(this.broadcast(embed, platform, 'updates', null));
}
Promise.all(results);
await Promise.all(newNews.map(i => this.broadcast(new NewsEmbed(this.bot, [i], 'updates', platform), platform, 'updates')));
}

async sendSortie(newSortie, platform) {
const embed = new SortieEmbed(this.bot, newSortie, platform);
try {
const articles = await warframe.getSearchList({
query: newSortie.boss,
limit: 1,
});
const details = await warframe.getArticleDetails({
ids: articles.items.map(i => i.id),
});
const item = Object.values(details.items)[0];
const thumb = item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
const thumb = await getThumbnailForItem(newSortie.boss);
if (thumb) {
embed.thumbnail.url = thumb;
}
Expand Down
16 changes: 8 additions & 8 deletions src/settings/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Database {
return Promise.mapSeries(schema, q => this.db.query(q));
}

/**
/**
* Initialize data for guilds in channels for existing guilds
* @param {Client} client for pulling guild information
*/
Expand Down Expand Up @@ -315,7 +315,7 @@ class Database {
return [];
}
return res[0]
.map(result => ({ text: result.text, thing: result.item_or_type }));
.map(result => ({ text: result.text, thing: result.item_or_type }));
}
return [];
}
Expand Down Expand Up @@ -345,15 +345,15 @@ class Database {
try {
const query = SQL`SELECT DISTINCT channels.id as channelId
FROM type_notifications`
.append(items && items.length > 0 ?
SQL` INNER JOIN item_notifications ON type_notifications.channel_id = item_notifications.channel_id` : SQL``)
.append(SQL` INNER JOIN channels ON channels.id = type_notifications.channel_id`)
.append(SQL` INNER JOIN settings ON channels.id = settings.channel_id`)
.append(SQL`
.append(items && items.length > 0 ?
SQL` INNER JOIN item_notifications ON type_notifications.channel_id = item_notifications.channel_id` : SQL``)
.append(SQL` INNER JOIN channels ON channels.id = type_notifications.channel_id`)
.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, ${this.bot.shardCount}) = ${this.bot.shardId}
AND settings.setting = "platform" AND settings.val = ${platform || 'pc'} `)
.append(items && items.length > 0 ? SQL`AND item_notifications.item IN (${items})
.append(items && items.length > 0 ? SQL`AND item_notifications.item IN (${items})
AND item_notifications.channel_id = settings.channel_id;` : SQL`;`);
return this.db.query(query);
} catch (e) {
Expand Down