/[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 153 by rakin, Mon Jul 29 17:28:48 2024 UTC
# Line 10  import getMember from '../../utils/getMe Line 10  import getMember from '../../utils/getMe
10  import ms from 'ms';  import ms from 'ms';
11  import { unmute } from './UnmuteCommand';  import { unmute } from './UnmuteCommand';
12  import PunishmentType from '../../types/PunishmentType';  import PunishmentType from '../../types/PunishmentType';
13    import { 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          const { default: Punishment } = await import('../../models/Punishment');          const { default: Punishment } = await import('../../models/Punishment');
18                    
19          if (dateTime) {          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    
66          await Punishment.create({          await Punishment.create({
67              type: PunishmentType.MUTE,              type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
68              user_id: user.id,              user_id: user.id,
69              guild_id: msg.guild!.id,              guild_id: msg.guild!.id,
70              mod_id: msg.member!.user.id,              mod_id: msg.member!.user.id,
# Line 59  export async function mute(client: Disco Line 74  export async function mute(client: Disco
74                  time: timeInterval ? ms(timeInterval) : undefined                  time: timeInterval ? ms(timeInterval) : undefined
75              }              }
76          });          });
77            
78          await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);          await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
         await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);  
79          await user.send({          await user.send({
80              embeds: [              embeds: [
81                  new MessageEmbed()                  new MessageEmbed()
# Line 76  export async function mute(client: Disco Line 90  export async function mute(client: Disco
90      catch (e) {      catch (e) {
91          console.log(e);          console.log(e);
92                    
93          await msg.reply({          if (msg instanceof Message)
94              embeds: [              await msg.reply({
95                  new MessageEmbed()                  embeds: [
96                  .setColor('#f14a60')                      new MessageEmbed()
97                  .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')
98              ]                      .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
99          });                  ]
100                });
101            else
102                await msg.editReply({
103                    embeds: [
104                        new MessageEmbed()
105                        .setColor('#f14a60')
106                        .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
107                    ]
108                });
109    
110          return;          return;
111      }      }
# Line 108  export default class MuteCommand extends Line 131  export default class MuteCommand extends
131              return;              return;
132          }          }
133    
134            if (msg instanceof CommandInteraction)
135                await msg.deferReply();
136    
137          let user: GuildMember;          let user: GuildMember;
138          let reason: string | undefined;          let reason: string | undefined;
139          let time: string | undefined;          let time: string | undefined;
140          let timeInterval: number | undefined;          let timeInterval: number | undefined;
141          let dateTime: number | undefined;          let dateTime: number | undefined;
142            let hard: boolean = false;
143    
144          if (options.isInteraction) {          if (options.isInteraction) {
145              user = await <GuildMember> options.options.getMember('member');              user = await <GuildMember> options.options.getMember('member');
# Line 121  export default class MuteCommand extends Line 148  export default class MuteCommand extends
148                  reason = await <string> options.options.getString('reason');                  reason = await <string> options.options.getString('reason');
149              }              }
150    
151                if (options.options.getBoolean('hardmute')) {
152                    hard = await <boolean> options.options.getBoolean('hardmute');
153                }
154    
155              if (options.options.getString('time')) {              if (options.options.getString('time')) {
156                  time = await options.options.getString('time') as string;                  time = await options.options.getString('time') as string;
157                  timeInterval = await ms(time);                  timeInterval = await ms(time);
158    
159                  if (!timeInterval) {                  if (!timeInterval) {
160                      await msg.reply({                      await this.deferReply(msg, {
161                          embeds: [                          embeds: [
162                              new MessageEmbed()                              new MessageEmbed()
163                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 142  export default class MuteCommand extends Line 173  export default class MuteCommand extends
173              const user2 = await getMember((msg as Message), options);              const user2 = await getMember((msg as Message), options);
174    
175              if (!user2) {              if (!user2) {
176                  await msg.reply({                  await this.deferReply(msg, {
177                      embeds: [                      embeds: [
178                          new MessageEmbed()                          new MessageEmbed()
179                          .setColor('#f14a60')                          .setColor('#f14a60')
# Line 162  export default class MuteCommand extends Line 193  export default class MuteCommand extends
193                  args.shift();                  args.shift();
194    
195                  if (index !== -1) {                  if (index !== -1) {
196                      args.splice(index - 1, 2)                      args.splice(index - 1, 2);
197                  }                  }
198    
199                  reason = await args.join(' ');                  reason = await args.join(' ');
# Line 172  export default class MuteCommand extends Line 203  export default class MuteCommand extends
203                  time = await options.args[index + 1];                  time = await options.args[index + 1];
204    
205                  if (time === undefined) {                  if (time === undefined) {
206                      await msg.reply({                      await this.deferReply(msg, {
207                          embeds: [                          embeds: [
208                              new MessageEmbed()                              new MessageEmbed()
209                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 184  export default class MuteCommand extends Line 215  export default class MuteCommand extends
215                  }                  }
216    
217                  if (!ms(time)) {                  if (!ms(time)) {
218                      await msg.reply({                      await this.deferReply(msg, {
219                          embeds: [                          embeds: [
220                              new MessageEmbed()                              new MessageEmbed()
221                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 203  export default class MuteCommand extends Line 234  export default class MuteCommand extends
234              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
235          }          }
236    
237          await mute(client, dateTime, user, msg, timeInterval, reason);          if (shouldNotModerate(client, user)) {
238                await msg.reply({
239                    embeds: [
240                        {
241                            description: "This user cannot be muted."
242                        }
243                    ]
244                });
245    
246                return;
247            }
248            
249            await mute(client, dateTime, user, msg, timeInterval, reason, hard);
250    
251          const fields = [          const fields = [
252              {              {
# Line 217  export default class MuteCommand extends Line 260  export default class MuteCommand extends
260              {              {
261                  name: "Duration",                  name: "Duration",
262                  value: time === undefined ? "*No duration set*" : (time + '')                  value: time === undefined ? "*No duration set*" : (time + '')
263                },
264                {
265                    name: "Role Takeout",
266                    value: hard ? 'Yes' : 'No'
267              }              }
268          ];          ];
269    
270          console.log(fields);                  console.log(fields);        
271    
272          await msg.reply({          await this.deferReply(msg, {
273              embeds: [              embeds: [
274                  new MessageEmbed()                  new MessageEmbed()
275                  .setAuthor({                  .setAuthor({
# Line 234  export default class MuteCommand extends Line 281  export default class MuteCommand extends
281              ]              ]
282          });          });
283      }      }
 }  
284    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26