/[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 333 by rakin, Mon Jul 29 17:29:35 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';
# 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                    createdAt: new Date()
61                });
62            }
63    
64          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
65          await user.roles.add(role!);          await user.roles.add(role!, reason);
66    
67          await Punishment.create({          await Punishment.create({
68              type: PunishmentType.MUTE,              type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
69              user_id: user.id,              user_id: user.id,
70              guild_id: msg.guild!.id,              guild_id: msg.guild!.id,
71              mod_id: msg.member!.user.id,              mod_id: msg.member!.user.id,
# Line 61  export async function mute(client: Disco Line 75  export async function mute(client: Disco
75                  time: timeInterval ? ms(timeInterval) : undefined                  time: timeInterval ? ms(timeInterval) : undefined
76              }              }
77          });          });
78            
79            await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
80    
81          await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);          try {
82          await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);              await user.send({
83          await user.send({                  embeds: [
84              embeds: [                      new MessageEmbed()
85                  new MessageEmbed()                      .setAuthor({
86                  .setAuthor({                          iconURL: <string> msg.guild!.iconURL(),
87                      iconURL: <string> msg.guild!.iconURL(),                          name: `\tYou have been muted in ${msg.guild!.name}`
88                      name: `\tYou have been muted in ${msg.guild!.name}`                      })
89                  })                      .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
90                  .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)                  ]
91              ]              });
92          });          }
93            catch (e) {
94                console.log(e);
95            }
96      }      }
97      catch (e) {      catch (e) {
98          console.log(e);          console.log(e);
99                    
100          await msg.reply({          if (msg instanceof Message)
101              embeds: [              await msg.reply({
102                  new MessageEmbed()                  embeds: [
103                  .setColor('#f14a60')                      new MessageEmbed()
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?")                      .setColor('#f14a60')
105              ]                      .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
106          });                  ]
107                });
108            else
109                await msg.editReply({
110                    embeds: [
111                        new MessageEmbed()
112                        .setColor('#f14a60')
113                        .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
114                    ]
115                });
116    
117          return;          return;
118      }      }
# Line 92  export async function mute(client: Disco Line 120  export async function mute(client: Disco
120    
121  export default class MuteCommand extends BaseCommand {  export default class MuteCommand extends BaseCommand {
122      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
123        permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
124    
125      constructor() {      constructor() {
126          super('mute', 'moderation', []);          super('mute', 'moderation', []);
# Line 110  export default class MuteCommand extends Line 139  export default class MuteCommand extends
139              return;              return;
140          }          }
141    
142            if (msg instanceof CommandInteraction)
143                await msg.deferReply();
144    
145          let user: GuildMember;          let user: GuildMember;
146          let reason: string | undefined;          let reason: string | undefined;
147          let time: string | undefined;          let time: string | undefined;
148          let timeInterval: number | undefined;          let timeInterval: number | undefined;
149          let dateTime: number | undefined;          let dateTime: number | undefined;
150            let hard: boolean = false;
151    
152          if (options.isInteraction) {          if (options.isInteraction) {
153              user = await <GuildMember> options.options.getMember('member');              user = await <GuildMember> options.options.getMember('member');
# Line 123  export default class MuteCommand extends Line 156  export default class MuteCommand extends
156                  reason = await <string> options.options.getString('reason');                  reason = await <string> options.options.getString('reason');
157              }              }
158    
159                if (options.options.getBoolean('hardmute')) {
160                    hard = await <boolean> options.options.getBoolean('hardmute');
161                }
162    
163              if (options.options.getString('time')) {              if (options.options.getString('time')) {
164                  time = await options.options.getString('time') as string;                  time = await options.options.getString('time') as string;
165                  timeInterval = await ms(time);                  timeInterval = await ms(time);
166    
167                  if (!timeInterval) {                  if (!timeInterval) {
168                      await msg.reply({                      await this.deferReply(msg, {
169                          embeds: [                          embeds: [
170                              new MessageEmbed()                              new MessageEmbed()
171                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 144  export default class MuteCommand extends Line 181  export default class MuteCommand extends
181              const user2 = await getMember((msg as Message), options);              const user2 = await getMember((msg as Message), options);
182    
183              if (!user2) {              if (!user2) {
184                  await msg.reply({                  await this.deferReply(msg, {
185                      embeds: [                      embeds: [
186                          new MessageEmbed()                          new MessageEmbed()
187                          .setColor('#f14a60')                          .setColor('#f14a60')
# Line 164  export default class MuteCommand extends Line 201  export default class MuteCommand extends
201                  args.shift();                  args.shift();
202    
203                  if (index !== -1) {                  if (index !== -1) {
204                      args.splice(index - 1, 2)                      args.splice(index - 1, 2);
205                  }                  }
206    
207                  reason = await args.join(' ');                  reason = await args.join(' ');
# Line 174  export default class MuteCommand extends Line 211  export default class MuteCommand extends
211                  time = await options.args[index + 1];                  time = await options.args[index + 1];
212    
213                  if (time === undefined) {                  if (time === undefined) {
214                      await msg.reply({                      await this.deferReply(msg, {
215                          embeds: [                          embeds: [
216                              new MessageEmbed()                              new MessageEmbed()
217                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 186  export default class MuteCommand extends Line 223  export default class MuteCommand extends
223                  }                  }
224    
225                  if (!ms(time)) {                  if (!ms(time)) {
226                      await msg.reply({                      await this.deferReply(msg, {
227                          embeds: [                          embeds: [
228                              new MessageEmbed()                              new MessageEmbed()
229                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 205  export default class MuteCommand extends Line 242  export default class MuteCommand extends
242              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
243          }          }
244    
245          await mute(client, dateTime, user, msg, timeInterval, reason);          if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
246                return;
247            }
248            
249            if (shouldNotModerate(client, user)) {
250                await msg.reply({
251                    embeds: [
252                        {
253                            description: "This user cannot be muted."
254                        }
255                    ]
256                });
257    
258                return;
259            }
260            
261            await mute(client, dateTime, user, msg, timeInterval, reason, hard);
262    
263          const fields = [          const fields = [
264              {              {
# Line 219  export default class MuteCommand extends Line 272  export default class MuteCommand extends
272              {              {
273                  name: "Duration",                  name: "Duration",
274                  value: time === undefined ? "*No duration set*" : (time + '')                  value: time === undefined ? "*No duration set*" : (time + '')
275                },
276                {
277                    name: "Role Takeout",
278                    value: hard ? 'Yes' : 'No'
279              }              }
280          ];          ];
281    
282          console.log(fields);                  console.log(fields);        
283    
284          await msg.reply({          await this.deferReply(msg, {
285              embeds: [              embeds: [
286                  new MessageEmbed()                  new MessageEmbed()
287                  .setAuthor({                  .setAuthor({
# Line 236  export default class MuteCommand extends Line 293  export default class MuteCommand extends
293              ]              ]
294          });          });
295      }      }
 }  
296    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26