/[sudobot]/branches/3.x/src/commands/moderation/WarnCommand.ts
ViewVC logotype

Contents of /branches/3.x/src/commands/moderation/WarnCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6218 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2022 OSN Inc.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import { CommandInteraction, GuildMember, Message, User } from 'discord.js';
21 import BaseCommand from '../../utils/structures/BaseCommand';
22 import DiscordClient from '../../client/Client';
23 import CommandOptions from '../../types/CommandOptions';
24 import InteractionOptions from '../../types/InteractionOptions';
25 import MessageEmbed from '../../client/MessageEmbed';
26 import getMember from '../../utils/getMember';
27 import PunishmentType from '../../types/PunishmentType';
28
29 export async function warn(client: DiscordClient, user: User, reason: string | undefined, msg: Message | CommandInteraction, warned_by?: User) {
30 const { default: Punishment } = await import('../../models/Punishment');
31
32 const warning = await Punishment.create({
33 guild_id: msg.guild!.id,
34 user_id: user.id,
35 reason,
36 mod_id: warned_by?.id ?? msg.member!.user.id,
37 mod_tag: warned_by?.tag ?? (msg.member!.user as User).tag,
38 type: PunishmentType.WARNING,
39 createdAt: new Date()
40 });
41
42 const strike = await Punishment.count({
43 guild_id: msg.guild!.id,
44 user_id: user.id,
45 type: PunishmentType.WARNING,
46 });
47
48 // await History.create(user.id, msg.guild!, 'warn', warned_by?.id ?? msg.member!.user.id, reason ?? null);
49
50 let DMed = true;
51
52 try {
53 await user.send({
54 embeds: [
55 new MessageEmbed({
56 author: {
57 name: `You have been warned in ${msg.guild!.name}`,
58 iconURL: msg.guild!.iconURL()!
59 },
60 fields: [
61 {
62 name: 'Reason',
63 value: reason ?? '*No reason provided*'
64 },
65 {
66 name: 'Strike',
67 value: `${strike} time(s)`
68 }
69 ]
70 })
71 ]
72 });
73 }
74 catch (e) {
75 console.log(e);
76 DMed = false;
77 }
78
79 await client.logger.logWarn(msg, user, (warned_by ?? msg.member!.user) as User, reason, warning.get('id') as number);
80
81 return { warning, strike, DMed };
82 }
83
84 export default class WarnCommand extends BaseCommand {
85 supportsInteractions: boolean = true;
86
87 constructor() {
88 super('warn', 'moderation', []);
89 }
90
91 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
92 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
93 await msg.reply({
94 embeds: [
95 new MessageEmbed()
96 .setColor('#f14a60')
97 .setDescription(`This command requires at least one argument.`)
98 ]
99 });
100
101 return;
102 }
103
104 let user: GuildMember;
105 let reason: string | undefined;
106
107 if (options.isInteraction) {
108 user = await <GuildMember> options.options.getMember('member');
109
110 if (!user) {
111 await msg.reply({
112 embeds: [
113 new MessageEmbed()
114 .setColor('#f14a60')
115 .setDescription("Invalid user given.")
116 ]
117 });
118
119 return;
120 }
121
122 if (options.options.getString('reason')) {
123 reason = <string> options.options.getString('reason');
124 }
125 }
126 else {
127 try {
128 const user2 = await getMember((msg as Message), options);
129
130 if (!user2) {
131 throw new Error('Invalid user');
132 }
133
134 user = user2;
135 }
136 catch (e) {
137 await msg.reply({
138 embeds: [
139 new MessageEmbed()
140 .setColor('#f14a60')
141 .setDescription(`Invalid user given.`)
142 ]
143 });
144
145 return;
146 }
147
148 console.log(user);
149
150 if (options.args[1]) {
151 await options.args.shift();
152 reason = options.args.join(' ');
153 }
154 }
155
156 try {
157 const { warning, strike, DMed } = await warn(client, user.user, reason, msg, msg.member?.user as User);
158
159 await msg.reply({
160 embeds: [
161 new MessageEmbed()
162 .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."))
163 .addFields([
164 {
165 name: "Reason",
166 value: typeof reason === 'undefined' ? '*No reason provided*' : reason
167 },
168 {
169 name: "Strike",
170 value: strike + ' time(s)'
171 },
172 {
173 name: "Warned by",
174 value: (msg.member?.user as User).tag
175 },
176 {
177 name: "ID",
178 value: warning.get('id') + ''
179 }
180 ])
181 ]
182 });
183 }
184 catch (e) {
185 console.log(e);
186 }
187 }
188 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26