/[sudobot]/trunk/src/commands/moderation/MuteCommand.ts
ViewVC logotype

Diff of /trunk/src/commands/moderation/MuteCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 86 by rakin, Mon Jul 29 17:28:32 2024 UTC revision 430 by rakin, Mon Jul 29 17:30:12 2024 UTC
# Line 1  Line 1 
1  import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js';  /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
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 { CommandInteraction, Guild, GuildMember, Message, Permissions, User } from 'discord.js';
21  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
22  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
23  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
24  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
25  import MessageEmbed from '../../client/MessageEmbed';  import MessageEmbed from '../../client/MessageEmbed';
 import getUser from '../../utils/getUser';  
 import History from '../../automod/History';  
26  import getMember from '../../utils/getMember';  import getMember from '../../utils/getMember';
27  import ms from 'ms';  import ms from 'ms';
 import { unmute } from './UnmuteCommand';  
28  import PunishmentType from '../../types/PunishmentType';  import PunishmentType from '../../types/PunishmentType';
29    import { hasPermission, shouldNotModerate } from '../../utils/util';
30    import UnmuteQueue from '../../queues/UnmuteQueue';
31    
32  export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction, timeInterval: number | undefined, reason: string | undefined) {  export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction | { guild: Guild, member: GuildMember, editReply?: undefined }, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) {
33      try {      try {
34          const { default: Punishment } = await import('../../models/Punishment');          const { default: Punishment } = await import('../../models/Punishment');
35                    
36          if (dateTime) {          const { getTimeouts, clearTimeoutv2, setTimeoutv2 } = await import('../../utils/setTimeout');
37              await client.db.get("INSERT INTO unmutes(user_id, guild_id, time) VALUES(?, ?, ?)", [user.id, msg.guild!.id, new Date(dateTime).toISOString()], async (err: any) => {  
38                  if (err)          const timeouts = getTimeouts();
39                      console.log(err);          
40                            for (const timeout of timeouts.values()) {
41                      console.log('A timeout has been set.');              if (timeout.row.params) {
42                    try {
43                      setTimeout(async () => {                      const json = JSON.parse(timeout.row.params);
44                          await client.db.get("SELECT * FROM unmutes WHERE time = ?", [new Date(dateTime!).toISOString()], async (err: any, data: any) => {  
45                              if (err)                      if (json) {
46                                  console.log(err);                          if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
47                                                            await clearTimeoutv2(timeout);
48                              if (data) {                          }
49                                  await client.db.get('DELETE FROM unmutes WHERE id = ?', [data.id], async (err: any) => {                      }
50                                      let guild = await client.guilds.cache.find(g => g.id === data.guild_id);                  }
51                                      let member = await guild?.members.cache.find(m => m.id === data.user_id);                  catch (e) {
52                                console.log(e);                    
53                                      if (member) {                  }
54                                          await unmute(client, member, msg, client.user!);              }
55                                          await History.create(member.id, msg.guild!, 'unmute', client.user!.id, null);          }
56                                      }  
57                    if (dateTime && timeInterval) {
58                                      console.log(data);              // await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);
59                                  });              for await (const queue of client.queueManager.queues.values()) {
60                              }                  if (queue instanceof UnmuteQueue && queue.data!.memberID === user.id && queue.data!.guildID === msg.guild!.id) {
61                          });                      await queue.cancel();
62                      }, timeInterval);                  }
63                }
64    
65                await client.queueManager.addQueue(UnmuteQueue, {
66                    data: {
67                        guildID: msg.guild!.id,
68                        memberID: user.id
69                    },
70                    runAt: new Date(Date.now() + timeInterval),
71                    guild: msg.guild!.id
72                });
73            }
74            
75            if (hard) {
76                const { default: Hardmute } = await import("../../models/Hardmute");
77                const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id);
78                await user.roles.remove(roles, reason);
79    
80                await Hardmute.create({
81                    user_id: user.id,
82                    roles: roles.map(role => role.id),
83                    guild_id: msg.guild!.id,
84                    createdAt: new Date()
85              });              });
86          }          }
87    
88          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));          const role = await msg.guild!.roles.fetch(client.config.props[msg.guild!.id].mute_role);
89          await user.roles.add(role!);          await user.roles.add(role!, reason);
90    
91          await Punishment.create({          await Punishment.create({
92              type: PunishmentType.MUTE,              type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
93              user_id: user.id,              user_id: user.id,
94              guild_id: msg.guild!.id,              guild_id: msg.guild!.id,
95              mod_id: msg.member!.user.id,              mod_id: msg.member!.user.id,
# Line 57  export async function mute(client: Disco Line 97  export async function mute(client: Disco
97              reason,              reason,
98              meta: {              meta: {
99                  time: timeInterval ? ms(timeInterval) : undefined                  time: timeInterval ? ms(timeInterval) : undefined
100              }              },
101                createdAt: new Date()
102          });          });
103            
104            await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
105    
106          await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);          try {
107          await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);              await user.send({
108          await user.send({                  embeds: [
109              embeds: [                      new MessageEmbed()
110                  new MessageEmbed()                      .setAuthor({
111                  .setAuthor({                          iconURL: <string> msg.guild!.iconURL(),
112                      iconURL: <string> msg.guild!.iconURL(),                          name: `\tYou have been muted in ${msg.guild!.name}`
113                      name: `\tYou have been muted in ${msg.guild!.name}`                      })
114                  })                      .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
115                  .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)                  ]
116              ]              });
117          });          }
118            catch (e) {
119                console.log(e);
120            }
121      }      }
122      catch (e) {      catch (e) {
123          console.log(e);          console.log(e);
124                    
125          await msg.reply({          if (msg instanceof Message)
126              embeds: [              await msg.reply({
127                  new MessageEmbed()                  embeds: [
128                  .setColor('#f14a60')                      new MessageEmbed()
129                  .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")                      .setColor('#f14a60')
130              ]                      .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
131          });                  ]
132                });
133            else if (msg.editReply)
134                await msg.editReply({
135                    embeds: [
136                        new MessageEmbed()
137                        .setColor('#f14a60')
138                        .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
139                    ]
140                });
141    
142          return;          return;
143      }      }
# Line 90  export async function mute(client: Disco Line 145  export async function mute(client: Disco
145    
146  export default class MuteCommand extends BaseCommand {  export default class MuteCommand extends BaseCommand {
147      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
148        permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
149    
150      constructor() {      constructor() {
151          super('mute', 'moderation', []);          super('mute', 'moderation', []);
# Line 108  export default class MuteCommand extends Line 164  export default class MuteCommand extends
164              return;              return;
165          }          }
166    
167            if (msg instanceof CommandInteraction)
168                await msg.deferReply();
169    
170          let user: GuildMember;          let user: GuildMember;
171          let reason: string | undefined;          let reason: string | undefined;
172          let time: string | undefined;          let time: string | undefined;
173          let timeInterval: number | undefined;          let timeInterval: number | undefined;
174          let dateTime: number | undefined;          let dateTime: number | undefined;
175            let hard: boolean = false;
176    
177          if (options.isInteraction) {          if (options.isInteraction) {
178              user = await <GuildMember> options.options.getMember('member');              user = await <GuildMember> options.options.getMember('member');
# Line 121  export default class MuteCommand extends Line 181  export default class MuteCommand extends
181                  reason = await <string> options.options.getString('reason');                  reason = await <string> options.options.getString('reason');
182              }              }
183    
184                if (options.options.getBoolean('hardmute')) {
185                    hard = await <boolean> options.options.getBoolean('hardmute');
186                }
187    
188              if (options.options.getString('time')) {              if (options.options.getString('time')) {
189                  time = await options.options.getString('time') as string;                  time = await options.options.getString('time') as string;
190                  timeInterval = await ms(time);                  timeInterval = await ms(time);
191    
192                  if (!timeInterval) {                  if (!timeInterval) {
193                      await msg.reply({                      await this.deferReply(msg, {
194                          embeds: [                          embeds: [
195                              new MessageEmbed()                              new MessageEmbed()
196                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 142  export default class MuteCommand extends Line 206  export default class MuteCommand extends
206              const user2 = await getMember((msg as Message), options);              const user2 = await getMember((msg as Message), options);
207    
208              if (!user2) {              if (!user2) {
209                  await msg.reply({                  await this.deferReply(msg, {
210                      embeds: [                      embeds: [
211                          new MessageEmbed()                          new MessageEmbed()
212                          .setColor('#f14a60')                          .setColor('#f14a60')
# Line 162  export default class MuteCommand extends Line 226  export default class MuteCommand extends
226                  args.shift();                  args.shift();
227    
228                  if (index !== -1) {                  if (index !== -1) {
229                      args.splice(index - 1, 2)                      args.splice(index - 1, 2);
230                  }                  }
231    
232                  reason = await args.join(' ');                  reason = await args.join(' ');
# Line 172  export default class MuteCommand extends Line 236  export default class MuteCommand extends
236                  time = await options.args[index + 1];                  time = await options.args[index + 1];
237    
238                  if (time === undefined) {                  if (time === undefined) {
239                      await msg.reply({                      await this.deferReply(msg, {
240                          embeds: [                          embeds: [
241                              new MessageEmbed()                              new MessageEmbed()
242                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 184  export default class MuteCommand extends Line 248  export default class MuteCommand extends
248                  }                  }
249    
250                  if (!ms(time)) {                  if (!ms(time)) {
251                      await msg.reply({                      await this.deferReply(msg, {
252                          embeds: [                          embeds: [
253                              new MessageEmbed()                              new MessageEmbed()
254                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 203  export default class MuteCommand extends Line 267  export default class MuteCommand extends
267              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
268          }          }
269    
270          await mute(client, dateTime, user, msg, timeInterval, reason);          if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
271                return;
272            }
273            
274            if (shouldNotModerate(client, user)) {
275                await msg.reply({
276                    embeds: [
277                        {
278                            description: "This user cannot be muted."
279                        }
280                    ]
281                });
282    
283                return;
284            }
285            
286            await mute(client, dateTime, user, msg, timeInterval, reason, hard);
287    
288          const fields = [          const fields = [
289              {              {
# Line 217  export default class MuteCommand extends Line 297  export default class MuteCommand extends
297              {              {
298                  name: "Duration",                  name: "Duration",
299                  value: time === undefined ? "*No duration set*" : (time + '')                  value: time === undefined ? "*No duration set*" : (time + '')
300                },
301                {
302                    name: "Role Takeout",
303                    value: hard ? 'Yes' : 'No'
304              }              }
305          ];          ];
306    
307          console.log(fields);                  console.log(fields);        
308    
309          await msg.reply({          await this.deferReply(msg, {
310              embeds: [              embeds: [
311                  new MessageEmbed()                  new MessageEmbed()
312                  .setAuthor({                  .setAuthor({
# Line 234  export default class MuteCommand extends Line 318  export default class MuteCommand extends
318              ]              ]
319          });          });
320      }      }
 }  
321    }

Legend:
Removed from v.86  
changed lines
  Added in v.430

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26