diff --git a/labscore/client.js b/labscore/client.js index 2f27b5d..291e5f2 100644 --- a/labscore/client.js +++ b/labscore/client.js @@ -25,24 +25,64 @@ module.exports.paginator = new Paginator(cluster, { pageNumber: true }); +// Clients + let commandPrefix = '.' if(process.env.PREFIX_OVERRIDE) commandPrefix = process.env.PREFIX_OVERRIDE; +const commandClient = new CommandClient(cluster, { + activateOnEdits: true, + mentionsEnabled: false, + prefix: commandPrefix, + ratelimits: [ + {duration: 60000, limit: 50, type: 'guild'}, + {duration: 5000, limit: 5, type: 'channel'}, + ] +}); + +const interactionClient = new InteractionCommandClient() + +const { maintower } = require('./logging'); +const { icon } = require('./utils/markdown'); +const { editOrReply } = require('./utils/message'); + +commandClient.on('commandRunError', async ({context, error}) => { + try{ + // Log the error via our maintower service + let packages = { + data: {}, + origin: {}, + meta: { + shard: context.shardId.toString(), + cluster: context.manager.clusterId.toString() + } + } + + if(context.user) packages.origin.user = { name: `${context.user.username}#${context.user.discriminator}`, id: context.user.id } + if(context.guild) packages.origin.guild = { name: context.guild.name, id: context.guild.id } + if(context.channel) packages.origin.channel = { name: context.channel.name, id: context.channel.id } + + packages.data.command = context.message.content + packages.data.error = error ? error.stack || error.message : error + if(error.raw) packages.data.raw = JSON.stringify(error.raw, null, 2) + + let req = await maintower(packages, "01") + + await editOrReply(context, { + content: `${icon("cross")} Something went wrong while attempting to run this command. (Reference: \`${req}\`)` + }) + }catch(e){ + await editOrReply(context, { + content: `${icon("cross")} Something went wrong while attempting to run this command.` + }) + } +}); + (async () => { await cluster.run(); - const commandClient = new CommandClient(cluster, { - activateOnEdits: true, - mentionsEnabled: false, - prefix: commandPrefix, - ratelimits: [ - {duration: 60000, limit: 50, type: 'guild'}, - {duration: 5000, limit: 5, type: 'channel'}, - ] - }); await commandClient.addMultipleIn('../commands/message/'); await commandClient.run() - const interactionClient = new InteractionCommandClient() await interactionClient.addMultipleIn('../commands/interaction/'); await interactionClient.run(); diff --git a/labscore/logging.js b/labscore/logging.js new file mode 100644 index 0000000..94eaa39 --- /dev/null +++ b/labscore/logging.js @@ -0,0 +1,22 @@ +const superagent = require('superagent') + +const MAINTOWER_BASE_URL = `https://labscore-v2.vercel.app/maintower/` + +let maintowerClient = "1fepg2wdk-prod" +if(process.env.MAINTOWER_OVERRIDE) maintowerClient = process.env.MAINTOWER_OVERRIDE + +module.exports.maintower = async function (packages, type){ + try{ + let res = await superagent.post(MAINTOWER_BASE_URL + 'invoke') + .query({ + client: maintowerClient, + type: type + }) + .send(packages) + if(res.body.status == 0) return res.body.id; + throw res.body.message + }catch(e){ + console.log(e) + throw "Maintower request failed." + } +} \ No newline at end of file