/[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 203 by rakin, Mon Jul 29 17:29:00 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                });
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 92  export async function mute(client: Disco Line 119  export async function mute(client: Disco
119    
120  export default class MuteCommand extends BaseCommand {  export default class MuteCommand extends BaseCommand {
121      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
122        permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
123    
124      constructor() {      constructor() {
125          super('mute', 'moderation', []);          super('mute', 'moderation', []);
# Line 110  export default class MuteCommand extends Line 138  export default class MuteCommand extends
138              return;              return;
139          }          }
140    
141            if (msg instanceof CommandInteraction)
142                await msg.deferReply();
143    
144          let user: GuildMember;          let user: GuildMember;
145          let reason: string | undefined;          let reason: string | undefined;
146          let time: string | undefined;          let time: string | undefined;
147          let timeInterval: number | undefined;          let timeInterval: number | undefined;
148          let dateTime: number | undefined;          let dateTime: number | undefined;
149            let hard: boolean = false;
150    
151          if (options.isInteraction) {          if (options.isInteraction) {
152              user = await <GuildMember> options.options.getMember('member');              user = await <GuildMember> options.options.getMember('member');
# Line 123  export default class MuteCommand extends Line 155  export default class MuteCommand extends
155                  reason = await <string> options.options.getString('reason');                  reason = await <string> options.options.getString('reason');
156              }              }
157    
158                if (options.options.getBoolean('hardmute')) {
159                    hard = await <boolean> options.options.getBoolean('hardmute');
160                }
161    
162              if (options.options.getString('time')) {              if (options.options.getString('time')) {
163                  time = await options.options.getString('time') as string;                  time = await options.options.getString('time') as string;
164                  timeInterval = await ms(time);                  timeInterval = await ms(time);
165    
166                  if (!timeInterval) {                  if (!timeInterval) {
167                      await msg.reply({                      await this.deferReply(msg, {
168                          embeds: [                          embeds: [
169                              new MessageEmbed()                              new MessageEmbed()
170                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 144  export default class MuteCommand extends Line 180  export default class MuteCommand extends
180              const user2 = await getMember((msg as Message), options);              const user2 = await getMember((msg as Message), options);
181    
182              if (!user2) {              if (!user2) {
183                  await msg.reply({                  await this.deferReply(msg, {
184                      embeds: [                      embeds: [
185                          new MessageEmbed()                          new MessageEmbed()
186                          .setColor('#f14a60')                          .setColor('#f14a60')
# Line 164  export default class MuteCommand extends Line 200  export default class MuteCommand extends
200                  args.shift();                  args.shift();
201    
202                  if (index !== -1) {                  if (index !== -1) {
203                      args.splice(index - 1, 2)                      args.splice(index - 1, 2);
204                  }                  }
205    
206                  reason = await args.join(' ');                  reason = await args.join(' ');
# Line 174  export default class MuteCommand extends Line 210  export default class MuteCommand extends
210                  time = await options.args[index + 1];                  time = await options.args[index + 1];
211    
212                  if (time === undefined) {                  if (time === undefined) {
213                      await msg.reply({                      await this.deferReply(msg, {
214                          embeds: [                          embeds: [
215                              new MessageEmbed()                              new MessageEmbed()
216                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 186  export default class MuteCommand extends Line 222  export default class MuteCommand extends
222                  }                  }
223    
224                  if (!ms(time)) {                  if (!ms(time)) {
225                      await msg.reply({                      await this.deferReply(msg, {
226                          embeds: [                          embeds: [
227                              new MessageEmbed()                              new MessageEmbed()
228                              .setColor('#f14a60')                              .setColor('#f14a60')
# Line 205  export default class MuteCommand extends Line 241  export default class MuteCommand extends
241              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
242          }          }
243    
244          await mute(client, dateTime, user, msg, timeInterval, reason);          if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
245                return;
246            }
247            
248            if (shouldNotModerate(client, user)) {
249                await msg.reply({
250                    embeds: [
251                        {
252                            description: "This user cannot be muted."
253                        }
254                    ]
255                });
256    
257                return;
258            }
259            
260            await mute(client, dateTime, user, msg, timeInterval, reason, hard);
261    
262          const fields = [          const fields = [
263              {              {
# Line 219  export default class MuteCommand extends Line 271  export default class MuteCommand extends
271              {              {
272                  name: "Duration",                  name: "Duration",
273                  value: time === undefined ? "*No duration set*" : (time + '')                  value: time === undefined ? "*No duration set*" : (time + '')
274                },
275                {
276                    name: "Role Takeout",
277                    value: hard ? 'Yes' : 'No'
278              }              }
279          ];          ];
280    
281          console.log(fields);                  console.log(fields);        
282    
283          await msg.reply({          await this.deferReply(msg, {
284              embeds: [              embeds: [
285                  new MessageEmbed()                  new MessageEmbed()
286                  .setAuthor({                  .setAuthor({
# Line 236  export default class MuteCommand extends Line 292  export default class MuteCommand extends
292              ]              ]
293          });          });
294      }      }
 }  
295    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26