blob: c4ce74e11b3002d24a577b50a29d647f44cbd1cd [file] [log] [blame]
import { AuditLogEvent, Guild, GuildAuditLogsEntry, GuildMember } from "discord.js";
import type { NucleusClient } from "../utils/client.js";
import { purgeByUser } from "../actions/tickets/delete.js";
import { callback as statsChannelRemove } from "../reflex/statsChannelUpdate.js";
export const event = "guildMemberRemove";
export async function callback(client: NucleusClient, member: GuildMember) {
const startTime = Date.now() - 10 * 1000;
purgeByUser(member.id, member.guild.id);
await statsChannelRemove(client, member);
const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
if (!(await isLogging(member.guild.id, "guildMemberUpdate"))) return;
const kickAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberKick)).filter(
(entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id
)[0];
const banAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberBanAdd)).filter(
(entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id
)[0];
let type = "leave";
if (kickAuditLog) {
if (kickAuditLog.executor!.id === client.user!.id) return;
if (kickAuditLog.createdAt.getTime() >= startTime) {
type = "kick";
}
}
if (banAuditLog) {
if (banAuditLog.executor!.id === client.user!.id) return;
if (banAuditLog.createdAt.getTime() >= startTime) {
if (!kickAuditLog) {
return;
} else if (kickAuditLog.createdAt.valueOf() < banAuditLog.createdAt.valueOf()) {
return;
}
}
}
let data;
if (type === "kick") {
if (!kickAuditLog) return;
await client.database.history.create(
"kick",
member.guild.id,
member.user,
kickAuditLog.executor,
kickAuditLog.reason
);
data = {
meta: {
type: "memberKick",
displayName: "Member Kicked",
calculateType: "guildMemberPunish",
color: NucleusColors.red,
emoji: "PUNISH.KICK.RED",
timestamp: Date.now()
},
list: {
memberId: entry(member.id, `\`${member.id}\``),
name: entry(member.id, renderUser(member.user)),
joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
kicked: entry(Date.now(), renderDelta(Date.now())),
kickedBy: entry(kickAuditLog.executor!.id, renderUser(kickAuditLog.executor!)),
reason: entry(
kickAuditLog.reason,
kickAuditLog.reason ? `\n> ${kickAuditLog.reason}` : "*No reason provided.*"
),
accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
serverMemberCount: member.guild.memberCount
},
hidden: {
guild: member.guild.id
}
};
} else {
await client.database.history.create("leave", member.guild.id, member.user, null, null);
data = {
meta: {
type: "memberLeave",
displayName: "Member Left",
calculateType: "guildMemberUpdate",
color: NucleusColors.red,
emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE",
timestamp: Date.now()
},
list: {
memberId: entry(member.id, `\`${member.id}\``),
name: entry(member.id, renderUser(member.user)),
joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
left: entry(Date.now(), renderDelta(Date.now())),
accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
serverMemberCount: member.guild.memberCount
},
hidden: {
guild: member.guild.id
}
};
}
log(data);
}