/[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 51 by rakin, Mon Jul 29 17:28:23 2024 UTC revision 194 by rakin, Mon Jul 29 17:28:58 2024 UTC
# Line 9  import History from '../../automod/Histo Line 9  import History from '../../automod/Histo
9  import getMember from '../../utils/getMember';  import getMember from '../../utils/getMember';
10  import ms from 'ms';  import ms from 'ms';
11  import { unmute } from './UnmuteCommand';  import { unmute } from './UnmuteCommand';
12    import PunishmentType from '../../types/PunishmentType';
13    import { hasPermission, shouldNotModerate } from '../../utils/util';
14    
15  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) {
16      try {      try {
17          if (dateTime) {          const { default: Punishment } = await import('../../models/Punishment');
18            
19            const { getTimeouts, clearTimeoutv2, setTimeoutv2 } = await import('../../utils/setTimeout');
20    
21            const timeouts = getTimeouts();
22            
23            for (const timeout of timeouts.values()) {
24                if (timeout.row.params) {
25                    try {
26                        const json = JSON.parse(timeout.row.params);
27    
28                        if (json) {
29                            if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
30                                await clearTimeoutv2(timeout);
31                            }
32                        }
33                    }
34                    catch (e) {
35                        console.log(e);                    
36                    }
37                }
38            }
39    
40            if (dateTime && timeInterval) {
41              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 client.db.get("INSERT INTO unmutes(user_id, guild_id, time) VALUES(?, ?, ?)", [user.id, msg.guild!.id, new Date(dateTime).toISOString()], async (err: any) => {
42                  if (err)                  if (err)
43                      console.log(err);                      console.log(err);
44                                    
45                      console.log('A timeout has been set.');                      console.log('A timeout has been set.');
46    
47                      setTimeout(async () => {                      await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);
48                          await client.db.get("SELECT * FROM unmutes WHERE time = ?", [new Date(dateTime!).toISOString()], async (err: any, data: any) => {              });
49                              if (err)          }
50                                  console.log(err);          
51                                        if (hard) {
52                              if (data) {              const { default: Hardmute } = await import("../../models/Hardmute");
53                                  await client.db.get('DELETE FROM unmutes WHERE id = ?', [data.id], async (err: any) => {              const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id);
54                                      let guild = await client.guilds.cache.find(g => g.id === data.guild_id);              await user.roles.remove(roles, reason);
55                                      let member = await guild?.members.cache.find(m => m.id === data.user_id);  
56                        await Hardmute.create({
57                                      if (member) {                  user_id: user.id,
58                                          await unmute(client, member, msg, client.user!);                  roles: roles.map(role => role.id),
59                                          await History.create(member.id, msg.guild!, 'unmute', client.user!.id, null);                  guild_id: msg.guild!.id,
                                     }  
           
                                     console.log(data);  
                                 });  
                             }  
                         });  
                     }, timeInterval);  
60              });              });
61          }          }
62    
63          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
64          await user.roles.add(role!);          await user.roles.add(role!, reason);
65          await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);  
66          await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);          await Punishment.create({
67          await user.send({              type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
68              embeds: [              user_id: user.id,
69                  new MessageEmbed()              guild_id: msg.guild!.id,
70                  .setAuthor({              mod_id: msg.member!.user.id,
71                      iconURL: <string> msg.guild!.iconURL(),              mod_tag: (msg.member!.user as User).tag,
72                      name: `\tYou have been muted in ${msg.guild!.name}`              reason,
73                  })              meta: {
74                  .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)                  time: timeInterval ? ms(timeInterval) : undefined
75              ]              }
76          });          });
77            
78            await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
79    
80            try {
81                await user.send({
82                    embeds: [
83                        new MessageEmbed()
84                        .setAuthor({
85                            iconURL: <string> msg.guild!.iconURL(),
86                            name: `\tYou have been muted in ${msg.guild!.name}`
87                        })
88                        .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
89                    ]
90                });
91            }
92            catch (e) {
93                console.log(e);
94            }
95      }      }
96      catch (e) {      catch (e) {
97          console.log(e);          console.log(e);
98                    
99          await msg.reply({          if (msg instanceof Message)
100              embeds: [              await msg.reply({
101                  new MessageEmbed()                  embeds: [
102                  .setColor('#f14a60')                      new MessageEmbed()
103                  .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')
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            else
108                await msg.editReply({
109                    embeds: [
110                        new MessageEmbed()
111                        .setColor('#f14a60')
112                        .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
113                    ]
114                });
115    
116          return;          return;
117      }      }
# Line 92  export default class MuteCommand extends Line 137  export default class MuteCommand extends
137              return;              return;
138          }          }
139    
140            if (msg instanceof CommandInteraction)
141                await msg.deferReply();
142    
143          let user: GuildMember;          let user: GuildMember;
144          let reason: string | undefined;          let reason: string | undefined;
145          let time: string | undefined;          let time: string | undefined;
146          let timeInterval: number | undefined;          let timeInterval: number | undefined;
147          let dateTime: number | undefined;          let dateTime: number | undefined;
148            let hard: boolean = false;
149    
150          if (options.isInteraction) {          if (options.isInteraction) {
151              user = await <GuildMember> options.options.getMember('member');              user = await <GuildMember> options.options.getMember('member');
# Line 105  export default class MuteCommand extends Line 154  export default class MuteCommand extends
154                  reason = await <string> options.options.getString('reason');                  reason = await <string> options.options.getString('reason');
155              }              }
156    
157                if (options.options.getBoolean('hardmute')) {
158                    hard = await <boolean> options.options.getBoolean('hardmute');
159                }
160    
161              if (options.options.getString('time')) {              if (options.options.getString('time')) {
162                  time = await options.options.getString('time') as string;                  time = await options.options.getString('time') as string;
163                  timeInterval = await ms(time);                  timeInterval = await ms(time);
164    
165                  if (!timeInterval) {                  if (!timeInterval) {
166                      await msg.reply({                      await this.deferReply(msg, {
167                          embeds: [                          embeds: [
168                              new MessageEmbed()                              new MessageEmbed()
169                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 126  export default class MuteCommand extends Line 179  export default class MuteCommand extends
179              const user2 = await getMember((msg as Message), options);              const user2 = await getMember((msg as Message), options);
180    
181              if (!user2) {              if (!user2) {
182                  await msg.reply({                  await this.deferReply(msg, {
183                      embeds: [                      embeds: [
184                          new MessageEmbed()                          new MessageEmbed()
185                          .setColor('#f14a60')                          .setColor('#f14a60')
# Line 146  export default class MuteCommand extends Line 199  export default class MuteCommand extends
199                  args.shift();                  args.shift();
200    
201                  if (index !== -1) {                  if (index !== -1) {
202                      args.splice(index - 1, 2)                      args.splice(index - 1, 2);
203                  }                  }
204    
205                  reason = await args.join(' ');                  reason = await args.join(' ');
# Line 156  export default class MuteCommand extends Line 209  export default class MuteCommand extends
209                  time = await options.args[index + 1];                  time = await options.args[index + 1];
210    
211                  if (time === undefined) {                  if (time === undefined) {
212                      await msg.reply({                      await this.deferReply(msg, {
213                          embeds: [                          embeds: [
214                              new MessageEmbed()                              new MessageEmbed()
215                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 168  export default class MuteCommand extends Line 221  export default class MuteCommand extends
221                  }                  }
222    
223                  if (!ms(time)) {                  if (!ms(time)) {
224                      await msg.reply({                      await this.deferReply(msg, {
225                          embeds: [                          embeds: [
226                              new MessageEmbed()                              new MessageEmbed()
227                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 187  export default class MuteCommand extends Line 240  export default class MuteCommand extends
240              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
241          }          }
242    
243          await mute(client, dateTime, user, msg, timeInterval, reason);          if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
244                return;
245            }
246            
247            if (shouldNotModerate(client, user)) {
248                await msg.reply({
249                    embeds: [
250                        {
251                            description: "This user cannot be muted."
252                        }
253                    ]
254                });
255    
256                return;
257            }
258            
259            await mute(client, dateTime, user, msg, timeInterval, reason, hard);
260    
261          const fields = [          const fields = [
262              {              {
# Line 201  export default class MuteCommand extends Line 270  export default class MuteCommand extends
270              {              {
271                  name: "Duration",                  name: "Duration",
272                  value: time === undefined ? "*No duration set*" : (time + '')                  value: time === undefined ? "*No duration set*" : (time + '')
273                },
274                {
275                    name: "Role Takeout",
276                    value: hard ? 'Yes' : 'No'
277              }              }
278          ];          ];
279    
280          console.log(fields);                  console.log(fields);        
281    
282          await msg.reply({          await this.deferReply(msg, {
283              embeds: [              embeds: [
284                  new MessageEmbed()                  new MessageEmbed()
285                  .setAuthor({                  .setAuthor({
# Line 218  export default class MuteCommand extends Line 291  export default class MuteCommand extends
291              ]              ]
292          });          });
293      }      }
 }  
294    }

Legend:
Removed from v.51  
changed lines
  Added in v.194

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26