/[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 106 by rakin, Mon Jul 29 17:28:37 2024 UTC revision 194 by rakin, Mon Jul 29 17:28:58 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 { 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          const { default: Punishment } = await import('../../models/Punishment');          const { default: Punishment } = await import('../../models/Punishment');
18                    
# Line 47  export async function mute(client: Disco Line 48  export async function mute(client: Disco
48              });              });
49          }          }
50                    
51            if (hard) {
52                const { default: Hardmute } = await import("../../models/Hardmute");
53                const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id);
54                await user.roles.remove(roles, reason);
55    
56                await Hardmute.create({
57                    user_id: user.id,
58                    roles: roles.map(role => role.id),
59                    guild_id: msg.guild!.id,
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 61  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 client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
79    
80          await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);          try {
81          await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);              await user.send({
82          await user.send({                  embeds: [
83              embeds: [                      new MessageEmbed()
84                  new MessageEmbed()                      .setAuthor({
85                  .setAuthor({                          iconURL: <string> msg.guild!.iconURL(),
86                      iconURL: <string> msg.guild!.iconURL(),                          name: `\tYou have been muted in ${msg.guild!.name}`
87                      name: `\tYou have been muted in ${msg.guild!.name}`                      })
88                  })                      .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
89                  .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)                  ]
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 110  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 123  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 144  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 164  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 174  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 186  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 205  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 219  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 236  export default class MuteCommand extends Line 291  export default class MuteCommand extends
291              ]              ]
292          });          });
293      }      }
 }  
294    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26