/[sudobot]/branches/7.x/src/events/guildMember/GuildMemberUpdateEvent.ts
ViewVC logotype

Contents of /branches/7.x/src/events/guildMember/GuildMemberUpdateEvent.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: 4471 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 { AuditLogEvent, Events, GuildMember } from "discord.js";
22 import EventListener from "../../core/EventListener";
23 import { logError } from "../../utils/logger";
24
25 export default class GuildMemberUpdateEvent extends EventListener<Events.GuildMemberUpdate> {
26 public readonly name = Events.GuildMemberUpdate;
27
28 async execute(oldMember: GuildMember, newMember: GuildMember) {
29 super.execute(oldMember, newMember);
30
31 if (oldMember.nickname !== newMember.nickname) {
32 await this.client.logger.logNicknameUpdate(oldMember, newMember);
33 }
34
35 if (!oldMember.roles.cache.equals(newMember.roles.cache)) {
36 await this.client.logger.logMemberRoleUpdate(oldMember, newMember);
37 }
38
39 if (!oldMember.communicationDisabledUntil && newMember.communicationDisabledUntil) {
40 setTimeout(async () => {
41 try {
42 const auditLog = (
43 await newMember.guild.fetchAuditLogs({
44 limit: 1,
45 type: AuditLogEvent.MemberUpdate
46 })
47 ).entries.first();
48
49 if (auditLog?.executor?.id && auditLog.executor.id !== this.client.user?.id) {
50 const timeout = auditLog.changes.reverse().find(change => change.key === "communication_disabled_until");
51
52 if (!timeout) return;
53
54 const infraction = await this.client.prisma.infraction.create({
55 data: {
56 guildId: newMember.guild.id,
57 moderatorId: auditLog.executor.id,
58 type: InfractionType.TIMEOUT,
59 userId: newMember.user.id,
60 reason: auditLog.reason ?? undefined
61 }
62 });
63
64 await this.client.logger.logMemberTimeout(newMember, {
65 moderator: auditLog.executor,
66 id: infraction.id.toString(),
67 reason: auditLog.reason ?? undefined
68 });
69 }
70 } catch (e) {
71 logError(e);
72 }
73 }, 3500);
74 } else if (oldMember.communicationDisabledUntil && !newMember.communicationDisabledUntil) {
75 setTimeout(async () => {
76 try {
77 const auditLog = (
78 await newMember.guild.fetchAuditLogs({
79 limit: 1,
80 type: AuditLogEvent.MemberUpdate
81 })
82 ).entries.first();
83
84 if (auditLog?.executor?.id && auditLog.executor.id !== this.client.user?.id) {
85 const infraction = await this.client.prisma.infraction.create({
86 data: {
87 guildId: newMember.guild.id,
88 moderatorId: auditLog.executor.id,
89 type: InfractionType.TIMEOUT_REMOVE,
90 userId: newMember.user.id
91 }
92 });
93
94 await this.client.logger.logMemberTimeoutRemove(newMember, {
95 moderator: auditLog.executor,
96 id: infraction.id.toString()
97 });
98 }
99 } catch (e) {
100 logError(e);
101 }
102 }, 2000);
103 }
104 }
105 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26