blob: a267f0959d442c3f76f75c76b3a12632945e5b0a [file] [log] [blame]
TheCodedProfa16d1672023-01-18 18:58:34 -05001import { AuditLogEvent, Guild, GuildAuditLogsEntry, GuildChannel, Invite } from "discord.js";
pineafan0f5cc782022-08-12 21:55:42 +01002// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import humanizeDuration from "humanize-duration";
PineaFan752af462022-12-31 21:59:38 +00004import type { NucleusClient } from "../utils/client.js";
pineafan0f5cc782022-08-12 21:55:42 +01005
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "inviteCreate";
pineafane625d782022-05-09 18:04:32 +01007
PineaFan752af462022-12-31 21:59:38 +00008export async function callback(client: NucleusClient, invite: Invite) {
TheCodedProfa16d1672023-01-18 18:58:34 -05009 if(!invite.guild) return; // This is a DM invite (not a guild invite
pineafan0f5cc782022-08-12 21:55:42 +010010 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
TheCodedProfa16d1672023-01-18 18:58:34 -050011 const auditLog = (await getAuditLog(invite.guild as Guild, AuditLogEvent.InviteCreate))
12 .filter((entry: GuildAuditLogsEntry) => (entry.target as Invite)!.code === invite.code)[0]!;
13 if (auditLog.executor!.id === client.user!.id) return;
pineafan63fc5e22022-08-04 22:04:10 +010014 const data = {
15 meta: {
16 type: "inviteCreate",
17 displayName: "Invite Created",
18 calculateType: "guildUpdate",
19 color: NucleusColors.green,
20 emoji: "INVITE.CREATE",
21 timestamp: invite.createdTimestamp
22 },
23 list: {
TheCodedProfa16d1672023-01-18 18:58:34 -050024 channel: entry(invite.channel!.id, renderChannel(invite.channel as GuildChannel)),
pineafan63fc5e22022-08-04 22:04:10 +010025 link: entry(invite.url, invite.url),
Skyler Grey11236ba2022-08-08 21:13:33 +010026 expires: entry(invite.maxAge, invite.maxAge ? humanizeDuration(invite.maxAge * 1000) : "Never"),
TheCodedProfa16d1672023-01-18 18:58:34 -050027 createdBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
28 created: entry(invite.createdTimestamp, renderDelta(invite.createdTimestamp!))
pineafan63fc5e22022-08-04 22:04:10 +010029 },
30 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010031 guild: invite.guild!.id
pineafane625d782022-05-09 18:04:32 +010032 }
pineafan63fc5e22022-08-04 22:04:10 +010033 };
34 log(data);
pineafane625d782022-05-09 18:04:32 +010035}