/[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 124 by rakin, Mon Jul 29 17:28:41 2024 UTC revision 428 by rakin, Mon Jul 29 17:30:11 2024 UTC
# Line 1  Line 1 
1  import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js';  /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
5    *
6    * SudoBot is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Affero General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10    *
11    * SudoBot is distributed in the hope that it will be useful, but
12    * WITHOUT ANY WARRANTY; without even the implied warranty of
13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    * GNU Affero General Public License for more details.
15    *
16    * You should have received a copy of the GNU Affero General Public License
17    * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18    */
19    
20    import { CommandInteraction, Guild, GuildMember, Message, Permissions, User } from 'discord.js';
21  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
22  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
23  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
24  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
25  import MessageEmbed from '../../client/MessageEmbed';  import MessageEmbed from '../../client/MessageEmbed';
 import getUser from '../../utils/getUser';  
 import History from '../../automod/History';  
26  import getMember from '../../utils/getMember';  import getMember from '../../utils/getMember';
27  import ms from 'ms';  import ms from 'ms';
 import { unmute } from './UnmuteCommand';  
28  import PunishmentType from '../../types/PunishmentType';  import PunishmentType from '../../types/PunishmentType';
29    import { hasPermission, shouldNotModerate } from '../../utils/util';
30    import UnmuteQueue from '../../queues/UnmuteQueue';
31    
32  export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) {  export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction | { guild: Guild, member: GuildMember, editReply?: undefined }, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) {
33      try {      try {
34          const { default: Punishment } = await import('../../models/Punishment');          const { default: Punishment } = await import('../../models/Punishment');
35                    
# Line 37  export async function mute(client: Disco Line 55  export async function mute(client: Disco
55          }          }
56    
57          if (dateTime && timeInterval) {          if (dateTime && timeInterval) {
58              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 setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);
59                  if (err)              for await (const queue of client.queueManager.queues.values()) {
60                      console.log(err);                  if (queue instanceof UnmuteQueue && queue.data!.memberID === user.id && queue.data!.guildID === msg.guild!.id) {
61                                        await queue.cancel();
62                      console.log('A timeout has been set.');                  }
63                }
64    
65                      await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);              await client.queueManager.addQueue(UnmuteQueue, {
66                    data: {
67                        guildID: msg.guild!.id,
68                        memberID: user.id
69                    },
70                    runAt: new Date(Date.now() + timeInterval)
71              });              });
72          }          }
73                    
# Line 56  export async function mute(client: Disco Line 80  export async function mute(client: Disco
80                  user_id: user.id,                  user_id: user.id,
81                  roles: roles.map(role => role.id),                  roles: roles.map(role => role.id),
82                  guild_id: msg.guild!.id,                  guild_id: msg.guild!.id,
83                    createdAt: new Date()
84              });              });
85          }          }
86    
87          const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));          const role = await msg.guild!.roles.fetch(client.config.props[msg.guild!.id].mute_role);
88          await user.roles.add(role!, reason);          await user.roles.add(role!, reason);
89    
90          await Punishment.create({          await Punishment.create({
# Line 71  export async function mute(client: Disco Line 96  export async function mute(client: Disco
96              reason,              reason,
97              meta: {              meta: {
98                  time: timeInterval ? ms(timeInterval) : undefined                  time: timeInterval ? ms(timeInterval) : undefined
99              }              },
100                createdAt: new Date()
101          });          });
102                    
103          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, hard);
104          await user.send({  
105              embeds: [          try {
106                  new MessageEmbed()              await user.send({
107                  .setAuthor({                  embeds: [
108                      iconURL: <string> msg.guild!.iconURL(),                      new MessageEmbed()
109                      name: `\tYou have been muted in ${msg.guild!.name}`                      .setAuthor({
110                  })                          iconURL: <string> msg.guild!.iconURL(),
111                  .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)                          name: `\tYou have been muted in ${msg.guild!.name}`
112              ]                      })
113          });                      .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
114                    ]
115                });
116            }
117            catch (e) {
118                console.log(e);
119            }
120      }      }
121      catch (e) {      catch (e) {
122          console.log(e);          console.log(e);
# Line 97  export async function mute(client: Disco Line 129  export async function mute(client: Disco
129                      .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")                      .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
130                  ]                  ]
131              });              });
132          else          else if (msg.editReply)
133              await msg.editReply({              await msg.editReply({
134                  embeds: [                  embeds: [
135                      new MessageEmbed()                      new MessageEmbed()
# Line 112  export async function mute(client: Disco Line 144  export async function mute(client: Disco
144    
145  export default class MuteCommand extends BaseCommand {  export default class MuteCommand extends BaseCommand {
146      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
147        permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
148    
149      constructor() {      constructor() {
150          super('mute', 'moderation', []);          super('mute', 'moderation', []);
# Line 233  export default class MuteCommand extends Line 266  export default class MuteCommand extends
266              dateTime = Date.now() + timeInterval;              dateTime = Date.now() + timeInterval;
267          }          }
268    
269            if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
270                return;
271            }
272            
273            if (shouldNotModerate(client, user)) {
274                await msg.reply({
275                    embeds: [
276                        {
277                            description: "This user cannot be muted."
278                        }
279                    ]
280                });
281    
282                return;
283            }
284            
285          await mute(client, dateTime, user, msg, timeInterval, reason, hard);          await mute(client, dateTime, user, msg, timeInterval, reason, hard);
286    
287          const fields = [          const fields = [
# Line 268  export default class MuteCommand extends Line 317  export default class MuteCommand extends
317              ]              ]
318          });          });
319      }      }
 }  
320    }

Legend:
Removed from v.124  
changed lines
  Added in v.428

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26