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

Commit

Permalink
Events-AutoModDetect: Initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
RealEthanPlayzDev committed Sep 11, 2024
1 parent 47fc679 commit 2694c16
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ model Guild {
VerifyTempPaused Boolean @default(false)
VerifyVerifiedRoleId String @default("")
VerifyUnverifiedRoleId String @default("")
CreateCaseFromAutoMod Boolean @default(true)
EnabledGuildFeatures GuildFeatures[]
ModerationCase ModerationCase[]
Tag Tag[]
Expand Down
35 changes: 35 additions & 0 deletions src/events/autoModDetect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { AutoModerationActionType } from "discord.js";
import { GuildFeatures, ModerationAction } from "@prisma/client";
import type { MeteoriumEvent } from "./eventsEntry.js";

export const Event: MeteoriumEvent<"autoModerationActionExecution"> = {
event: "autoModerationActionExecution",
async callback(client, exec) {
const guildSettings = await client.db.guild.findUnique({ where: { GuildId: exec.guild.id } });
if (!guildSettings) throw new Error("could not get settings from database");
if (!guildSettings.CreateCaseFromAutoMod) return;
if (!client.guildFeatures.hasFeatureEnabled(exec.guild.id, GuildFeatures.Moderation)) return;

const rule =
exec.autoModerationRule || (await exec.guild.autoModerationRules.fetch(exec.ruleId).catch(() => null));
if (!rule) return;

await client.dbUtils.createModerationCase({
Action:
exec.action.type == AutoModerationActionType.Timeout ? ModerationAction.Mute : ModerationAction.Warn,
GuildId: exec.guild.id,
TargetUserId: exec.userId,
ModeratorUserId: rule.creatorId,
Reason: `Automatically generated case from Auto Moderation (automod) trigger:\n${rule.name}`,
Duration:
exec.action.type == AutoModerationActionType.Timeout
? exec.action.metadata.durationSeconds
? (exec.action.metadata.durationSeconds * 1000).toString()
: "0"
: undefined,
});

return;
},
once: false,
};
1 change: 1 addition & 0 deletions src/events/eventsEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * as PresenceResumption from "./presenceResumption.js";
export * as GuildDataSetup from "./guildDataSetup.js";
export * as GuildMemberJoinLogging from "./guildMemberJoinLogging.js";
export * as GuildMemberLeaveLogging from "./guildMemberLeaveLogging.js";
export * as AutoModDetect from "./autoModDetect.js";

export type MeteoriumEvent<EventName extends keyof ClientEvents> = {
event: EventName;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const client = new MeteoriumClient({
IntentsBitField.Flags.GuildModeration,
IntentsBitField.Flags.GuildVoiceStates,
IntentsBitField.Flags.GuildPresences,
IntentsBitField.Flags.AutoModerationConfiguration,
IntentsBitField.Flags.AutoModerationExecution,
],
});

Expand Down
9 changes: 8 additions & 1 deletion src/interactions/commands/management/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type DbSettingNames =
| "VerifyAttachEnabled"
| "VerifyTempPaused"
| "VerifyVerifiedRoleId"
| "VerifyUnverifiedRoleId";
| "VerifyUnverifiedRoleId"
| "CreateCaseFromAutoMod";
type SettingData = { type: SettingType; inName: string; dbName: DbSettingNames; description: string };
const settingsMapping: Array<SettingData> = [
{
Expand Down Expand Up @@ -85,6 +86,12 @@ const settingsMapping: Array<SettingData> = [
dbName: "VerifyUnverifiedRoleId",
description: "The role to be given for those who failed verification",
},
{
type: SettingType.Boolean,
inName: "casefromautomod",
dbName: "CreateCaseFromAutoMod",
description: "Detect automod events and automatically create a case from it",
},
];

const interactionData = new SlashCommandBuilder()
Expand Down

0 comments on commit 2694c16

Please sign in to comment.