Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Int-CI-Moderation-TempBan: Fix a bunch of stuff and add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
RealEthanPlayzDev committed Apr 25, 2024
1 parent c6adce9 commit a0edd71
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/interactions/commands/moderation/tempBan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const Command: MeteoriumChatCommand = {
// Ban
const delMsgSeconds = delMsgHistoryTime ? ms(delMsgHistoryTime) * 1000 : undefined;
await member.ban({
reason: `Case #${caseId} from ${moderator.username} (${moderator.id}): ${reason}`,
reason: `Case #${caseDb.CaseId} from ${moderator.username} (${moderator.id}): ${reason}`,
deleteMessageSeconds: delMsgSeconds
? delMsgSeconds >= 604800
? 604800
Expand Down Expand Up @@ -138,20 +138,41 @@ export const Command: MeteoriumChatCommand = {
return await interaction.respond([]);
},
async initialize(client) {
const logNS = client.logging.getNamespace("Moderation").getNamespace("TempBan");
setInterval(async () => {
await client.db.$transaction(async (tx) => {
const active = await tx.activeTempBans.findMany({ include: { Case: true } });
const promises = active.map(async (active) => {
const createdAt = active.Case.CreatedAt;
const current = new Date();
const expiresAt = new Date(Number(active.Case.CreatedAt) + ms(active.Case.Duration));
if (expiresAt <= createdAt) {
if (expiresAt <= current) {
logNS.verbose(`Processing unban for ${active.Case.TargetUserId} in ${active.Case.GuildId}`);

// Unban
const guild = await client.guilds.fetch(active.Case.GuildId);
await guild.members.unban(active.Case.TargetUserId);
await guild.members.unban(
active.Case.TargetUserId,
`Case #${active.Case.CaseId}: Automatic unban from temporary ban.`,
);
await tx.activeTempBans.delete({ where: { ActiveTempBanId: active.ActiveTempBanId } });
await tx.moderationCase.update({
where: { GlobalCaseId: active.GlobalCaseId },
data: { Active: false },
});

// Log
const logEmbed = await client.dbUtils.generateCaseEmbedFromCaseId(
active.Case.GuildId,
active.Case.CaseId,
undefined,
true,
);
if (!logEmbed)
throw new Error(
`could not generate embed for case #${active.Case.CaseId}@${active.Case.GuildId} (${active.Case.GlobalCaseId})`,
);
logEmbed.setTitle("Automatic unban");
await client.dbUtils.sendGuildLog(active.Case.GuildId, { embeds: [logEmbed] });
}
return;
});
Expand Down

0 comments on commit a0edd71

Please sign in to comment.