From 7f31751c9183244774715c39d23d2b5f8305e47d Mon Sep 17 00:00:00 2001 From: Nightlife Date: Sat, 12 Jan 2019 03:52:35 +0800 Subject: [PATCH] hopefully probably fix some things --- Atlas.js | 14 ++++-- Dockerfile | 2 +- package.json | 3 +- src/autoscale.js | 16 +++--- src/events/messageCreate.js | 59 ++++++++++++++++------ src/structures/Command.js | 2 - src/structures/Database.js | 6 ++- src/structures/Settings.js | 8 +-- src/tagengine/interpreter.js | 6 +-- yarn.lock | 98 +++++++++++++++++++----------------- 10 files changed, 128 insertions(+), 86 deletions(-) diff --git a/Atlas.js b/Atlas.js index af4e0739..08b1e8e3 100644 --- a/Atlas.js +++ b/Atlas.js @@ -120,11 +120,17 @@ module.exports = class Atlas { captureUnhandledRejections: true, stacktrace: true, autoBreadcrumbs: { http: true }, - beforeSend: (event) => { - console.error(event.originalException); + }); - return event; - }, + process.on('unhandledRejection', (reason) => { + throw reason; + }); + + process.on('uncaughtException', async (err) => { + Sentry.captureException(err); + + console.error(err); + // process.exit(0); }); const events = await fs.readdir('src/events'); diff --git a/Dockerfile b/Dockerfile index 29a7a30a..1678514f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node +FROM node:10 # Create app directory WORKDIR /usr/src/app diff --git a/package.json b/package.json index c55ed7a3..e6e5b990 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "start": "node index.js", "dev": "nodemon index.js", - "test": "eslint . --fix" + "test": "eslint . --fix", + "debug": "node --inspect=0.0.0.0 index.js" }, "engines": { "node": ">=10.0.0" diff --git a/src/autoscale.js b/src/autoscale.js index 4b8bef9f..b043049b 100644 --- a/src/autoscale.js +++ b/src/autoscale.js @@ -1,9 +1,9 @@ // at the itme of writing this i just want to play with kubernete :D // from https://github.com/TheSharks/WildBeast/blob/df03e9402d7690cacc9735def80eff2de5263b77/src/internal/k8s-autoscale.js const os = require('os'); -const superagent = require('superagent'); +// const superagent = require('superagent'); -const prettyMs = require('atlas-lib/lib/utils/prettyMs'); +// const prettyMs = require('atlas-lib/lib/utils/prettyMs'); module.exports = async () => { if (process.env.AUTOSCALE !== 'true') { @@ -14,12 +14,12 @@ module.exports = async () => { } // FIXME: this could be inaccurate if the values change while we're starting - const { body: { shards, session_start_limit: startLimit } } = await superagent.get('https://discordapp.com/api/gateway/bot') - .set('Authorization', `Bot ${process.env.TOKEN}`); + // const { body: { shards, session_start_limit: startLimit } } = await superagent.get('https://discordapp.com/api/gateway/bot') + // .set('Authorization', `Bot ${process.env.TOKEN}`); - if (startLimit.remaining < 1) { - console.error(`Reached start limit, resets in ${prettyMs(startLimit.reset_after)}`); - } + // if (startLimit.remaining < 1) { + // console.error(`Reached start limit, resets in ${prettyMs(startLimit.reset_after)}`); + // } // each pod's hostname matches a known pattern like web-0 or web-1 // https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id @@ -34,7 +34,7 @@ module.exports = async () => { const index = Number(match[1]); return { - total: shards, + total: Number(process.env.SHARDS_TOTAL), mine: index, }; }; diff --git a/src/events/messageCreate.js b/src/events/messageCreate.js index 12bfe849..727e2fe9 100644 --- a/src/events/messageCreate.js +++ b/src/events/messageCreate.js @@ -1,5 +1,7 @@ const Cache = require('atlas-lib/lib/structures/Cache'); +const Action = require('../structures/Action'); + const ratelimits = new Cache('ratelimits'); const prefixes = process.env.PREFIXES.split(','); @@ -45,26 +47,47 @@ module.exports = class Ready { // try and find an action, if one exists it'll run it then do nothing. if (msg.guild && settings) { - const actions = await settings.findActions(msg); - - if (actions.length) { - for (const action of actions) { - try { - await action.execute(msg); - if (actions.length !== 1) { - // sleep for 1s to prevent abuse - await this.Atlas.lib.utils.sleep(1000); - } - } catch (e) { - this.Atlas.Sentry.captureException(e); - } + const actions = await this.Atlas.DB.Action.find({ + guild: msg.guild.id, + $or: [{ + 'trigger.type': 'messageCreate', + $or: [{ + 'trigger.content': msg.channel.id, + }, { + 'trigger.content': null, + }, { + 'trigger.content': undefined, + }], + }, { + 'trigger.type': 'label', + 'trigger.content': msg.label, + }, { + 'trigger.type': 'keyword', + }], + }); + + for (const rawAction of actions) { + if (rawAction.trigger.type === 'keyword' && !msg.content.toLowerCase().includes(rawAction.trigger.content.toLowerCase())) { + continue; } - // if an "custom command" was called, don't do anything. - if (actions.find(a => a.trigger.type === 'label' && a.trigger.content === msg.label)) { - return; + const action = new Action(settings, rawAction); + + try { + await action.execute(msg); + if (actions.length !== 1) { + // sleep for 1s to prevent abuse + await this.Atlas.lib.utils.sleep(1000); + } + } catch (e) { + this.Atlas.Sentry.captureException(e); } } + + // if an "custom command" was called, don't do anything. + if (actions.find(a => a.trigger.type === 'label' && a.trigger.content === msg.label)) { + return; + } } if (msg.prefix) { // eslint-disable-line no-extra-parens @@ -118,6 +141,10 @@ module.exports = class Ready { } } + if (process.env.VERBOSE === 'true') { + console.log(`executing ${msg.command.info.name}`); + } + return msg.command.execute(msg, msg.args, { settings, }); diff --git a/src/structures/Command.js b/src/structures/Command.js index 6b69a626..05a74aaa 100644 --- a/src/structures/Command.js +++ b/src/structures/Command.js @@ -60,7 +60,6 @@ class Command { // parse --args="arg" - const parsedArgs = {}; const parsed = parseArgs(msg.content, { configuration: { @@ -138,7 +137,6 @@ class Command { msg.options = options; } - // run the command const out = await this.action(msg, args, { settings, diff --git a/src/structures/Database.js b/src/structures/Database.js index d0dd8a5f..3bf8c664 100644 --- a/src/structures/Database.js +++ b/src/structures/Database.js @@ -2,6 +2,8 @@ const mongoose = require('mongoose'); const Settings = require('./Settings'); +/* eslint-disable func-names */ + module.exports = class Database { constructor() { const SettingsSchema = require('atlas-lib/lib/models/Settings'); @@ -22,7 +24,7 @@ module.exports = class Database { } async settings(guild) { - let settings = await mongoose.model('Settings').findOne({ id: guild.id || guild }); + let settings = await this.Settings.findOne({ id: guild.id || guild }); if (!settings) { const data = typeof guild === 'string' ? { id: guild } : guild; @@ -34,7 +36,7 @@ module.exports = class Database { } async user(user) { - let profile = await mongoose.model('User').findOne({ id: user.id || user }); + let profile = await this.User.findOne({ id: user.id || user }); if (!profile) { const data = typeof user === 'string' ? { id: user } : user; diff --git a/src/structures/Settings.js b/src/structures/Settings.js index 11144f0c..9020f791 100644 --- a/src/structures/Settings.js +++ b/src/structures/Settings.js @@ -407,11 +407,11 @@ module.exports = class GuildSettings { } async getTriggers() { - return (await this.Atlas.DB.Action.find({ + return this.Atlas.DB.Action.find({ guild: this.id, }, { trigger: 1, - })).map(o => o.toObject()); + }); } async getAction(id) { @@ -429,7 +429,9 @@ module.exports = class GuildSettings { } async findActions(msg) { - const triggers = (await this.getTriggers()).filter(({ trigger }) => { + const actions = await this.getTriggers(); + + const triggers = actions.filter(({ trigger }) => { if (trigger.type === 'messageCreate') { if (!trigger.content) { return true; diff --git a/src/tagengine/interpreter.js b/src/tagengine/interpreter.js index 5f9bc925..d26c9ca7 100644 --- a/src/tagengine/interpreter.js +++ b/src/tagengine/interpreter.js @@ -54,7 +54,7 @@ const interp = async (tokens, context, functions) => { } try { - let textArgs = null; + let textArgs = []; if (func.info.dontParse) { textArgs = args.map((a) => { // don't worry about it ;) @@ -94,9 +94,9 @@ const interp = async (tokens, context, functions) => { console.warn(e); } - output.push(`{${thisToken.value}-ERROR${errors.length}-${e.message.split(' ').join('-').toLowerCase().replace(/[^A-z-]/g, '')}}`); - Atlas.Sentry.captureException(e); + + output.push(`{${thisToken.value}-ERROR${errors.length}-${e.message.split(' ').join('-').toLowerCase().replace(/[^A-z-]/g, '')}}`); } // if it ran into an error or was successful it would have been managed diff --git a/yarn.lock b/yarn.lock index 10e769d5..0f234eba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -278,62 +278,63 @@ dependencies: core-js "^2.5.7" -"@sentry/core@4.4.2": - version "4.4.2" - resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.4.2.tgz#562526bc634c087f04bbca68b09cedc4b41cc64d" - integrity sha512-hJyAodTCf4sZfVdf41Rtuzj4EsyzYq5rdMZ+zc2Vinwdf8D0/brHe91fHeO0CKXEb2P0wJsrjwMidG/ccq/M8A== - dependencies: - "@sentry/hub" "4.4.2" - "@sentry/minimal" "4.4.2" - "@sentry/types" "4.4.2" - "@sentry/utils" "4.4.2" +"@sentry/core@4.5.0": + version "4.5.0" + resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.5.0.tgz#0dfb82ea6421681916ce73e6f833de17ae86ac86" + integrity sha512-+uzJelvAYQTd60aJ2y8PovqP+CmLFoX3a30yh2Qt9fXomwqYupaAPE+kYtOJMYuXFpVMUTxCxxNPmaC/bwUUIg== + dependencies: + "@sentry/hub" "4.5.0" + "@sentry/minimal" "4.5.0" + "@sentry/types" "4.5.0" + "@sentry/utils" "4.5.0" tslib "^1.9.3" -"@sentry/hub@4.4.2": - version "4.4.2" - resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.4.2.tgz#1399556fda06fb83c4f186c4aa842725f520159c" - integrity sha512-oe9ytXkTWyD+QmOpVzHAqTbRV4Hc0ee2Nt6HvrDtRmlXzQxfvTWG2F8KYT6w8kzqg5klnuRpnsmgTTV3KuNBVQ== +"@sentry/hub@4.5.0": + version "4.5.0" + resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.5.0.tgz#f61da6fa33d8b5a90c4324eda220f720d064fbf0" + integrity sha512-z9K4YYkMR5x7SdVTGVsWAdWh/PdSJyavar9Nk3I674RRjXS6IewFw7A+cgYDWTuMjB5MslbwoH2S9Pc+xRPKdg== dependencies: - "@sentry/types" "4.4.2" - "@sentry/utils" "4.4.2" + "@sentry/types" "4.5.0" + "@sentry/utils" "4.5.0" tslib "^1.9.3" -"@sentry/minimal@4.4.2": - version "4.4.2" - resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.4.2.tgz#13fffc6b17a2401b6a79947838a637626ab80b10" - integrity sha512-GEZZiNvVgqFAESZhAe3vjwTInn13lI2bSI3ItQN4RUWKL/W4n/fwVoDJbkb1U8aWxanuMnRDEpKwyQv6zYTZfw== +"@sentry/minimal@4.5.0": + version "4.5.0" + resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.5.0.tgz#5f4dd50772920af5a44d52a8f1c86e7b37172913" + integrity sha512-nH6kL7+I7cL/t7GEK3nv60e2PDg1DGZtakQ1GuqloP19phPj+6BsxjT+Njt1KLcSPsQPWwwK0C6rm0NsamPnpQ== dependencies: - "@sentry/hub" "4.4.2" - "@sentry/types" "4.4.2" + "@sentry/hub" "4.5.0" + "@sentry/types" "4.5.0" tslib "^1.9.3" "@sentry/node@^4.4.2": - version "4.4.2" - resolved "https://registry.yarnpkg.com/@sentry/node/-/node-4.4.2.tgz#549921d2df3cbf58ebcfb525c3005c3fec4739a3" - integrity sha512-8/KlSdfVhledZ6PS6muxZY5r2pqhw8MNSXP7AODR2qRrHwsbnirVgV21WIAYAjKXEfYQGbm69lyoaTJGazlQ3Q== - dependencies: - "@sentry/core" "4.4.2" - "@sentry/hub" "4.4.2" - "@sentry/types" "4.4.2" - "@sentry/utils" "4.4.2" + version "4.5.0" + resolved "https://registry.yarnpkg.com/@sentry/node/-/node-4.5.0.tgz#d569b1f4b0fb4e9e5c9e8deb181e2b4ca21d111e" + integrity sha512-gR/SR3kqbVjIsoO3ZkDr95+9B+Hqefp00LqhbffgNc+emG6eB4QmMKooZYUKyG28l/d39THe76AWAq748JcmIw== + dependencies: + "@sentry/core" "4.5.0" + "@sentry/hub" "4.5.0" + "@sentry/types" "4.5.0" + "@sentry/utils" "4.5.0" "@types/stack-trace" "0.0.29" cookie "0.3.1" - https-proxy-agent "^2.2.1" + https-proxy-agent "2.2.1" + lru_map "0.3.3" lsmod "1.0.0" stack-trace "0.0.10" tslib "^1.9.3" -"@sentry/types@4.4.2": - version "4.4.2" - resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.4.2.tgz#f38dd3bc671cd2f5983a85553aebeac9c2286b17" - integrity sha512-QyQd6PKKIyjJgaq/RQjsxPJEWbXcuiWZ9RvSnhBjS5jj53HEzkM1qkbAFqlYHJ1DTJJ1EuOM4+aTmGzHe93zuA== +"@sentry/types@4.5.0": + version "4.5.0" + resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.5.0.tgz#59e2a27d48b01b44e8959aa5c8a30514fe1086a9" + integrity sha512-IstPjFoebQGrQWdM732D/+S0BTovmDgezyplk4kLEb87+/B+YK0hlhziUa7B2byTFJGgQQKXy7h2sKZBrA3GUA== -"@sentry/utils@4.4.2": - version "4.4.2" - resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.4.2.tgz#e05a47e135ecef29e63a996f59aee8c8f792c222" - integrity sha512-j/Ad8G1abHlJdD2q7aWWbSOSeWB5M5v1R1VKL8YPlwEbSvvmEQWePhBKFI0qlnKd2ObdUQsj86pHEXJRSFNfCw== +"@sentry/utils@4.5.0": + version "4.5.0" + resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.5.0.tgz#44e33773dc631cc22240a327942a8e4b97d42850" + integrity sha512-H05lcDUL3D01f6egjglDO7ICAQ6nOroNHCol9Q3z7lutveGirh4aWFTpw8XU9H7/jnGGZorc0wSrLQtq8vWm7A== dependencies: - "@sentry/types" "4.4.2" + "@sentry/types" "4.5.0" tslib "^1.9.3" "@types/stack-trace@0.0.29": @@ -1184,7 +1185,7 @@ hosted-git-info@^2.1.4: resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz#97f236977bd6e125408930ff6de3eec6281ec047" integrity sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w== -https-proxy-agent@^2.2.1: +https-proxy-agent@2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-2.2.1.tgz#51552970fa04d723e04c56d04178c3f92592bbc0" integrity sha512-HPCTS1LW51bcyMYbxUIOO4HEOlQ1/1qRaFWcyxvwaqUS9TY88aoEuHUY33kuAh1YhVVaDQhLZsnPd+XNARWZlQ== @@ -1603,6 +1604,11 @@ lru-cache@^4.0.1: pseudomap "^1.0.2" yallist "^2.1.2" +lru_map@0.3.3: + version "0.3.3" + resolved "https://registry.yarnpkg.com/lru_map/-/lru_map-0.3.3.tgz#b5c8351b9464cbd750335a79650a0ec0e56118dd" + integrity sha1-tcg1G5Rky9dQM1p5ZQoOwOVhGN0= + lsmod@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/lsmod/-/lsmod-1.0.0.tgz#9a00f76dca36eb23fa05350afe1b585d4299e64b" @@ -2127,9 +2133,9 @@ regexp-clone@0.0.1: integrity sha1-p8LgmJH9vzj7sQ03b7cwA+aKxYk= regexp-tree@~0.0.85: - version "0.0.85" - resolved "https://registry.yarnpkg.com/regexp-tree/-/regexp-tree-0.0.85.tgz#278ef1946084e9ddd84b26e69828aab69e3c3186" - integrity sha512-KkuweOhL1M00EHljLhq2lARpLoazdKd0BpkfOZKcO55lWhRqeRCIPSPGf9osgMPj1l/0v37FaqDwa9Ks1W+92A== + version "0.0.86" + resolved "https://registry.yarnpkg.com/regexp-tree/-/regexp-tree-0.0.86.tgz#ea4e26220eebad25313d6f4f19c23535cec86745" + integrity sha512-xjTX9GmRw7Nn2wxinLoqQXv9Dt5yerP7CJIsYe/5z5gFs0Ltu20wRuZophafi9sf0JpsdVtki5EuICRYpgB1AQ== dependencies: cli-table3 "^0.5.0" colors "^1.1.2" @@ -2410,9 +2416,9 @@ swearjar@^0.2.0: integrity sha1-w/Gje/zXye+z1mY8mNTBAqnjyj0= table@^5.0.2: - version "5.1.1" - resolved "https://registry.yarnpkg.com/table/-/table-5.1.1.tgz#92030192f1b7b51b6eeab23ed416862e47b70837" - integrity sha512-NUjapYb/qd4PeFW03HnAuOJ7OMcBkJlqeClWxeNlQ0lXGSb52oZXGzkO0/I0ARegQ2eUT1g2VDJH0eUxDRcHmw== + version "5.2.0" + resolved "https://registry.yarnpkg.com/table/-/table-5.2.0.tgz#2e38bd1f16dd3f97085ac80cdc574ad9198af04d" + integrity sha512-hAdBBAMCZl4/U3eQhsPN2Z8wRJC98lpRhDW2I86VQbPBqyj4E681VhvUkfb90qUJ4rnRfu8t4/8SGHPsAH1ygg== dependencies: ajv "^6.6.1" lodash "^4.17.11"