From e4d61c512458bd5560227dbf25f23b5f555e4703 Mon Sep 17 00:00:00 2001 From: Tobiah Date: Wed, 27 May 2020 11:49:07 -0500 Subject: [PATCH] fix: close #331, close #300, close #270 --- src/CommonFunctions.js | 2 +- src/Logger.js | 4 +-- src/commands/Ondemand/WhereIs.js | 54 +++++++++++++++++++++++++------- src/commands/Tracking/Track.js | 8 +++-- src/embeds/WhereisEmbed.js | 8 +++-- src/notifications/Notifier.js | 2 +- 6 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/CommonFunctions.js b/src/CommonFunctions.js index 7a302f79e..916322732 100644 --- a/src/CommonFunctions.js +++ b/src/CommonFunctions.js @@ -785,7 +785,7 @@ const getChannel = (channelsParam, message, channels) => { let { channel } = message; let channelsColl; if (message.guild) { - channelsColl = message.guild.channels; + channelsColl = message.guild.channels.cache; } else { channelsColl = new Collection(); channelsColl.set(message.channel.id, message.channel); diff --git a/src/Logger.js b/src/Logger.js index ee70604bc..b263ea9f9 100644 --- a/src/Logger.js +++ b/src/Logger.js @@ -13,8 +13,8 @@ const scope = (process.env.SCOPE || 'worker').toUpperCase(); const ErrorEmbed = require('./embeds/ErrorEmbed'); let errorHook; -if (scope === 'worker' && process.env.CONTROL_WH_ID) { - errorHook = new WebhookClient(process.env.CONTROL_WH_ID, process.env.CONTROL_WH_TOKEN); +if (process.env.CONTROL_WH_ID) { + errorHook = new WebhookClient(process.env.CONTROL_WH_ID, process.env.CONTROL_WH_TOKEN); } /** diff --git a/src/commands/Ondemand/WhereIs.js b/src/commands/Ondemand/WhereIs.js index 166b5edd5..8b54298ab 100644 --- a/src/commands/Ondemand/WhereIs.js +++ b/src/commands/Ondemand/WhereIs.js @@ -43,18 +43,50 @@ class Whereis extends Command { try { query = query.trim().toLowerCase(); const queryWReplaces = query.replace(/prime/ig, 'p.').replace(/blueprint/ig, 'bp'); - let results = await this.ws.search('drops', queryWReplaces); + const queryResults = (await this.ws.search('drops', queryWReplaces)) + .map((result) => { + const r = { + item: result.item, + rarity: result.rarity, + chance: `${String(parseFloat(result.chance).toFixed(2)).padEnd(5, '0')}%`, + chanceNum: parseFloat(result.chance).toFixed(2), + place: result.place + .replace('Level ', '') + .replace(' Orb Vallis Bounty', '') + .replace(' Cetus Bounty', '') + .trim(), + }; + r.place = r.place.split('/')[1] || r.place; + return r; + }); - results = results.map(result => ({ - item: result.item, - rarity: result.rarity, - chance: `${String(parseFloat(result.chance).toFixed(2)).padEnd(5, '0')}%`, - place: result.place - .replace('Level ', '') - .replace('Orb Vallis Bounty', 'Bounty') - .replace('Cetus Bounty', 'Bounty') - .trim(), - })); + let results = []; + + const map = new Map(); + for (const item of queryResults) { + const isRelic = item.place.includes('Relic'); + const relic = item.place.split('(')[0].trim(); + if (isRelic && (!map.has(relic) || map.get(relic) < item.chanceNum)) { + if (map.has(relic)) { + let indexToRemove; + results.forEach((urelic, index) => { + if (urelic.place.includes(relic)) { + indexToRemove = index; + } + }); + if (typeof indexToRemove !== 'undefined') { + results.splice(indexToRemove, 1); + } + } + map.set(relic, item.chanceNum); + results.push(item); + } else if (!isRelic && (!map.has(item.place) || map.get(item.place) < item.chanceNum)) { + map.set(item.place, item.chanceNum); + results.push(item); + } + } + + results = [...(new Set(results))]; const longestName = results.length ? results.map(result => result.item) .reduce((a, b) => (a.length > b.length ? a : b)) : ''; diff --git a/src/commands/Tracking/Track.js b/src/commands/Tracking/Track.js index 271fd3c8c..0ccc6091d 100644 --- a/src/commands/Tracking/Track.js +++ b/src/commands/Tracking/Track.js @@ -28,10 +28,11 @@ class Track extends Command { } async run(message, ctx) { + this.logger.info('running track....'); const unsplitItems = getEventsOrItems(message); const roomId = new RegExp(`${captures.channel}|here`, 'ig'); - if (unsplitItems.length === 0) { + if (!unsplitItems.length) { return this.failure(message, ctx.prefix); } const trackables = trackablesFromParameters(unsplitItems); @@ -43,7 +44,10 @@ class Track extends Command { trackables.items = trackables.items .filter((elem, pos) => trackables.items.indexOf(elem) === pos); - const channelParam = message.strippedContent.match(roomId) ? message.strippedContent.match(roomId)[0].trim().replace(/<|>|#/ig, '') : undefined; + const channelParam = message.strippedContent.match(roomId) + ? message.strippedContent.match(roomId)[0].trim().replace(/<|>|#/ig, '') + : undefined; + const channel = getChannel(channelParam, message); if (trackables.events.length) { diff --git a/src/embeds/WhereisEmbed.js b/src/embeds/WhereisEmbed.js index ac1a32550..16d7cf7f2 100644 --- a/src/embeds/WhereisEmbed.js +++ b/src/embeds/WhereisEmbed.js @@ -18,8 +18,12 @@ class WhereisEmbed extends BaseEmbed { this.fields = []; resultsGroups.forEach((results, index) => { - const mappedResults = results.map(result => `\`${result.item.padEnd(nameWidth, '\u2003')} ` - + `| ${result.place.padEnd(relicWidth, '\u2003')} | ${result.rarity.charAt(0)}@${result.chance}\``); + const mappedResults = results.map((result) => { + const item = result.item.padEnd(nameWidth, '\u2003'); + const place = (result.place.split('/')[1] || result.place).padEnd(relicWidth, '\u2003'); + const chance = `${result.rarity.charAt(0)}@${result.chance}`; + return `\`${item} | ${place} | ${chance}\``; + }); const value = mappedResults.join('\n'); if (index > 0) { diff --git a/src/notifications/Notifier.js b/src/notifications/Notifier.js index 8fcf78112..0c9e6fc37 100644 --- a/src/notifications/Notifier.js +++ b/src/notifications/Notifier.js @@ -281,7 +281,7 @@ class Notifier { ...rawData.weeklyChallenges.map(w => w.id), rawData.arbitration && rawData.arbitration.enemy ? `arbitration:${new Date(rawData.arbitration.expiry).getTime()}` - : `arbitration:0`, + : 'arbitration:0', ...rawData.twitter.map(t => t.id), ...(rawData.nightwave.active ? rawData.nightwave.activeChallenges.map(c => c.id) : []), );