Skip to content

Commit 805e4e9

Browse files
authored
Feature/nathen418/detect containerization (#37)
2 parents 6265df2 + 62e5422 commit 805e4e9

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

env.template

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,4 @@ MONGODB_URI=
33
BOT_DEFAULT_PREFIX=
44
VERSION=
55
UPTIME_KUMA_MONITOR_DOMAIN=
6-
UPTIME_KUMA_MONITOR_ID=
7-
8-
9-
# Fill out these values with the role id's of the roles you have in your server
10-
11-
Prefrosh=
12-
Freshman=
13-
Sophomore=
14-
Junior=
15-
Senior=
16-
Graduatestudent=
17-
Alumni=
18-
Sileader=
19-
Ta=
20-
Studentemployee=
21-
Professor=
6+
UPTIME_KUMA_MONITOR_ID=

features/statuspage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import axios from "axios";
22
import { Client } from "discord.js";
3+
import { isDocker } from "../utils/util";
34

45
export default (client: Client): void => {
6+
// Check if the bot is running in a docker container by checking if the env variable UPTIME_KUMA_CONTAINERIZED is true
7+
if (isDocker()) return;
58
const updateStatus = async () => {
69
// This function is called every 1 minutes and pings the network status page for uptime monitoring
710
await axios.get(

index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import WOKCommands from "wokcommands";
44
import path from "path";
55
import chalk from "chalk";
66
import dotenv from "dotenv";
7+
import { isDocker } from "./utils/util";
78

89
// import custom modules
9-
import { checkForRoles, checkIfCollectionsExist } from "./rolesOps";
10+
import { checkForRoles, checkIfCollectionsExist } from "./utils/roleUtils";
1011
import { classModel } from "./models/classModel";
1112
import { staffModel } from "./models/staffModel";
1213
import { yearModel } from "./models/yearModel";
@@ -27,6 +28,8 @@ const client = new DiscordJs.Client({
2728
client.on("ready", async () => {
2829
if (client.user) {
2930
console.log(chalk.green(`Logged in as ${client.user.tag}!`));
31+
if (isDocker())
32+
console.log(chalk.blueBright(`Running in a Docker container!`));
3033
console.log(
3134
chalk.yellow.bold(`I am running version: ${process.env.VERSION}`)
3235
);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"typescript": "^4.5.5",
3232
"wokcommands": "^1.5.3"
3333
}
34-
}
34+
}

utils/util.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1+
import fs from "node:fs";
12
export function sleep(ms: number) {
23
// Create new promise that resolves itself after a delay of <ms>
34
return new Promise((resolve: (args: void) => void) =>
45
setTimeout(resolve, ms)
56
);
67
}
8+
9+
//! This code was taken from the package `is-docker` and modified to work here with esm
10+
//! Original repository: https://github.com/sindresorhus/is-docker
11+
let isDockerCached: boolean;
12+
13+
function hasDockerEnv() {
14+
try {
15+
fs.statSync("/.dockerenv");
16+
return true;
17+
} catch {
18+
return false;
19+
}
20+
}
21+
22+
function hasDockerCGroup() {
23+
try {
24+
return fs.readFileSync("/proc/self/cgroup", "utf8").includes("docker");
25+
} catch {
26+
return false;
27+
}
28+
}
29+
30+
export function isDocker() {
31+
// TODO: Use `??=` when targeting Node.js 16.
32+
if (isDockerCached === undefined) {
33+
isDockerCached = hasDockerEnv() || hasDockerCGroup();
34+
}
35+
36+
return isDockerCached;
37+
}

0 commit comments

Comments
 (0)