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