all repos — stealth-developers @ 75b3455c0ab83f42ca6511de32864991d10876f8

apps/bot/src/events/index.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
import type { OAuth2Guild } from "discord.js";
import { upsertActor, upsertGuild } from "@stealth-developers/db";
import { client } from "@/client";
import { logger } from "@/lib";

client.on("clientReady", async () => {
	const guilds = client.guilds.fetch();
	if (!guilds) return;

	for (const guild of (await guilds).values()) await getGuildActor(guild);
});

async function getGuildActor(guild: OAuth2Guild) {
	const member = await client.guilds.cache.get(guild.id)?.members.fetchMe();
	if (!member) return;

	const dbGuild = await upsertGuild({
		discordId: guild.id,
		icon: guild.iconURL(),
		name: guild.name,
	});
	if (!dbGuild) return;

	const actor = await upsertActor({
		moderator: true,
		guild: {
			discordId: guild.id,
			icon: guild.iconURL(),
			name: guild.name,
		},
		user: {
			discordId: member.user.id,
			username: member.user.username,
			displayName: member.displayName,
			avatar: member.user.avatarURL(),
		},
	});

	logger.info({ actor }, `upserted guild actor for ${guild.id} (${guild.name})`);
	return actor;
}