diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5cb03d8 --- /dev/null +++ b/.env.example @@ -0,0 +1,69 @@ +# Discord Bot Token +token= +# Environment the bot runs in. +# Valid options are prod, local. +environment=local +# (Optional) Overwrites the bots prefixes. Separated by | +# Example: ,|,, +PREFIX_OVERRIDE= + +# Configures sharding for the bot. +# Leave at 1 for local development. +SHARDS=1 +SHARDS_PER_CLUSTER=1 + +# (Optional) Localhost port that should be used for 1p API calls. +# Example: 3000 +USE_LOCAL_API= + +# (Optional) Changes the path of the client module. +PATH_OVERRIDE= + +# lc.art Webhook +ART_WEBHOOK= + +# (Optional) Testing Service Config URL +# If left empty will disable all test features. +TESTING_CONFIG_URL= + +# Maintower (Logging) --- + +# (Optional) Maintower Service URL. +# If left out will disable logging. +MAINTOWER_URL= +# (Optional) Custom logging source ID +MAINTOWER_OVERRIDE= + +# 1p API Key +API_KEY= + +# Ingest Analytics API --- + +# (Optional) Ingest Service Host URL +# If left out will disable analytics. +INGEST_SERVICE_HOST= +# (Required for Ingest) Ingest Analytics Client ID +INGEST_SERVICE_CLIENT= + +# Obelisk 1p API --- + +# Obelisk Host URL +OBELISK_HOST= +# Obelisk 1p API Key +MONOLITH_API_KEY= + +# 3p API Keys --- + +# Google Tenor API Key +GOOGLE_TENOR_KEY= +# Makesweet API Key +MAKESWEET_KEY= +# SongLink API Key +SONGLINK_KEY= +# XKCD Key +XKCD_KEY= + +# Manager --- + +# (Optional) Manager Host URL +PB_MANAGER_HOST= \ No newline at end of file diff --git a/index.js b/index.js index 82d566b..757c9df 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,8 @@ const time = Date.now(); const token = process.env.token; // Get the correct path for each environment type -let client = `./labscore/labscore/client.js`; -if(process.env.environment === "local") client = `./labscore/client.js`; +let client = "./labscore/client.js" +if(process.env.PATH_OVERRIDE) client = process.env.PATH_OVERRIDE; const SHARDS = process.env.SHARDS || 2; const SHARDS_PER_CLUSTER = process.env.SHARDS_PER_CLUSTER_OVERRIDE || 2; @@ -19,26 +19,26 @@ const manager = new ClusterManager(client, token, { }); (async () => { - console.log(`[${process.env.HOSTNAME}] launching bot`) + console.log(`[${process.env.HOSTNAME || "labscore"}] launching bot`) // Logging manager.on("clusterProcess", ({ clusterProcess }) => { clusterProcess.on('close', ({code, signal}) => { - basecamp(`<:ico_w4:1086624964284788787>\`[${process.env.HOSTNAME}]\` **\` CLUSTER_CLOSED \`** Cluster ${clusterProcess.clusterId} closed with code \`${code}\` and signal \`${signal}\` @ \`${Date.now()}\``) + basecamp(`<:ico_w4:1086624964284788787>\`[${process.env.HOSTNAME || "labscore"}]\` **\` CLUSTER_CLOSED \`** Cluster ${clusterProcess.clusterId} closed with code \`${code}\` and signal \`${signal}\` @ \`${Date.now()}\``) }); clusterProcess.on('warn', async ({error}) => { - await basecamp(`<:ico_w2:1086624961025810485>\`[${process.env.HOSTNAME}]\` **\` CLUSTER_WARNING \`** Cluster ${clusterProcess.clusterId} issued warning @ \`${Date.now()}\``) + await basecamp(`<:ico_w2:1086624961025810485>\`[${process.env.HOSTNAME || "labscore"}]\` **\` CLUSTER_WARNING \`** Cluster ${clusterProcess.clusterId} issued warning @ \`${Date.now()}\``) await basecamp(`\`\`\`js\n${error}\`\`\``) }); }) await manager.run(); - console.log(`[${process.env.HOSTNAME}] bot ready. took ${(Date.now() - time) / 1000}.`) + console.log(`[${process.env.HOSTNAME || "labscore"}] bot ready. took ${(Date.now() - time) / 1000}.`) // This is kind of a hack. // Our current deployment system has a tendency to launch the bot twice, this "ensures" that // incorrect instances close again. This should *probably* just be moved to dirigent instead. - if(process.env.environment.toLowerCase() === "prod"){ + if(process.env.PB_MANAGER_HOST){ let liveDeploy = await superagent.get(`${process.env.PB_MANAGER_HOST}_pbs/v1/GetPbServiceId`) if(process.env.HOSTNAME !== liveDeploy.body.d){ console.log(`[${process.env.HOSTNAME}] invalid deployment session`) diff --git a/labscore/client.js b/labscore/client.js index 5edf5ea..5a8d8f8 100644 --- a/labscore/client.js +++ b/labscore/client.js @@ -129,6 +129,17 @@ commandClient.on('commandDelete', async ({context, reply}) => { commandClient.on('commandRunError', async ({context, error}) => { try{ + // No service configured + if(!process.env.MAINTOWER_URL){ + console.log(error); + + await editOrReply(context, { + content: `${icon("cross")} Something went wrong while attempting to run this command. (check console)` + }) + + return; + } + // Log the error via our maintower service let packages = { data: {}, @@ -162,6 +173,17 @@ commandClient.on('commandRunError', async ({context, error}) => { interactionClient.on('commandRunError', async ({context, error}) => { try{ + // No service configured + if(!process.env.MAINTOWER_URL){ + console.error(error ? error.stack || error.message : error); + + await editOrReply(context, { + content: `${icon("cross")} Something went wrong while attempting to run this command. (check console)` + }) + + return; + } + // Log the error via our maintower service let packages = { data: {}, @@ -197,6 +219,9 @@ interactionClient.on('commandRunError', async ({context, error}) => { // Logging cluster.on(ClientEvents.REST_RESPONSE, async ({response, restRequest, shard}) => { + // No service configured + if(!process.env.MAINTOWER_URL) return; + const route = response.request.route; if (route) { if (!response.ok) { @@ -209,6 +234,7 @@ interactionClient.on('commandRunError', async ({context, error}) => { }); cluster.on(ClientEvents.WARN, async ({error}) => { + if(!process.env.MAINTOWER_URL) { console.warn(error); return; } await basecamp(`<:ico_w2:1086624961025810485>\`[${process.env.HOSTNAME}]\` **\` CLUSTER_WARNING \`** Cluster ${cluster.manager.clusterId} reported warning @ \`${Date.now()}\`:\n\`\`\`${error}\`\`\``) }); diff --git a/labscore/logging.js b/labscore/logging.js index cc078ab..b65ce8d 100644 --- a/labscore/logging.js +++ b/labscore/logging.js @@ -1,11 +1,12 @@ const superagent = require('superagent') -const MAINTOWER_BASE_URL = `https://labscore-v2.vercel.app/maintower/` +const MAINTOWER_BASE_URL = process.env.MAINTOWER_URL let maintowerClient = "1fepg2wdk-prod" if(process.env.MAINTOWER_OVERRIDE) maintowerClient = process.env.MAINTOWER_OVERRIDE module.exports.maintower = async function (packages, type){ + if(!MAINTOWER_BASE_URL) { console.warn("No maintower url configured."); return; } try{ let res = await superagent.post(MAINTOWER_BASE_URL + 'invoke') .set({ @@ -26,6 +27,7 @@ module.exports.maintower = async function (packages, type){ } module.exports.basecamp = async function (log, content = ""){ + if(!MAINTOWER_BASE_URL) { console.warn("No maintower url configured."); return; } try{ let res = await superagent.post(MAINTOWER_BASE_URL + 'basecamp') .set({ @@ -41,6 +43,7 @@ module.exports.basecamp = async function (log, content = ""){ } module.exports.ingest = async function (event, type = "generic"){ + if(!process.env.INGEST_SERVICE_HOST) return console.warn("No ingest service host configured.") try{ let r = await superagent.get(`${process.env.INGEST_SERVICE_HOST}/d/${type}/${encodeURIComponent(event)}`) .set("x-ingest-client", process.env.INGEST_SERVICE_CLIENT) diff --git a/labscore/utils/testing.js b/labscore/utils/testing.js index ad20c4b..a4949cc 100644 --- a/labscore/utils/testing.js +++ b/labscore/utils/testing.js @@ -36,6 +36,8 @@ async function getTestConfig(){ } async function hasFeature(context, feature){ + if(!process.env.TESTING_CONFIG_URL) { console.warn("Test service config URL is missing, test features will be disabled."); return false; }; + // We need to load the test config first if(TESTING_REVISION == "-1") await getTestConfig();