/[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 102 by rakin, Mon Jul 29 17:28:36 2024 UTC revision 342 by rakin, Mon Jul 29 17:29:39 2024 UTC
# Line 1  Line 1 
1  import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js';  import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, Permissions, User } from 'discord.js';
2  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
3  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
4  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
5  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
6  import MessageEmbed from '../../client/MessageEmbed';  import MessageEmbed from '../../client/MessageEmbed';
 import getUser from '../../utils/getUser';  
 import History from '../../automod/History';  
7  import getMember from '../../utils/getMember';  import getMember from '../../utils/getMember';
8  import ms from 'ms';  import ms from 'ms';
 import { unmute } from './UnmuteCommand';  
9  import PunishmentType from '../../types/PunishmentType';  import PunishmentType from '../../types/PunishmentType';
10    import { hasPermission, shouldNotModerate } from '../../utils/util';
11    
12  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, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) {
13      try {      try {
14          const { default: Punishment } = await import('../../models/Punishment');          const { default: Punishment } = await import('../../models/Punishment');
15                    
# Line 25  export async function mute(client: Disco Line 23  export async function mute(client: Disco
23                      const json = JSON.parse(timeout.row.params);                      const json = JSON.parse(timeout.row.params);
24    
25                      if (json) {                      if (json) {
26                          if (json[1] === user.id) {                          if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
27                              await clearTimeoutv2(timeout);                              await clearTimeoutv2(timeout);
28                          }                          }
29                      }                      }
# Line 37  export async function mute(client: Disco Line 35  export async function mute(client: Disco
35          }          }
36    
37          if (dateTime && timeInterval) {          if (dateTime && timeInterval) {
38              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) => {              await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);
                 if (err)  
                     console.log(err);  
                   
                     console.log('A timeout has been set.');  
   
                     await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);  
             });  
39          }          }
40                    
41            if (hard) {
42                const { default: Hardmute } = await import("../../models/Hardmute");
43                const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id);
44                await user.roles.remove(roles, reason);
45    
46                await Hardmute.create({
47                    user_id: user.id,
48                    roles: roles.map(role => role.id),
49                    guild_id: msg.guild!.id,
50                    createdAt: new Date()
51                });
52            }
53    
54          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
55          await user.roles.add(role!);          await user.roles.add(role!, reason);
56    
57          await Punishment.create({          await Punishment.create({
58              type: PunishmentType.MUTE,              type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
59              user_id: user.id,              user_id: user.id,
60              guild_id: msg.guild!.id,              guild_id: msg.guild!.id,
61              mod_id: msg.member!.user.id,              mod_id: msg.member!.user.id,
# Line 59  export async function mute(client: Disco Line 63  export async function mute(client: Disco
63              reason,              reason,
64              meta: {              meta: {
65                  time: timeInterval ? ms(timeInterval) : undefined                  time: timeInterval ? ms(timeInterval) : undefined
66              }              },
67                createdAt: new Date()
68          });          });
69            
70            await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
71    
72          await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);          try {
73          await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);              await user.send({
74          await user.send({                  embeds: [
75              embeds: [                      new MessageEmbed()
76                  new MessageEmbed()                      .setAuthor({
77                  .setAuthor({                          iconURL: <string> msg.guild!.iconURL(),
78                      iconURL: <string> msg.guild!.iconURL(),                          name: `\tYou have been muted in ${msg.guild!.name}`
79                      name: `\tYou have been muted in ${msg.guild!.name}`                      })
80                  })                      .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
81                  .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)                  ]
82              ]              });
83          });          }
84            catch (e) {
85                console.log(e);
86            }
87      }      }
88      catch (e) {      catch (e) {
89          console.log(e);          console.log(e);
90                    
91          await msg.reply({          if (msg instanceof Message)
92              embeds: [              await msg.reply({
93                  new MessageEmbed()                  embeds: [
94                  .setColor('#f14a60')                      new MessageEmbed()
95                  .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')
96              ]                      .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
97          });                  ]
98                });
99            else
100                await msg.editReply({
101                    embeds: [
102                        new MessageEmbed()
103                        .setColor('#f14a60')
104                        .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
105                    ]
106                });
107    
108          return;          return;
109      }      }
# Line 92  export async function mute(client: Disco Line 111  export async function mute(client: Disco
111    
112  export default class MuteCommand extends BaseCommand {  export default class MuteCommand extends BaseCommand {
113      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
114        permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
115    
116      constructor() {      constructor() {
117          super('mute', 'moderation', []);          super('mute', 'moderation', []);
# Line 110  export default class MuteCommand extends Line 130  export default class MuteCommand extends
130              return;              return;
131          }          }
132    
133            if (msg instanceof CommandInteraction)
134                await msg.deferReply();
135    
136          let user: GuildMember;          let user: GuildMember;
137          let reason: string | undefined;          let reason: string | undefined;
138          let time: string | undefined;          let time: string | undefined;
139          let timeInterval: number | undefined;          let timeInterval: number | undefined;
140          let dateTime: number | undefined;          let dateTime: number | undefined;
141            let hard: boolean = false;
142    
143          if (options.isInteraction) {          if (options.isInteraction) {
144              user = await <GuildMember> options.options.getMember('member');              user = await <GuildMember> options.options.getMember('member');
# Line 123  export default class MuteCommand extends Line 147  export default class MuteCommand extends
147                  reason = await <string> options.options.getString('reason');                  reason = await <string> options.options.getString('reason');
148              }              }
149    
150                if (options.options.getBoolean('hardmute')) {
151                    hard = await <boolean> options.options.getBoolean('hardmute');
152                }
153    
154              if (options.options.getString('time')) {              if (options.options.getString('time')) {
155                  time = await options.options.getString('time') as string;                  time = await options.options.getString('time') as string;
156                  timeInterval = await ms(time);                  timeInterval = await ms(time);
157    
158                  if (!timeInterval) {                  if (!timeInterval) {
159                      await msg.reply({                      await this.deferReply(msg, {
160                          embeds: [                          embeds: [
161                              new MessageEmbed()                              new MessageEmbed()
162                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 144  export default class MuteCommand extends Line 172  export default class MuteCommand extends
172              const user2 = await getMember((msg as Message), options);              const user2 = await getMember((msg as Message), options);
173    
174              if (!user2) {              if (!user2) {
175                  await msg.reply({                  await this.deferReply(msg, {
176                      embeds: [                      embeds: [
177                          new MessageEmbed()                          new MessageEmbed()
178                          .setColor('#f14a60')                          .setColor('#f14a60')
# Line 164  export default class MuteCommand extends Line 192  export default class MuteCommand extends
192                  args.shift();                  args.shift();
193    
194                  if (index !== -1) {                  if (index !== -1) {
195                      args.splice(index - 1, 2)                      args.splice(index - 1, 2);
196                  }                  }
197    
198                  reason = await args.join(' ');                  reason = await args.join(' ');
# Line 174  export default class MuteCommand extends Line 202  export default class MuteCommand extends
202                  time = await options.args[index + 1];                  time = await options.args[index + 1];
203    
204                  if (time === undefined) {                  if (time === undefined) {
205                      await msg.reply({                      await this.deferReply(msg, {
206                          embeds: [                          embeds: [
207                              new MessageEmbed()                              new MessageEmbed()
208                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 186  export default class MuteCommand extends Line 214  export default class MuteCommand extends
214                  }                  }
215    
216                  if (!ms(time)) {                  if (!ms(time)) {
217                      await msg.reply({                      await this.deferReply(msg, {
218                          embeds: [                          embeds: [
219                              new MessageEmbed()                              new MessageEmbed()
220                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 205  export default class MuteCommand extends Line 233  export default class MuteCommand extends
233              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
234          }          }
235    
236          await mute(client, dateTime, user, msg, timeInterval, reason);          if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
237                return;
238            }
239            
240            if (shouldNotModerate(client, user)) {
241                await msg.reply({
242                    embeds: [
243                        {
244                            description: "This user cannot be muted."
245                        }
246                    ]
247                });
248    
249                return;
250            }
251            
252            await mute(client, dateTime, user, msg, timeInterval, reason, hard);
253    
254          const fields = [          const fields = [
255              {              {
# Line 219  export default class MuteCommand extends Line 263  export default class MuteCommand extends
263              {              {
264                  name: "Duration",                  name: "Duration",
265                  value: time === undefined ? "*No duration set*" : (time + '')                  value: time === undefined ? "*No duration set*" : (time + '')
266                },
267                {
268                    name: "Role Takeout",
269                    value: hard ? 'Yes' : 'No'
270              }              }
271          ];          ];
272    
273          console.log(fields);                  console.log(fields);        
274    
275          await msg.reply({          await this.deferReply(msg, {
276              embeds: [              embeds: [
277                  new MessageEmbed()                  new MessageEmbed()
278                  .setAuthor({                  .setAuthor({
# Line 236  export default class MuteCommand extends Line 284  export default class MuteCommand extends
284              ]              ]
285          });          });
286      }      }
 }  
287    }

Legend:
Removed from v.102  
changed lines
  Added in v.342

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26