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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26