added discord container for renaming and role-management
This commit is contained in:
19
apps/discord-server/.d.ts
vendored
Normal file
19
apps/discord-server/.d.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
declare module "next-auth/jwt" {
|
||||
interface JWT {
|
||||
uid: string;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
}
|
||||
}
|
||||
declare module "cookie-parser";
|
||||
|
||||
import type { User } from "@repo/db";
|
||||
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user?: User | null;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
apps/discord-server/.dockerignore
Normal file
6
apps/discord-server/.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
||||
node_modules
|
||||
Dockerfile
|
||||
.dockerignore
|
||||
nodemon.json
|
||||
.env
|
||||
.env.example
|
||||
7
apps/discord-server/.env.example
Normal file
7
apps/discord-server/.env.example
Normal file
@@ -0,0 +1,7 @@
|
||||
DISCORD_SERVER_PORT=3005
|
||||
DISCORD_GUILD_ID=1077269395019141140
|
||||
DISCORD_OAUTH_CLIENT_ID=930384053344034846
|
||||
DISCORD_OAUTH_SECRET=96aSvmIePqFTbGc54mad0QsZfDnYwhl1
|
||||
DISCORD_BOT_TOKEN=OTMwMzg0MDUzMzQ0MDM0ODQ2.G7zIy-._hE3dTbtUv6sd7nIP2PUn3d8s-2MFk0x3nYMg8
|
||||
DISCORD_REDIRECT_URL=https://hub.premiumag.de/api/discord-redirect
|
||||
NEXT_PUBLIC_DISCORD_URL=https://discord.com/oauth2/authorize?client_id=930384053344034846&response_type=code&redirect_uri=https%3A%2F%2Fhub.premiumag.de%2Fapi%2Fdiscord-redirect&scope=identify+guilds+email
|
||||
50
apps/discord-server/Dockerfile
Normal file
50
apps/discord-server/Dockerfile
Normal file
@@ -0,0 +1,50 @@
|
||||
FROM node:22-alpine AS base
|
||||
|
||||
ENV PNPM_HOME="/usr/local/pnpm"
|
||||
ENV PATH="${PNPM_HOME}:${PATH}"
|
||||
RUN corepack enable && corepack prepare pnpm@latest --activate
|
||||
|
||||
RUN pnpm add -g turbo@^2.5
|
||||
|
||||
FROM base AS builder
|
||||
RUN apk update
|
||||
RUN apk add --no-cache libc6-compat
|
||||
|
||||
WORKDIR /usr/app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN ls -lh
|
||||
|
||||
RUN turbo prune discord-server --docker
|
||||
|
||||
FROM base AS installer
|
||||
RUN apk update
|
||||
RUN apk add --no-cache libc6-compat
|
||||
|
||||
WORKDIR /usr/app
|
||||
|
||||
COPY --from=builder /usr/app/out/json/ .
|
||||
RUN pnpm install
|
||||
|
||||
# Build the project
|
||||
COPY --from=builder /usr/app/out/full/ .
|
||||
|
||||
RUN turbo run build
|
||||
|
||||
FROM base AS runner
|
||||
WORKDIR /usr/app
|
||||
|
||||
# Don't run production as root
|
||||
RUN addgroup --system --gid 1001 nodejs
|
||||
RUN adduser --system --uid 1001 nextjs
|
||||
USER nextjs
|
||||
|
||||
# Automatically leverage output traces to reduce image size
|
||||
# https://nextjs.org/docs/advanced-features/output-file-tracing
|
||||
COPY --from=installer --chown=nextjs:nodejs /usr/app/ ./
|
||||
|
||||
# Expose the application port
|
||||
EXPOSE 3003
|
||||
|
||||
CMD ["pnpm", "--dir", "apps/discord-server", "run", "start"]
|
||||
19
apps/discord-server/index.ts
Normal file
19
apps/discord-server/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import "dotenv/config";
|
||||
import express from "express";
|
||||
import { createServer } from "http";
|
||||
import router from "routes/router";
|
||||
import cookieParser from "cookie-parser";
|
||||
import cors from "cors";
|
||||
import "modules/chron";
|
||||
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(cookieParser());
|
||||
app.use(router);
|
||||
|
||||
server.listen(process.env.DISCORD_SERVER_PORT, () => {
|
||||
console.log(`Server running on port ${process.env.DISCORD_SERVER_PORT}`);
|
||||
});
|
||||
62
apps/discord-server/modules/chron.ts
Normal file
62
apps/discord-server/modules/chron.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { MissionLog, prisma } from "@repo/db";
|
||||
import cron from "node-cron";
|
||||
|
||||
const removeClosedMissions = async () => {
|
||||
const oldMissions = await prisma.mission.findMany({
|
||||
where: {
|
||||
state: "running",
|
||||
},
|
||||
});
|
||||
oldMissions.forEach(async (mission) => {
|
||||
const lastAlert = (mission.missionLog as unknown as MissionLog[]).find((l) => {
|
||||
return l.type === "alert-log";
|
||||
});
|
||||
const lastAlertTime = lastAlert ? new Date(lastAlert.timeStamp) : null;
|
||||
|
||||
const aircraftsInMission = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
stationId: {
|
||||
in: mission.missionStationIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (
|
||||
!aircraftsInMission ||
|
||||
!aircraftsInMission.some((a) => ["1", "2", "6"].includes(a.fmsStatus))
|
||||
)
|
||||
return;
|
||||
|
||||
const now = new Date();
|
||||
if (!lastAlertTime) return;
|
||||
// change State to closed if last alert was more than 180 minutes ago
|
||||
if (now.getTime() - lastAlertTime.getTime() < 30 * 60 * 1000) return;
|
||||
const log: MissionLog = {
|
||||
type: "completed-log",
|
||||
auto: true,
|
||||
timeStamp: new Date().toISOString(),
|
||||
data: {},
|
||||
};
|
||||
|
||||
await prisma.mission.update({
|
||||
where: {
|
||||
id: mission.id,
|
||||
},
|
||||
data: {
|
||||
state: "finished",
|
||||
missionLog: {
|
||||
push: log as any,
|
||||
},
|
||||
},
|
||||
});
|
||||
console.log(`Mission ${mission.id} closed due to inactivity.`);
|
||||
});
|
||||
};
|
||||
|
||||
cron.schedule("*/5 * * * *", async () => {
|
||||
try {
|
||||
await removeClosedMissions();
|
||||
} catch (error) {
|
||||
console.error("Error removing closed missions:", error);
|
||||
}
|
||||
});
|
||||
19
apps/discord-server/modules/discord.ts
Normal file
19
apps/discord-server/modules/discord.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Client, GatewayIntentBits } from "discord.js";
|
||||
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.Guilds],
|
||||
});
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
|
||||
if (!token) {
|
||||
throw new Error("DISCORD_BOT_TOKEN environment variable is not set.");
|
||||
}
|
||||
|
||||
client.login(token);
|
||||
|
||||
client.on("ready", () => {
|
||||
console.log(`Logged in as ${client.user?.tag}`);
|
||||
});
|
||||
|
||||
export default client;
|
||||
5
apps/discord-server/nodemon.json
Normal file
5
apps/discord-server/nodemon.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"watch": ["."],
|
||||
"ext": "ts",
|
||||
"exec": "tsx index.ts"
|
||||
}
|
||||
35
apps/discord-server/package.json
Normal file
35
apps/discord-server/package.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "discord-server",
|
||||
"exports": {
|
||||
"helpers": "./helper"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "nodemon --signal SIGINT",
|
||||
"start": "tsx index.ts --transpile-only",
|
||||
"build": "tsc"
|
||||
},
|
||||
"packageManager": "pnpm@10.11.0",
|
||||
"devDependencies": {
|
||||
"@repo/db": "workspace:*",
|
||||
"@repo/typescript-config": "workspace:*",
|
||||
"@types/cookie-parser": "^1.4.8",
|
||||
"@types/cors": "^2.8.18",
|
||||
"@types/express": "^5.0.2",
|
||||
"@types/node": "^22.15.29",
|
||||
"@types/nodemailer": "^6.4.17",
|
||||
"concurrently": "^9.1.2",
|
||||
"typescript": "latest"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.9.0",
|
||||
"cors": "^2.8.5",
|
||||
"cron": "^4.3.1",
|
||||
"discord.js": "^14.19.3",
|
||||
"dotenv": "^16.5.0",
|
||||
"express": "^5.1.0",
|
||||
"node-cron": "^4.1.0",
|
||||
"nodemon": "^3.1.10",
|
||||
"react": "^19.1.0",
|
||||
"tsx": "^4.19.4"
|
||||
}
|
||||
}
|
||||
69
apps/discord-server/routes/member.ts
Normal file
69
apps/discord-server/routes/member.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import client from "modules/discord";
|
||||
|
||||
const GUILD_ID = process.env.DISCORD_GUILD_ID;
|
||||
if (!GUILD_ID) {
|
||||
throw new Error("DISCORD_GUILD_ID environment variable is not set.");
|
||||
}
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
const getMember = async (memberId: string) => {
|
||||
const guild = client.guilds.cache.get(GUILD_ID);
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
try {
|
||||
return guild.members.cache.get(memberId) ?? (await guild.members.fetch(memberId));
|
||||
} catch (error) {
|
||||
console.error("Error fetching member:", error);
|
||||
throw new Error("Member not found");
|
||||
}
|
||||
};
|
||||
|
||||
router.post("/rename", async (req: Request, res: Response) => {
|
||||
const { newName, memberId } = req.body;
|
||||
if (typeof newName !== "string" || !memberId) {
|
||||
res.status(400).json({ error: "Invalid or missing newName or memberId" });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const member = await getMember(memberId);
|
||||
await member.setNickname(newName);
|
||||
console.log(`Member ${member.id} renamed to ${newName}`);
|
||||
res.status(200).json({ message: "Member renamed successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error renaming member:", error);
|
||||
res.status(500).json({ error: "Failed to rename member" });
|
||||
}
|
||||
});
|
||||
|
||||
const handleRoleChange = (action: "add" | "remove") => async (req: Request, res: Response) => {
|
||||
const { roleIds, memberId } = req.body;
|
||||
if (!Array.isArray(roleIds) || !memberId) {
|
||||
res.status(400).json({ error: "Invalid or missing roleIds or memberId" });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const member = await getMember(memberId);
|
||||
|
||||
const currentRoleIds = member.roles.cache.map((role) => role.id);
|
||||
const filteredRoleIds =
|
||||
action === "add"
|
||||
? roleIds.filter((id: string) => !currentRoleIds.includes(id))
|
||||
: roleIds.filter((id: string) => currentRoleIds.includes(id));
|
||||
if (filteredRoleIds.length === 0) {
|
||||
res.status(200).json({ message: `No roles to ${action}` });
|
||||
return;
|
||||
}
|
||||
|
||||
await member.roles[action](roleIds);
|
||||
res.status(200).json({ message: `Roles ${action}ed successfully` });
|
||||
} catch (error) {
|
||||
console.error(`Error ${action}ing roles:`, error);
|
||||
res.status(500).json({ error: `Failed to ${action} roles` });
|
||||
}
|
||||
};
|
||||
|
||||
router.post("/add-role", handleRoleChange("add"));
|
||||
router.post("/remove-role", handleRoleChange("remove"));
|
||||
|
||||
export default router;
|
||||
8
apps/discord-server/routes/router.ts
Normal file
8
apps/discord-server/routes/router.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Router } from "express";
|
||||
import memberRouter from "./member";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
router.use("/member", memberRouter);
|
||||
|
||||
export default router;
|
||||
11
apps/discord-server/tsconfig.json
Normal file
11
apps/discord-server/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "@repo/typescript-config/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"allowImportingTsExtensions": false,
|
||||
"baseUrl": ".",
|
||||
"jsx": "react"
|
||||
},
|
||||
"include": ["**/*.ts", "./index.ts", "**/*.d.ts"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user