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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26