1 |
rakin |
86 |
import { BanOptions, CommandInteraction, EmojiIdentifierResolvable, GuildMember, Interaction, InteractionCollector, Message, MessageActionRow, MessageButton, MessageOptions, ReplyOptions, TextChannel, User } from 'discord.js'; |
2 |
rakin |
51 |
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 getUser from '../../utils/getUser'; |
8 |
|
|
import getMember from '../../utils/getMember'; |
9 |
|
|
import History from '../../automod/History'; |
10 |
|
|
import { fetchEmoji } from '../../utils/Emoji'; |
11 |
rakin |
86 |
import Punishment from '../../models/Punishment'; |
12 |
|
|
import PunishmentType from '../../types/PunishmentType'; |
13 |
rakin |
51 |
|
14 |
|
|
export default class HistoryCommand extends BaseCommand { |
15 |
|
|
supportsInteractions: boolean = true; |
16 |
|
|
|
17 |
|
|
constructor() { |
18 |
|
|
super('history', 'moderation', []); |
19 |
|
|
} |
20 |
|
|
|
21 |
rakin |
86 |
async genEmbed(client: DiscordClient, msg: Message | Interaction, user: User, page: number = 1) { |
22 |
|
|
const limit = 3; |
23 |
|
|
const offset = ((page < 1 ? 1 : page) - 1) * limit; |
24 |
|
|
|
25 |
|
|
const logs = await Punishment.findAndCountAll({ |
26 |
|
|
where: { |
27 |
|
|
guild_id: msg.guild!.id, |
28 |
|
|
user_id: user.id |
29 |
|
|
}, |
30 |
|
|
order: [ |
31 |
|
|
['createdAt', 'DESC'] |
32 |
|
|
], |
33 |
|
|
limit, |
34 |
|
|
offset |
35 |
|
|
}); |
36 |
|
|
|
37 |
|
|
let str = ''; |
38 |
|
|
const maxPage = Math.ceil(logs.count / limit); |
39 |
|
|
|
40 |
|
|
const convert = (type: PunishmentType) => { |
41 |
|
|
switch (type) { |
42 |
|
|
case PunishmentType.BAN: |
43 |
|
|
return 'Ban'; |
44 |
rakin |
106 |
case PunishmentType.SOFTBAN: |
45 |
|
|
return 'Soft Ban'; |
46 |
|
|
case PunishmentType.TEMPBAN: |
47 |
|
|
return 'Temporary Ban'; |
48 |
rakin |
86 |
case PunishmentType.BEAN: |
49 |
|
|
return 'Bean'; |
50 |
|
|
case PunishmentType.MUTE: |
51 |
|
|
return 'Mute'; |
52 |
rakin |
106 |
case PunishmentType.HARDMUTE: |
53 |
|
|
return 'Hardmute'; |
54 |
rakin |
86 |
case PunishmentType.KICK: |
55 |
|
|
return 'Kick'; |
56 |
|
|
case PunishmentType.WARNING: |
57 |
|
|
return 'Warning'; |
58 |
|
|
case PunishmentType.UNBAN: |
59 |
|
|
return 'Unban'; |
60 |
|
|
case PunishmentType.UNMUTE: |
61 |
|
|
return 'Unmute'; |
62 |
|
|
default: |
63 |
|
|
return "Unknown"; |
64 |
|
|
} |
65 |
|
|
}; |
66 |
|
|
|
67 |
|
|
for await (const log of logs.rows) { |
68 |
|
|
str += `**Case ID**: ${log.get().id}\n`; |
69 |
|
|
str += `Type: ${convert(log.get().type)}\n`; |
70 |
|
|
str += `Reason: ${log.get().reason ? (log.get().reason.trim() === '' ? '*No reason provided*' : log.get().reason) : '*No reason provided*'}\n`; |
71 |
|
|
|
72 |
|
|
// let mod_tag; |
73 |
|
|
|
74 |
|
|
// try { |
75 |
|
|
// const mod = await client.users.fetch(log.get().mod_id); |
76 |
|
|
|
77 |
|
|
// if (!mod) |
78 |
|
|
// throw new Error(); |
79 |
|
|
|
80 |
|
|
// mod_tag = mod.tag; |
81 |
|
|
// } |
82 |
|
|
// catch (e) { |
83 |
|
|
// mod_tag = log.get().mod_id; |
84 |
|
|
// } |
85 |
|
|
|
86 |
|
|
str += `Action Executor: ${log.get().mod_tag}\n`; |
87 |
|
|
str += `Date: ${log.get().createdAt.toLocaleString('en-US')}\n`; |
88 |
|
|
|
89 |
|
|
// if (log.get().type === PunishmentType.MUTE) { |
90 |
|
|
// str += `Duration: ${(log.get().meta ? JSON.parse(log.get().meta) : {})?.time ?? '*No duration set*'}\n`; |
91 |
|
|
// } |
92 |
|
|
|
93 |
|
|
if (log.get().meta) { |
94 |
rakin |
105 |
const json = typeof log.get().meta === 'string' ? JSON.parse(log.get().meta) : log.get().meta; |
95 |
rakin |
86 |
|
96 |
|
|
if (Object.keys(json).length > 0) { |
97 |
|
|
str += "Additional Attributes:\n```\n"; |
98 |
|
|
|
99 |
|
|
for (const key in json) { |
100 |
|
|
str += `${key}: ${json[key]}\n`; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
str += '\n```\n'; |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
str += '\n'; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
return { |
111 |
|
|
embeds: [ |
112 |
|
|
new MessageEmbed({ |
113 |
|
|
author: { |
114 |
|
|
name: user.tag, |
115 |
|
|
iconURL: user.displayAvatarURL() |
116 |
|
|
}, |
117 |
|
|
title: 'Moderation History', |
118 |
|
|
description: str === '' ? 'No history.' : str, |
119 |
|
|
timestamp: new Date(), |
120 |
|
|
}) |
121 |
|
|
], |
122 |
|
|
components: [ |
123 |
|
|
this.createActionRow(page, maxPage) |
124 |
|
|
] |
125 |
|
|
}; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
createActionRow(page: number, max: number) { |
129 |
|
|
console.log(max); |
130 |
|
|
|
131 |
|
|
const back = new MessageButton({ |
132 |
|
|
customId: 'history-back-', |
133 |
|
|
label: '<<', |
134 |
|
|
style: 'PRIMARY' |
135 |
|
|
}); |
136 |
|
|
|
137 |
|
|
const next = new MessageButton({ |
138 |
|
|
customId: 'history-next-', |
139 |
|
|
label: '>>', |
140 |
|
|
style: 'PRIMARY' |
141 |
|
|
}); |
142 |
|
|
|
143 |
|
|
let nextPage = page + 1; |
144 |
|
|
console.log(nextPage); |
145 |
|
|
|
146 |
|
|
if (nextPage > max) { |
147 |
|
|
nextPage = max; |
148 |
|
|
next.setDisabled(true); |
149 |
|
|
} |
150 |
|
|
else { |
151 |
|
|
next.setDisabled(false); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
let prevPage = page - 1; |
155 |
|
|
console.log(prevPage); |
156 |
|
|
|
157 |
|
|
if (prevPage <= 0) { |
158 |
|
|
prevPage = 1; |
159 |
|
|
back.setDisabled(true); |
160 |
|
|
} |
161 |
|
|
else { |
162 |
|
|
back.setDisabled(false); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
next.setCustomId('history-next-' + nextPage); |
166 |
|
|
back.setCustomId('history-back-' + prevPage); |
167 |
|
|
|
168 |
|
|
return new MessageActionRow() |
169 |
|
|
.addComponents( |
170 |
|
|
back, |
171 |
|
|
next |
172 |
|
|
); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
async update(client: DiscordClient, interaction: Interaction, message: Message) { |
176 |
|
|
console.log('here'); |
177 |
|
|
|
178 |
|
|
if (interaction.isButton() && interaction.customId.startsWith('history-')) { |
179 |
|
|
const splitted = interaction.customId.split('-'); |
180 |
|
|
|
181 |
|
|
if (splitted[2] === undefined || parseInt(splitted[2]) === NaN) |
182 |
|
|
return; |
183 |
|
|
|
184 |
|
|
if (splitted[1] === 'next' || splitted[1] === 'back') { |
185 |
|
|
const options = await this.genEmbed(client, interaction, (global as any).user, parseInt(splitted[2])); |
186 |
|
|
try { |
187 |
|
|
await interaction.update(options); |
188 |
|
|
} |
189 |
|
|
catch (e) { |
190 |
|
|
console.log(e); |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
|
196 |
rakin |
51 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
197 |
|
|
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
198 |
|
|
await msg.reply({ |
199 |
|
|
embeds: [ |
200 |
|
|
new MessageEmbed() |
201 |
|
|
.setColor('#f14a60') |
202 |
|
|
.setDescription(`This command requires at least one argument.`) |
203 |
|
|
] |
204 |
|
|
}); |
205 |
|
|
|
206 |
|
|
return; |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
let user: User | null | undefined; |
210 |
|
|
|
211 |
|
|
if (options.isInteraction) { |
212 |
|
|
user = await <User> options.options.getUser('user'); |
213 |
|
|
} |
214 |
|
|
else { |
215 |
|
|
try { |
216 |
|
|
user = await getUser(client, msg as Message, options); |
217 |
|
|
|
218 |
|
|
if (!user) |
219 |
|
|
throw new Error(); |
220 |
|
|
} |
221 |
|
|
catch (e) { |
222 |
|
|
console.log(e); |
223 |
|
|
|
224 |
|
|
await msg.reply({ |
225 |
|
|
embeds: [ |
226 |
|
|
new MessageEmbed() |
227 |
|
|
.setColor('#f14a60') |
228 |
|
|
.setDescription(`Invalid user given.`) |
229 |
|
|
] |
230 |
|
|
}); |
231 |
|
|
|
232 |
|
|
return; |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
rakin |
86 |
(global as any).user = user; |
237 |
rakin |
51 |
|
238 |
rakin |
86 |
let message = <Message> await msg.reply(await this.genEmbed(client, msg, user)); |
239 |
rakin |
51 |
|
240 |
rakin |
86 |
if (msg instanceof CommandInteraction) |
241 |
|
|
message = <Message> await msg.fetchReply(); |
242 |
rakin |
51 |
|
243 |
rakin |
86 |
const collector = new InteractionCollector(client, { |
244 |
|
|
guild: msg.guild!, |
245 |
|
|
channel: msg.channel!, |
246 |
|
|
max: 20, |
247 |
|
|
componentType: 'BUTTON', |
248 |
|
|
interactionType: 'MESSAGE_COMPONENT', |
249 |
|
|
message, |
250 |
|
|
time: 30000, |
251 |
|
|
filter(i) { |
252 |
|
|
return i.isButton() && i.member?.user.id === msg.member!.user.id; |
253 |
|
|
} |
254 |
|
|
}); |
255 |
rakin |
51 |
|
256 |
rakin |
86 |
collector.on('collect', async i => { |
257 |
|
|
await this.update(client, i, message); |
258 |
|
|
}); |
259 |
rakin |
51 |
|
260 |
rakin |
86 |
collector.on('end', async () => { |
261 |
|
|
try { |
262 |
|
|
await message.edit({ |
263 |
|
|
components: [] |
264 |
|
|
}); |
265 |
rakin |
51 |
} |
266 |
rakin |
86 |
catch (e) { |
267 |
|
|
console.log(e); |
268 |
|
|
} |
269 |
rakin |
51 |
}); |
270 |
|
|
} |
271 |
|
|
} |