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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 165 - (hide annotations)
Mon Jul 29 17:28:51 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5703 byte(s)
Update WarnCommand.ts
1 rakin 51 import { BanOptions, CommandInteraction, Guild, 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 rakin 85 import getMember from '../../utils/getMember';
8     import PunishmentType from '../../types/PunishmentType';
9 rakin 51 import History from '../../automod/History';
10    
11 rakin 148 export async function warn(client: DiscordClient, user: User, reason: string | undefined, msg: Message | CommandInteraction, warned_by?: User) {
12     const { default: Punishment } = await import('../../models/Punishment');
13    
14 rakin 85 const warning = await Punishment.create({
15     guild_id: msg.guild!.id,
16     user_id: user.id,
17     reason,
18     mod_id: warned_by?.id ?? msg.member!.user.id,
19 rakin 86 mod_tag: warned_by?.tag ?? (msg.member!.user as User).tag,
20 rakin 85 type: PunishmentType.WARNING,
21     });
22    
23     const strike = await Punishment.count({
24     where: {
25     guild_id: msg.guild!.id,
26     user_id: user.id,
27     type: PunishmentType.WARNING,
28 rakin 51 }
29 rakin 85 });
30 rakin 51
31 rakin 158 // await History.create(user.id, msg.guild!, 'warn', warned_by?.id ?? msg.member!.user.id, reason ?? null);
32 rakin 51
33 rakin 158 let DMed = true;
34 rakin 51
35 rakin 158 try {
36     await user.send({
37     embeds: [
38     new MessageEmbed({
39     author: {
40     name: `You have been warned in ${msg.guild!.name}`,
41     iconURL: msg.guild!.iconURL()!
42     },
43     fields: [
44     {
45     name: 'Reason',
46     value: reason ?? '*No reason provided*'
47     },
48     {
49     name: 'Strike',
50     value: `${strike} time(s)`
51     }
52     ]
53     })
54     ]
55     });
56     }
57     catch (e) {
58     console.log(e);
59     DMed = false;
60     }
61    
62 rakin 85 await client.logger.logWarn(msg, user, (warned_by ?? msg.member!.user) as User, reason, warning.get('id') as number);
63 rakin 51
64 rakin 158 return { warning, strike, DMed };
65 rakin 51 }
66    
67     export default class WarnCommand extends BaseCommand {
68     supportsInteractions: boolean = true;
69    
70     constructor() {
71     super('warn', 'moderation', []);
72     }
73    
74     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
75     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
76     await msg.reply({
77     embeds: [
78     new MessageEmbed()
79     .setColor('#f14a60')
80     .setDescription(`This command requires at least one argument.`)
81     ]
82     });
83    
84     return;
85     }
86    
87     let user: GuildMember;
88     let reason: string | undefined;
89    
90     if (options.isInteraction) {
91     user = await <GuildMember> options.options.getMember('member');
92    
93     if (!user) {
94     await msg.reply({
95     embeds: [
96     new MessageEmbed()
97     .setColor('#f14a60')
98     .setDescription("Invalid user given.")
99     ]
100     });
101    
102     return;
103     }
104    
105     if (options.options.getString('reason')) {
106     reason = <string> options.options.getString('reason');
107     }
108     }
109     else {
110     try {
111     const user2 = await getMember((msg as Message), options);
112    
113     if (!user2) {
114     throw new Error('Invalid user');
115     }
116    
117     user = user2;
118     }
119     catch (e) {
120     await msg.reply({
121     embeds: [
122     new MessageEmbed()
123     .setColor('#f14a60')
124     .setDescription(`Invalid user given.`)
125     ]
126     });
127    
128     return;
129     }
130    
131     console.log(user);
132    
133     if (options.args[1]) {
134     await options.args.shift();
135     reason = options.args.join(' ');
136     }
137     }
138    
139     try {
140 rakin 158 const { warning, strike, DMed } = await warn(client, user.user, reason, msg, msg.member?.user as User);
141 rakin 85
142     await msg.reply({
143     embeds: [
144     new MessageEmbed()
145     .setDescription(`The user ${user.user.tag} has been warned`)
146     .addFields([
147     {
148     name: "Reason",
149     value: typeof reason === 'undefined' ? '*No reason provided*' : reason
150     },
151     {
152     name: "Strike",
153     value: strike + ' time(s)'
154     },
155     {
156     name: "Warned by",
157     value: (msg.member?.user as User).tag
158     },
159     {
160     name: "ID",
161     value: warning.get('id') + ''
162 rakin 158 },
163 rakin 165 (DMed ? ...{} : {
164 rakin 158 name: "Note",
165     value: ":warning: The user has DMs disabled, so they might not know that they've been warned."
166     })
167 rakin 85 ])
168     ]
169     });
170 rakin 51 }
171     catch (e) {
172     console.log(e);
173     }
174     }
175 rakin 158 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26