/[sudobot]/branches/8.x/src/events/GuildAuditLogEntryCreateEvent.ts
ViewVC logotype

Contents of /branches/8.x/src/events/GuildAuditLogEntryCreateEvent.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6897 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /*
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2023 OSN Developers.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import { InfractionType } from "@prisma/client";
21 import {
22 AuditLogEvent,
23 Guild,
24 GuildAuditLogsActionType,
25 GuildAuditLogsEntry,
26 GuildAuditLogsTargetType,
27 GuildMember,
28 User,
29 VoiceChannel
30 } from "discord.js";
31 import EventListener from "../core/EventListener";
32 import { safeMemberFetch, safeUserFetch } from "../utils/fetch";
33
34 export default class GuildAuditLogEntryCreateEventListener extends EventListener<"guildAuditLogEntryCreate"> {
35 public readonly name = "guildAuditLogEntryCreate";
36
37 async execute(
38 auditLogEntry: GuildAuditLogsEntry<
39 AuditLogEvent,
40 GuildAuditLogsActionType,
41 GuildAuditLogsTargetType,
42 AuditLogEvent
43 >,
44 guild: Guild
45 ): Promise<void> {
46 if (auditLogEntry.action === AuditLogEvent.MemberBanAdd) {
47 const { executorId, executor, targetId, reason, target } = auditLogEntry;
48 const user =
49 target instanceof User
50 ? target
51 : target instanceof GuildMember
52 ? target.user
53 : targetId
54 ? await safeUserFetch(this.client, targetId)
55 : null;
56
57 if (user && executorId && executorId !== this.client.user?.id) {
58 const infraction = await this.client.prisma.infraction.create({
59 data: {
60 guildId: guild.id,
61 moderatorId: executorId,
62 type: InfractionType.BAN,
63 userId: user.id,
64 reason: reason ?? undefined
65 }
66 });
67
68 await this.client.loggerService.logUserBan({
69 moderator:
70 executor ??
71 (await safeUserFetch(this.client, executorId)) ??
72 ({
73 ...this.client.user!,
74 username: "Unknown",
75 id: executorId
76 } as unknown as User),
77 user,
78 guild,
79 id: infraction.id.toString(),
80 includeDeleteMessageSeconds: false,
81 reason: reason ?? undefined
82 });
83 }
84 } else if (auditLogEntry.action === AuditLogEvent.MemberKick) {
85 const { executorId, executor, targetId, reason, target } = auditLogEntry;
86 const user =
87 target instanceof User
88 ? target
89 : target instanceof GuildMember
90 ? target.user
91 : targetId
92 ? await safeUserFetch(this.client, targetId)
93 : null;
94
95 if (user && executorId && executorId !== this.client.user?.id) {
96 const infraction = await this.client.prisma.infraction.create({
97 data: {
98 guildId: guild.id,
99 moderatorId: executorId,
100 type: InfractionType.KICK,
101 userId: user.id,
102 reason: reason ?? undefined
103 }
104 });
105
106 await this.client.loggerService.logMemberKick({
107 moderator:
108 executor ??
109 (await safeUserFetch(this.client, executorId)) ??
110 ({
111 ...this.client.user!,
112 username: "Unknown",
113 id: executorId
114 } as unknown as User),
115 user,
116 guild,
117 id: infraction.id.toString(),
118 reason: reason ?? undefined
119 });
120 }
121 } else if (auditLogEntry.action === AuditLogEvent.MemberUpdate) {
122 const lastChange = auditLogEntry.changes.at(-1);
123 const channel = (await safeMemberFetch(guild, auditLogEntry.targetId!))?.voice
124 ?.channel as VoiceChannel | null;
125
126 if (!lastChange || !channel) {
127 return;
128 }
129
130 if (lastChange.key === "mute") {
131 if (lastChange.old === false && lastChange.new === true) {
132 await this.client.loggerService.logMemberVoiceMute({
133 user: auditLogEntry.target as User,
134 guild,
135 moderator: auditLogEntry.executor ?? undefined,
136 reason: auditLogEntry.reason ?? undefined,
137 channel
138 });
139 } else if (lastChange.old === true && lastChange.new === false) {
140 await this.client.loggerService.logMemberVoiceUnmute({
141 user: auditLogEntry.target as User,
142 guild,
143 moderator: auditLogEntry.executor ?? undefined,
144 reason: auditLogEntry.reason ?? undefined,
145 channel
146 });
147 }
148 }
149
150 if (lastChange.key === "deaf") {
151 if (lastChange.old === false && lastChange.new === true) {
152 await this.client.loggerService.logMemberDeaf({
153 user: auditLogEntry.target as User,
154 guild,
155 moderator: auditLogEntry.executor ?? undefined,
156 reason: auditLogEntry.reason ?? undefined,
157 channel
158 });
159 } else if (lastChange.old === true && lastChange.new === false) {
160 await this.client.loggerService.logMemberUndeaf({
161 user: auditLogEntry.target as User,
162 guild,
163 moderator: auditLogEntry.executor ?? undefined,
164 reason: auditLogEntry.reason ?? undefined,
165 channel
166 });
167 }
168 }
169 }
170 }
171 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26