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