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 |
import { 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 |
fields: [ |
62 |
{ |
63 |
name: 'Reason', |
64 |
value: reason ?? '*No reason provided*' |
65 |
}, |
66 |
{ |
67 |
name: 'Strike', |
68 |
value: `${strike} time(s)` |
69 |
} |
70 |
] |
71 |
}) |
72 |
] |
73 |
}); |
74 |
} |
75 |
catch (e) { |
76 |
console.log(e); |
77 |
DMed = false; |
78 |
} |
79 |
|
80 |
await client.logger.logWarn(msg, user, (warned_by ?? msg.member!.user) as User, reason, warning.get('id') as number); |
81 |
|
82 |
return { warning, strike, DMed }; |
83 |
} |
84 |
|
85 |
export default class WarnCommand extends BaseCommand { |
86 |
supportsInteractions: boolean = true; |
87 |
|
88 |
constructor() { |
89 |
super('warn', 'moderation', []); |
90 |
} |
91 |
|
92 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
93 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
94 |
await msg.reply({ |
95 |
embeds: [ |
96 |
new MessageEmbed() |
97 |
.setColor('#f14a60') |
98 |
.setDescription(`This command requires at least one argument.`) |
99 |
] |
100 |
}); |
101 |
|
102 |
return; |
103 |
} |
104 |
|
105 |
let user: GuildMember; |
106 |
let reason: string | undefined; |
107 |
|
108 |
if (options.isInteraction) { |
109 |
user = await <GuildMember> options.options.getMember('member'); |
110 |
|
111 |
if (!user) { |
112 |
await msg.reply({ |
113 |
embeds: [ |
114 |
new MessageEmbed() |
115 |
.setColor('#f14a60') |
116 |
.setDescription("Invalid user given.") |
117 |
] |
118 |
}); |
119 |
|
120 |
return; |
121 |
} |
122 |
|
123 |
if (options.options.getString('reason')) { |
124 |
reason = <string> options.options.getString('reason'); |
125 |
} |
126 |
} |
127 |
else { |
128 |
try { |
129 |
const user2 = await getMember((msg as Message), options); |
130 |
|
131 |
if (!user2) { |
132 |
throw new Error('Invalid user'); |
133 |
} |
134 |
|
135 |
user = user2; |
136 |
} |
137 |
catch (e) { |
138 |
await msg.reply({ |
139 |
embeds: [ |
140 |
new MessageEmbed() |
141 |
.setColor('#f14a60') |
142 |
.setDescription(`Invalid user given.`) |
143 |
] |
144 |
}); |
145 |
|
146 |
return; |
147 |
} |
148 |
|
149 |
console.log(user); |
150 |
|
151 |
|
152 |
if (options.args[1]) { |
153 |
await options.args.shift(); |
154 |
reason = options.args.join(' '); |
155 |
} |
156 |
} |
157 |
|
158 |
try { |
159 |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to warn this user."))) { |
160 |
return; |
161 |
} |
162 |
|
163 |
const { warning, strike, DMed } = await warn(client, user.user, reason, msg, msg.member?.user as User); |
164 |
|
165 |
await msg.reply({ |
166 |
embeds: [ |
167 |
new MessageEmbed() |
168 |
.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.")) |
169 |
.addFields([ |
170 |
{ |
171 |
name: "Reason", |
172 |
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
173 |
}, |
174 |
{ |
175 |
name: "Strike", |
176 |
value: strike + ' time(s)' |
177 |
}, |
178 |
{ |
179 |
name: "Warned by", |
180 |
value: (msg.member?.user as User).tag |
181 |
}, |
182 |
{ |
183 |
name: "ID", |
184 |
value: warning.get('id') + '' |
185 |
} |
186 |
]) |
187 |
] |
188 |
}); |
189 |
} |
190 |
catch (e) { |
191 |
console.log(e); |
192 |
} |
193 |
} |
194 |
} |