Discord Permissions will be revoked, when under a penalty
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { DISCORD_ROLES, MissionLog, NotificationPayload, prisma } from "@repo/db";
|
||||
import { io } from "index";
|
||||
import cron from "node-cron";
|
||||
import { setUserStandardNamePermissions } from "routes/helper";
|
||||
import { changeMemberRoles } from "routes/member";
|
||||
|
||||
const removeMission = async (id: number, reason: string) => {
|
||||
@@ -141,21 +142,12 @@ const removeConnectedAircrafts = async () => {
|
||||
});
|
||||
};
|
||||
const removePermissionsForBannedUsers = async () => {
|
||||
const activePenalties = await prisma.penalty.findMany({
|
||||
const removePermissionsPenaltys = await prisma.penalty.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
type: "BAN",
|
||||
suspended: false,
|
||||
},
|
||||
{
|
||||
type: "TIME_BAN",
|
||||
suspended: false,
|
||||
until: {
|
||||
gt: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
removePermissionApplied: false,
|
||||
User: {
|
||||
DiscordAccount: { isNot: null },
|
||||
},
|
||||
},
|
||||
include: {
|
||||
User: {
|
||||
@@ -167,16 +159,33 @@ const removePermissionsForBannedUsers = async () => {
|
||||
},
|
||||
});
|
||||
|
||||
for (const penalty of activePenalties) {
|
||||
const user = penalty.User;
|
||||
const addPermissionsPenaltys = await prisma.penalty.findMany({
|
||||
where: {
|
||||
addPermissionApplied: false,
|
||||
User: {
|
||||
DiscordAccount: { isNot: null },
|
||||
},
|
||||
OR: [{ suspended: true }, { until: { lt: new Date().toISOString() } }],
|
||||
},
|
||||
include: {
|
||||
User: {
|
||||
include: {
|
||||
DiscordAccount: true,
|
||||
FormerDiscordAccounts: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (user.DiscordAccount) {
|
||||
await changeMemberRoles(
|
||||
user.DiscordAccount.discordId,
|
||||
[DISCORD_ROLES.PILOT, DISCORD_ROLES.DISPATCHER],
|
||||
"remove",
|
||||
);
|
||||
}
|
||||
for (const penalty of removePermissionsPenaltys) {
|
||||
const user = penalty.User;
|
||||
console.log(`Removing roles for user ${user.id} due to penalty ${penalty.id}`);
|
||||
|
||||
await changeMemberRoles(
|
||||
user.DiscordAccount!.discordId,
|
||||
[DISCORD_ROLES.PILOT, DISCORD_ROLES.DISPATCHER],
|
||||
"remove",
|
||||
);
|
||||
|
||||
for (const formerAccount of user.FormerDiscordAccounts) {
|
||||
await changeMemberRoles(
|
||||
@@ -185,15 +194,29 @@ const removePermissionsForBannedUsers = async () => {
|
||||
"remove",
|
||||
);
|
||||
}
|
||||
await prisma.penalty.update({
|
||||
where: { id: penalty.id },
|
||||
data: { removePermissionApplied: true },
|
||||
});
|
||||
}
|
||||
for (const penalty of addPermissionsPenaltys) {
|
||||
console.log(`Restoring roles for user ${penalty.userId} due to penalty ${penalty.id}`);
|
||||
await setUserStandardNamePermissions({
|
||||
memberId: penalty.User.DiscordAccount!.discordId,
|
||||
userId: penalty.userId,
|
||||
});
|
||||
await prisma.penalty.update({
|
||||
where: { id: penalty.id },
|
||||
data: { addPermissionApplied: true },
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
cron.schedule("*/5 * * * *", async () => {
|
||||
await removePermissionsForBannedUsers();
|
||||
});
|
||||
removePermissionsForBannedUsers();
|
||||
|
||||
cron.schedule("*/1 * * * *", async () => {
|
||||
try {
|
||||
await removePermissionsForBannedUsers();
|
||||
await removeClosedMissions();
|
||||
await removeConnectedAircrafts();
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user