add example env, "improve" selfhostability

This commit is contained in:
bignutty 2024-07-17 17:21:19 +02:00
parent 99bac88155
commit 07ceaac9d9
5 changed files with 108 additions and 8 deletions

69
.env.example Normal file
View file

@ -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=

View file

@ -7,8 +7,8 @@ const time = Date.now();
const token = process.env.token; const token = process.env.token;
// Get the correct path for each environment type // Get the correct path for each environment type
let client = `./labscore/labscore/client.js`; let client = "./labscore/client.js"
if(process.env.environment === "local") client = `./labscore/client.js`; if(process.env.PATH_OVERRIDE) client = process.env.PATH_OVERRIDE;
const SHARDS = process.env.SHARDS || 2; const SHARDS = process.env.SHARDS || 2;
const SHARDS_PER_CLUSTER = process.env.SHARDS_PER_CLUSTER_OVERRIDE || 2; const SHARDS_PER_CLUSTER = process.env.SHARDS_PER_CLUSTER_OVERRIDE || 2;
@ -19,26 +19,26 @@ const manager = new ClusterManager(client, token, {
}); });
(async () => { (async () => {
console.log(`[${process.env.HOSTNAME}] launching bot`) console.log(`[${process.env.HOSTNAME || "labscore"}] launching bot`)
// Logging // Logging
manager.on("clusterProcess", ({ clusterProcess }) => { manager.on("clusterProcess", ({ clusterProcess }) => {
clusterProcess.on('close', ({code, signal}) => { 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}) => { 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 basecamp(`\`\`\`js\n${error}\`\`\``)
}); });
}) })
await manager.run(); 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. // This is kind of a hack.
// Our current deployment system has a tendency to launch the bot twice, this "ensures" that // 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. // 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`) let liveDeploy = await superagent.get(`${process.env.PB_MANAGER_HOST}_pbs/v1/GetPbServiceId`)
if(process.env.HOSTNAME !== liveDeploy.body.d){ if(process.env.HOSTNAME !== liveDeploy.body.d){
console.log(`[${process.env.HOSTNAME}] invalid deployment session`) console.log(`[${process.env.HOSTNAME}] invalid deployment session`)

View file

@ -129,6 +129,17 @@ commandClient.on('commandDelete', async ({context, reply}) => {
commandClient.on('commandRunError', async ({context, error}) => { commandClient.on('commandRunError', async ({context, error}) => {
try{ 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 // Log the error via our maintower service
let packages = { let packages = {
data: {}, data: {},
@ -162,6 +173,17 @@ commandClient.on('commandRunError', async ({context, error}) => {
interactionClient.on('commandRunError', async ({context, error}) => { interactionClient.on('commandRunError', async ({context, error}) => {
try{ 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 // Log the error via our maintower service
let packages = { let packages = {
data: {}, data: {},
@ -197,6 +219,9 @@ interactionClient.on('commandRunError', async ({context, error}) => {
// Logging // Logging
cluster.on(ClientEvents.REST_RESPONSE, async ({response, restRequest, shard}) => { cluster.on(ClientEvents.REST_RESPONSE, async ({response, restRequest, shard}) => {
// No service configured
if(!process.env.MAINTOWER_URL) return;
const route = response.request.route; const route = response.request.route;
if (route) { if (route) {
if (!response.ok) { if (!response.ok) {
@ -209,6 +234,7 @@ interactionClient.on('commandRunError', async ({context, error}) => {
}); });
cluster.on(ClientEvents.WARN, async ({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}\`\`\``) await basecamp(`<:ico_w2:1086624961025810485>\`[${process.env.HOSTNAME}]\` **\` CLUSTER_WARNING \`** Cluster ${cluster.manager.clusterId} reported warning @ \`${Date.now()}\`:\n\`\`\`${error}\`\`\``)
}); });

View file

@ -1,11 +1,12 @@
const superagent = require('superagent') 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" let maintowerClient = "1fepg2wdk-prod"
if(process.env.MAINTOWER_OVERRIDE) maintowerClient = process.env.MAINTOWER_OVERRIDE if(process.env.MAINTOWER_OVERRIDE) maintowerClient = process.env.MAINTOWER_OVERRIDE
module.exports.maintower = async function (packages, type){ module.exports.maintower = async function (packages, type){
if(!MAINTOWER_BASE_URL) { console.warn("No maintower url configured."); return; }
try{ try{
let res = await superagent.post(MAINTOWER_BASE_URL + 'invoke') let res = await superagent.post(MAINTOWER_BASE_URL + 'invoke')
.set({ .set({
@ -26,6 +27,7 @@ module.exports.maintower = async function (packages, type){
} }
module.exports.basecamp = async function (log, content = ""){ module.exports.basecamp = async function (log, content = ""){
if(!MAINTOWER_BASE_URL) { console.warn("No maintower url configured."); return; }
try{ try{
let res = await superagent.post(MAINTOWER_BASE_URL + 'basecamp') let res = await superagent.post(MAINTOWER_BASE_URL + 'basecamp')
.set({ .set({
@ -41,6 +43,7 @@ module.exports.basecamp = async function (log, content = ""){
} }
module.exports.ingest = async function (event, type = "generic"){ module.exports.ingest = async function (event, type = "generic"){
if(!process.env.INGEST_SERVICE_HOST) return console.warn("No ingest service host configured.")
try{ try{
let r = await superagent.get(`${process.env.INGEST_SERVICE_HOST}/d/${type}/${encodeURIComponent(event)}`) let r = await superagent.get(`${process.env.INGEST_SERVICE_HOST}/d/${type}/${encodeURIComponent(event)}`)
.set("x-ingest-client", process.env.INGEST_SERVICE_CLIENT) .set("x-ingest-client", process.env.INGEST_SERVICE_CLIENT)

View file

@ -36,6 +36,8 @@ async function getTestConfig(){
} }
async function hasFeature(context, feature){ 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 // We need to load the test config first
if(TESTING_REVISION == "-1") await getTestConfig(); if(TESTING_REVISION == "-1") await getTestConfig();