import { Client, GatewayIntentBits, Partials } from "discord.js"
import { listenForEvents } from "./events"
import { alertAboutCrashLog, logCrash } from "./crash"
import { registerCommands } from "./commands"

export const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.DirectMessageReactions,
    GatewayIntentBits.GuildMessageReactions,
  ],
  partials: [Partials.Channel, Partials.Message, Partials.Reaction],
})

await client.login(process.env.DISCORD_TOKEN)

listenForEvents(client)
await registerCommands(client)

process.on("unhandledRejection", async (error) => {
  console.error("💥 Unhandled promise rejection:", error)
  await logCrash(error)
})

process.on("uncaughtException", async (error) => {
  console.error("💥 Uncaught exception:", error)
  await logCrash(error)
})

await alertAboutCrashLog(client)
// startAuthServer() this is handy if you make a new bot
