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