added discord container for renaming and role-management

This commit is contained in:
PxlLoewe
2025-06-05 01:03:13 -07:00
parent 3c620b9b67
commit 6c9942a984
26 changed files with 824 additions and 28 deletions

View File

@@ -1,9 +1,10 @@
import { getMoodleCourseCompletionStatus, getMoodleUserById } from "./moodle";
import { CronJob } from "cron";
import { prisma } from "@repo/db";
import { DISCORD_ROLES, prisma } from "@repo/db";
import { sendCourseCompletedEmail } from "modules/mail";
import { handleParticipantFinished } from "modules/event";
import { eventCompleted } from "helper/events";
import { addRolesToMember, removeRolesFromMember } from "modules/discord";
const syncMoodleIds = async () => {
try {
@@ -101,19 +102,91 @@ export const checkFinishedParticipants = async () => {
});
};
const checkUnfinishedParticipants = async () => {
const participantsPending = await prisma.participant.findMany({
where: {
completetionWorkflowFinished: false,
},
include: {
Event: true,
User: {
include: {
discordAccounts: true,
},
},
},
});
participantsPending.forEach(async (p) => {
if (!p.User) return;
const completed = eventCompleted(p.Event, p);
if (completed) return;
if (p.User.discordAccounts[0] && p.Event.discordRoleId) {
await addRolesToMember(p.User.discordAccounts[0].discordId, [p.Event.discordRoleId]);
prisma.participant.update({
where: {
id: p.id,
},
data: {
inscriptionWorkflowCompleted: true,
statusLog: {
push: {
event: "Discord-Rolle hinzugefügt",
timestamp: new Date(),
user: "system",
},
},
},
});
}
});
};
const checkDiscordRoles = async () => {
const user = await prisma.user.findMany({
where: {
discordAccounts: {
some: {},
},
},
include: {
discordAccounts: true,
},
});
for (const u of user) {
// Here ony member Roles regarding their rights are checked
if (!u.discordAccounts[0]) continue;
const discordAccount = u.discordAccounts[0];
// For Pilot
if (u.permissions.includes("PILOT")) {
await addRolesToMember(discordAccount.discordId, [DISCORD_ROLES.PILOT]); // ONLINE_PILOT
} else {
await removeRolesFromMember(discordAccount.discordId, [DISCORD_ROLES.PILOT]); // ONLINE_PILOT
}
// for Dispatcher
if (u.permissions.includes("DISPO")) {
await addRolesToMember(discordAccount.discordId, [DISCORD_ROLES.DISPATCHER]); // ONLINE_DISPATCHER
} else {
await removeRolesFromMember(discordAccount.discordId, [DISCORD_ROLES.DISPATCHER]); // ONLINE_DISPATCHER
}
}
};
CronJob.from({ cronTime: "0 * * * *", onTick: syncMoodleIds, start: true });
CronJob.from({
cronTime: "*/1 * * * *",
onTick: async () => {
await updateParticipantMoodleResults();
await checkFinishedParticipants();
await checkUnfinishedParticipants();
},
start: true,
});
const debug = async () => {
await updateParticipantMoodleResults();
await checkFinishedParticipants();
};
debug();
CronJob.from({
cronTime: "0 * * * *",
onTick: checkDiscordRoles,
start: true,
});

View File

@@ -0,0 +1,38 @@
import axios from "axios";
const discordAxiosClient = axios.create({
baseURL: process.env.DISCORD_SERVER_URL || "https://discord.com/api/v10",
});
export const renameMember = async (memberId: string, newName: string) => {
discordAxiosClient
.post("/member/rename", {
memberId,
newName,
})
.catch((error) => {
console.error("Error renaming member:", error);
});
};
export const addRolesToMember = async (memberId: string, roleIds: string[]) => {
discordAxiosClient
.post("/member/add-role", {
memberId,
roleIds,
})
.catch((error) => {
console.error("Error adding roles to member:", error);
});
};
export const removeRolesFromMember = async (memberId: string, roleIds: string[]) => {
discordAxiosClient
.post("/member/remove-role", {
memberId,
roleIds,
})
.catch((error) => {
console.error("Error removing roles from member:", error);
});
};

View File

@@ -1,4 +1,5 @@
import { Event, Participant, prisma, User } from "@repo/db";
import { removeRolesFromMember } from "modules/discord";
import { sendCourseCompletedEmail } from "modules/mail";
export const handleParticipantFinished = async (
@@ -6,7 +7,7 @@ export const handleParticipantFinished = async (
participant: Participant,
user: User,
) => {
const discordID = await prisma.discordAccount.findFirst({
const discordAccount = await prisma.discordAccount.findFirst({
where: {
userId: user.id,
},
@@ -33,7 +34,9 @@ export const handleParticipantFinished = async (
},
});
//TODO: Send Discord Message
if (event.discordRoleId && discordAccount) {
await removeRolesFromMember(discordAccount.discordId, [event.discordRoleId]);
}
await sendCourseCompletedEmail(user.email, user, event);
await prisma.participant.update({