1 |
rakin |
393 |
/** |
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 |
rakin |
344 |
import { CommandInteraction, GuildMember, Message, User } from 'discord.js'; |
21 |
rakin |
51 |
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 |
rakin |
85 |
import getMember from '../../utils/getMember'; |
27 |
|
|
import PunishmentType from '../../types/PunishmentType'; |
28 |
rakin |
452 |
import { hasPermission } from '../../utils/util'; |
29 |
rakin |
51 |
|
30 |
rakin |
148 |
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 |
rakin |
85 |
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 |
rakin |
86 |
mod_tag: warned_by?.tag ?? (msg.member!.user as User).tag, |
39 |
rakin |
85 |
type: PunishmentType.WARNING, |
40 |
rakin |
336 |
createdAt: new Date() |
41 |
rakin |
85 |
}); |
42 |
|
|
|
43 |
|
|
const strike = await Punishment.count({ |
44 |
rakin |
336 |
guild_id: msg.guild!.id, |
45 |
|
|
user_id: user.id, |
46 |
|
|
type: PunishmentType.WARNING, |
47 |
rakin |
85 |
}); |
48 |
rakin |
51 |
|
49 |
rakin |
158 |
// await History.create(user.id, msg.guild!, 'warn', warned_by?.id ?? msg.member!.user.id, reason ?? null); |
50 |
rakin |
51 |
|
51 |
rakin |
158 |
let DMed = true; |
52 |
rakin |
51 |
|
53 |
rakin |
158 |
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 |
rakin |
85 |
await client.logger.logWarn(msg, user, (warned_by ?? msg.member!.user) as User, reason, warning.get('id') as number); |
81 |
rakin |
51 |
|
82 |
rakin |
158 |
return { warning, strike, DMed }; |
83 |
rakin |
51 |
} |
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 |
rakin |
452 |
|
151 |
rakin |
51 |
|
152 |
|
|
if (options.args[1]) { |
153 |
|
|
await options.args.shift(); |
154 |
|
|
reason = options.args.join(' '); |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
try { |
159 |
rakin |
452 |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to warn this user."))) { |
160 |
|
|
return; |
161 |
|
|
} |
162 |
|
|
|
163 |
rakin |
158 |
const { warning, strike, DMed } = await warn(client, user.user, reason, msg, msg.member?.user as User); |
164 |
rakin |
85 |
|
165 |
|
|
await msg.reply({ |
166 |
|
|
embeds: [ |
167 |
|
|
new MessageEmbed() |
168 |
rakin |
459 |
.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 |
|
|
.setTimestamp() |
170 |
|
|
.setColor("RED") |
171 |
rakin |
85 |
.addFields([ |
172 |
|
|
{ |
173 |
|
|
name: "Reason", |
174 |
|
|
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
175 |
|
|
}, |
176 |
|
|
{ |
177 |
|
|
name: "Strike", |
178 |
|
|
value: strike + ' time(s)' |
179 |
|
|
}, |
180 |
|
|
{ |
181 |
|
|
name: "Warned by", |
182 |
|
|
value: (msg.member?.user as User).tag |
183 |
|
|
}, |
184 |
|
|
{ |
185 |
|
|
name: "ID", |
186 |
|
|
value: warning.get('id') + '' |
187 |
rakin |
167 |
} |
188 |
rakin |
85 |
]) |
189 |
|
|
] |
190 |
|
|
}); |
191 |
rakin |
51 |
} |
192 |
|
|
catch (e) { |
193 |
|
|
console.log(e); |
194 |
|
|
} |
195 |
|
|
} |
196 |
rakin |
158 |
} |