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, Permissions, 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 |
import { generateInfractionDescription, hasPermission } from '../../utils/util'; |
29 |
|
30 |
export async function warn(client: DiscordClient, user: User, reason: string | undefined, msg: Message | CommandInteraction, warned_by?: User) { |
31 |
const { default: Punishment } = await import('../../models/Punishment'); |
32 |
|
33 |
const warning = await Punishment.create({ |
34 |
guild_id: msg.guild!.id, |
35 |
user_id: user.id, |
36 |
reason, |
37 |
mod_id: warned_by?.id ?? msg.member!.user.id, |
38 |
mod_tag: warned_by?.tag ?? (msg.member!.user as User).tag, |
39 |
type: PunishmentType.WARNING, |
40 |
createdAt: new Date() |
41 |
}); |
42 |
|
43 |
const strike = await Punishment.count({ |
44 |
guild_id: msg.guild!.id, |
45 |
user_id: user.id, |
46 |
type: PunishmentType.WARNING, |
47 |
}); |
48 |
|
49 |
// await History.create(user.id, msg.guild!, 'warn', warned_by?.id ?? msg.member!.user.id, reason ?? null); |
50 |
|
51 |
let DMed = true; |
52 |
|
53 |
try { |
54 |
await user.send({ |
55 |
embeds: [ |
56 |
new MessageEmbed({ |
57 |
author: { |
58 |
name: `You have been warned in ${msg.guild!.name}`, |
59 |
iconURL: msg.guild!.iconURL()! |
60 |
}, |
61 |
description: generateInfractionDescription(client, msg.guildId!, 'warning_message'), |
62 |
fields: [ |
63 |
{ |
64 |
name: 'Reason', |
65 |
value: reason ?? '*No reason provided*' |
66 |
}, |
67 |
{ |
68 |
name: 'Strike', |
69 |
value: `${strike} time(s)` |
70 |
}, |
71 |
{ |
72 |
name: 'Infraction ID', |
73 |
value: warning.numericId + '' |
74 |
} |
75 |
] |
76 |
}) |
77 |
.setColor('GOLD') |
78 |
] |
79 |
}); |
80 |
} |
81 |
catch (e) { |
82 |
console.log(e); |
83 |
DMed = false; |
84 |
} |
85 |
|
86 |
await client.logger.onMemberWarn(user, msg.guild!.id, warning.numericId!.toString(), reason, (warned_by ?? msg.member!.user) as User); |
87 |
|
88 |
return { warning, strike, DMed }; |
89 |
} |
90 |
|
91 |
export default class WarnCommand extends BaseCommand { |
92 |
supportsInteractions: boolean = true; |
93 |
permissions = [Permissions.FLAGS.MANAGE_MESSAGES]; |
94 |
|
95 |
constructor() { |
96 |
super('warn', 'moderation', []); |
97 |
} |
98 |
|
99 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
100 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
101 |
await msg.reply({ |
102 |
embeds: [ |
103 |
new MessageEmbed() |
104 |
.setColor('#f14a60') |
105 |
.setDescription(`This command requires at least one argument.`) |
106 |
] |
107 |
}); |
108 |
|
109 |
return; |
110 |
} |
111 |
|
112 |
if (msg instanceof CommandInteraction) |
113 |
await msg.deferReply(); |
114 |
|
115 |
let user: GuildMember; |
116 |
let reason: string | undefined; |
117 |
|
118 |
if (options.isInteraction) { |
119 |
user = await <GuildMember> options.options.getMember('member'); |
120 |
|
121 |
if (!user) { |
122 |
await this.deferReply(msg, { |
123 |
embeds: [ |
124 |
new MessageEmbed() |
125 |
.setColor('#f14a60') |
126 |
.setDescription("Invalid user given.") |
127 |
] |
128 |
}); |
129 |
|
130 |
return; |
131 |
} |
132 |
|
133 |
if (options.options.getString('reason')) { |
134 |
reason = <string> options.options.getString('reason'); |
135 |
} |
136 |
} |
137 |
else { |
138 |
try { |
139 |
const user2 = await getMember((msg as Message), options); |
140 |
|
141 |
if (!user2) { |
142 |
throw new Error('Invalid user'); |
143 |
} |
144 |
|
145 |
user = user2; |
146 |
} |
147 |
catch (e) { |
148 |
await this.deferReply(msg, { |
149 |
embeds: [ |
150 |
new MessageEmbed() |
151 |
.setColor('#f14a60') |
152 |
.setDescription(`Invalid user given.`) |
153 |
] |
154 |
}); |
155 |
|
156 |
return; |
157 |
} |
158 |
|
159 |
console.log(user); |
160 |
|
161 |
|
162 |
if (options.args[1]) { |
163 |
await options.args.shift(); |
164 |
reason = options.args.join(' '); |
165 |
} |
166 |
} |
167 |
|
168 |
try { |
169 |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to warn this user."))) { |
170 |
return; |
171 |
} |
172 |
|
173 |
const { warning, strike, DMed } = await warn(client, user.user, reason, msg, msg.member?.user as User); |
174 |
|
175 |
await this.deferReply(msg, { |
176 |
embeds: [ |
177 |
new MessageEmbed() |
178 |
.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.")) |
179 |
.setTimestamp() |
180 |
.setFooter({ text: 'Warned' }) |
181 |
.setColor("RED") |
182 |
.addFields([ |
183 |
{ |
184 |
name: "Reason", |
185 |
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
186 |
}, |
187 |
{ |
188 |
name: "Strike", |
189 |
value: strike + ' time(s)' |
190 |
}, |
191 |
{ |
192 |
name: "Warned by", |
193 |
value: (msg.member?.user as User).tag |
194 |
}, |
195 |
{ |
196 |
name: "Infraction ID", |
197 |
value: warning.get('numericId') + '' |
198 |
} |
199 |
]) |
200 |
] |
201 |
}); |
202 |
} |
203 |
catch (e) { |
204 |
console.log(e); |
205 |
} |
206 |
} |
207 |
} |