/[sudobot]/branches/7.x/src/services/QuickMuteService.ts
ViewVC logotype

Contents of /branches/7.x/src/services/QuickMuteService.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: 4659 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 { ChannelType, GuildMember, MessageReaction, PartialMessageReaction, PartialUser, TextChannel, User } from "discord.js";
21 import Service from "../core/Service";
22 import { GatewayEventListener } from "../decorators/GatewayEventListener";
23 import { HasEventListeners } from "../types/HasEventListeners";
24 import { log, logError, logInfo, logWarn } from "../utils/logger";
25
26 export const name = "quickmute";
27
28 export default class QuickMuteService extends Service implements HasEventListeners {
29 @GatewayEventListener("messageReactionAdd")
30 async onMessageReactionAdd(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser) {
31 if (!reaction || !reaction.message.guild || reaction.message.channel.type === ChannelType.DM) {
32 return;
33 }
34
35 const result = await this.handle(reaction, user);
36
37 if (result === false) {
38 await reaction.remove().catch(logError);
39 }
40 }
41
42 async handle(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser) {
43 log("Reaction");
44
45 const config = this.client.configManager.config[reaction.message.guildId!]?.quickmute;
46
47 log(config);
48
49 if (!config?.enabled || !config?.clear_emoji || !config?.noclear_emoji) {
50 return;
51 }
52
53 log("Reaction 2");
54
55 if (
56 reaction.emoji.id !== config.clear_emoji &&
57 reaction.emoji.identifier !== config.clear_emoji &&
58 reaction.emoji.id !== config.noclear_emoji &&
59 reaction.emoji.identifier !== config.noclear_emoji
60 ) {
61 return;
62 }
63
64 logInfo("Quickmute trigger reaction receieved.");
65
66 const clearMessages = reaction.emoji.id === config.clear_emoji || reaction.emoji.identifier === config.clear_emoji;
67 const guild = this.client.guilds.cache.get(reaction.message.guildId!);
68
69 if (!guild) {
70 logWarn("Could not resolve guild");
71 return;
72 }
73
74 if (reaction.message.author!.id === this.client.user!.id) {
75 log("Cannot mute the bot itself");
76 return false;
77 }
78
79 let moderator: GuildMember | undefined = undefined;
80
81 try {
82 moderator = guild.members.cache.get(user.id) ?? (await guild.members.fetch(user.id));
83 } catch (e) {
84 logError(e);
85 return;
86 }
87
88 if (!moderator) {
89 return;
90 }
91
92 const { permissions } = await this.client.permissionManager.getMemberPermissions(moderator, true);
93
94 if (!permissions.has("Administrator") && !permissions.has("ManageMessages") && !permissions.has("ModerateMembers")) {
95 logInfo("This moderator does not have permission to use quickmute!");
96 return false;
97 }
98
99 const member = reaction.message.member!;
100
101 if (!(await this.client.permissionManager.shouldModerate(member, moderator))) {
102 logInfo("This moderator does not have permission to mute this user!");
103 return false;
104 }
105
106 await this.client.infractionManager.createMemberMute(member, {
107 guild,
108 moderator: user as User,
109 autoRemoveQueue: true,
110 bulkDeleteReason: clearMessages
111 ? "Clearing recent messages from user, because the quickmute trigger was used."
112 : undefined,
113 duration: config.duration ?? 1000 * 60 * 60 * 2,
114 messageChannel: clearMessages ? (reaction.message.channel! as TextChannel) : undefined,
115 notifyUser: true,
116 sendLog: true,
117 reason:
118 config.reason ??
119 "You have violated the rules or guidelines of the server. Please take a break, and come back later."
120 });
121
122 if (!clearMessages) {
123 await reaction.remove().catch(logError);
124 }
125
126 return true;
127 }
128 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26