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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26