import { Events } from "discord.js"
import type { Guild } from "discord.js"
import dbGuildInit from "@/utils/dbGuildInit"
import dbGuild from "@/schemas/guild"
import { logConsole } from "@/utils/console"

export const name = Events.GuildUpdate
export async function execute(oldGuild: Guild, newGuild: Guild) {
	logConsole('discordjs', 'guild_update', { name: oldGuild.name })

	let guildProfile = await dbGuild.findOne({ guildId: newGuild.id })
	if (!guildProfile) guildProfile = await dbGuildInit(newGuild)
	else {
		guildProfile.guildName = newGuild.name
		guildProfile.guildIcon = newGuild.iconURL() ?? "None"
		await guildProfile.save().catch(console.error)
	}
}
