/[sudobot]/trunk/src/commands/moderation/MuteCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/moderation/MuteCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 86 - (hide annotations)
Mon Jul 29 17:28:32 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 8617 byte(s)
Updated the database system
1 rakin 51 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js';
2     import BaseCommand from '../../utils/structures/BaseCommand';
3     import DiscordClient from '../../client/Client';
4     import CommandOptions from '../../types/CommandOptions';
5     import InteractionOptions from '../../types/InteractionOptions';
6     import MessageEmbed from '../../client/MessageEmbed';
7     import getUser from '../../utils/getUser';
8     import History from '../../automod/History';
9     import getMember from '../../utils/getMember';
10     import ms from 'ms';
11     import { unmute } from './UnmuteCommand';
12 rakin 86 import PunishmentType from '../../types/PunishmentType';
13 rakin 51
14     export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction, timeInterval: number | undefined, reason: string | undefined) {
15     try {
16 rakin 86 const { default: Punishment } = await import('../../models/Punishment');
17    
18 rakin 51 if (dateTime) {
19     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) => {
20     if (err)
21     console.log(err);
22    
23     console.log('A timeout has been set.');
24    
25     setTimeout(async () => {
26     await client.db.get("SELECT * FROM unmutes WHERE time = ?", [new Date(dateTime!).toISOString()], async (err: any, data: any) => {
27     if (err)
28     console.log(err);
29    
30     if (data) {
31     await client.db.get('DELETE FROM unmutes WHERE id = ?', [data.id], async (err: any) => {
32     let guild = await client.guilds.cache.find(g => g.id === data.guild_id);
33     let member = await guild?.members.cache.find(m => m.id === data.user_id);
34    
35     if (member) {
36     await unmute(client, member, msg, client.user!);
37     await History.create(member.id, msg.guild!, 'unmute', client.user!.id, null);
38     }
39    
40     console.log(data);
41     });
42     }
43     });
44     }, timeInterval);
45     });
46     }
47    
48     const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
49     await user.roles.add(role!);
50 rakin 86
51     await Punishment.create({
52     type: PunishmentType.MUTE,
53     user_id: user.id,
54     guild_id: msg.guild!.id,
55     mod_id: msg.member!.user.id,
56     mod_tag: (msg.member!.user as User).tag,
57     reason,
58     meta: {
59     time: timeInterval ? ms(timeInterval) : undefined
60     }
61     });
62    
63 rakin 51 await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
64     await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User);
65     await user.send({
66     embeds: [
67     new MessageEmbed()
68     .setAuthor({
69     iconURL: <string> msg.guild!.iconURL(),
70     name: `\tYou have been muted in ${msg.guild!.name}`
71     })
72     .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
73     ]
74     });
75     }
76     catch (e) {
77     console.log(e);
78    
79     await msg.reply({
80     embeds: [
81     new MessageEmbed()
82     .setColor('#f14a60')
83     .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
84     ]
85     });
86    
87     return;
88     }
89     }
90    
91     export default class MuteCommand extends BaseCommand {
92     supportsInteractions: boolean = true;
93    
94     constructor() {
95     super('mute', 'moderation', []);
96     }
97    
98     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
99     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
100     await msg.reply({
101     embeds: [
102     new MessageEmbed()
103     .setColor('#f14a60')
104     .setDescription(`This command requires at least one argument.`)
105     ]
106     });
107    
108     return;
109     }
110    
111     let user: GuildMember;
112     let reason: string | undefined;
113     let time: string | undefined;
114     let timeInterval: number | undefined;
115     let dateTime: number | undefined;
116    
117     if (options.isInteraction) {
118     user = await <GuildMember> options.options.getMember('member');
119    
120     if (options.options.getString('reason')) {
121     reason = await <string> options.options.getString('reason');
122     }
123    
124     if (options.options.getString('time')) {
125     time = await options.options.getString('time') as string;
126     timeInterval = await ms(time);
127    
128     if (!timeInterval) {
129     await msg.reply({
130     embeds: [
131     new MessageEmbed()
132     .setColor('#f14a60')
133     .setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`)
134     ]
135     });
136    
137     return;
138     }
139     }
140     }
141     else {
142     const user2 = await getMember((msg as Message), options);
143    
144     if (!user2) {
145     await msg.reply({
146     embeds: [
147     new MessageEmbed()
148     .setColor('#f14a60')
149     .setDescription(`Invalid user given.`)
150     ]
151     });
152    
153     return;
154     }
155    
156     user = user2;
157    
158     const index = await options.args.indexOf('-t');
159    
160     if (options.args[1]) {
161     const args = [...options.args];
162     args.shift();
163    
164     if (index !== -1) {
165     args.splice(index - 1, 2)
166     }
167    
168     reason = await args.join(' ');
169     }
170    
171     if (index !== -1) {
172     time = await options.args[index + 1];
173    
174     if (time === undefined) {
175     await msg.reply({
176     embeds: [
177     new MessageEmbed()
178     .setColor('#f14a60')
179     .setDescription(`Option \`-t\` (time) requires an argument.`)
180     ]
181     });
182    
183     return;
184     }
185    
186     if (!ms(time)) {
187     await msg.reply({
188     embeds: [
189     new MessageEmbed()
190     .setColor('#f14a60')
191     .setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`)
192     ]
193     });
194    
195     return;
196     }
197    
198     timeInterval = await ms(time);
199     }
200     }
201    
202     if (timeInterval) {
203     dateTime = Date.now() + timeInterval;
204     }
205    
206     await mute(client, dateTime, user, msg, timeInterval, reason);
207    
208     const fields = [
209     {
210     name: "Muted by",
211     value: (msg.member!.user as User).tag
212     },
213     {
214     name: "Reason",
215     value: reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason
216     },
217     {
218     name: "Duration",
219     value: time === undefined ? "*No duration set*" : (time + '')
220     }
221     ];
222    
223     console.log(fields);
224    
225     await msg.reply({
226     embeds: [
227     new MessageEmbed()
228     .setAuthor({
229     name: user.user.tag,
230     iconURL: user.displayAvatarURL(),
231     })
232     .setDescription(user.user.tag + " has been muted.")
233     .addFields(fields)
234     ]
235     });
236     }
237     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26