import { CacheType, ChatInputCommandInteraction, Client, Events, GatewayIntentBits, Guild, GuildBasedChannel, Interaction, Role } from "discord.js" import { utilities } from "./utilties" import { commands } from "./commands" import { config } from "./config" import { SQLCommon } from "./utilties/storage/interfaces" export const client : Client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent ] }) export let db: SQLCommon if (config.DB_MODE == "sqlite") { db = new utilities.sqlite.SqliteDB("breadbot_test.db") db.run("PRAGMA foreign_keys = ON") utilities.tables.makeTables(db) //TODO I really don't want this to be here. utilities.events.messages.setupMessageCapture(client, db) utilities.events.roles.setupRoleCapture(client, db) } client.once(Events.ClientReady, () => { // TODO Winston should handle this console.log("Breadbot is ready") client.guilds.cache.forEach(async (guild: Guild) => { await utilities.commands.deployCommands(guild.id) // TODO handle failures? await utilities.guilds.insertGuild(db, guild) guild.channels.cache.forEach(async (channel: GuildBasedChannel) => { await utilities.channels.insertChannel(db, channel) }) guild.roles.cache.forEach(async (role: Role) => { await utilities.roles.insertRole(db, role) }) }) }) client.on(Events.GuildCreate, async (guild : Guild) => { await utilities.commands.deployCommands(guild.id) await utilities.guilds.insertGuild(db, guild) guild.channels.cache.forEach(async (channel: GuildBasedChannel) => { await utilities.channels.insertChannel(db, channel) }) }) client.on(Events.ChannelCreate, async (channel) => { console.log("CHANNEL CREATE CALLED") await utilities.channels.insertChannel(db, channel) }) client.on(Events.ThreadCreate, async (channel) => { console.log("THREAD CREATE CALLED") console.log(channel.toString()) await utilities.channels.insertChannel(db, channel) }) client.on(Events.InteractionCreate, async (interaction: Interaction) => { if (!interaction.isCommand()) { return } if (commands[interaction.commandName as keyof typeof commands]) { commands[interaction.commandName as keyof typeof commands].execute(interaction as ChatInputCommandInteraction) } }) setInterval(async () => { await utilities.breadthread.breadthreadProcessLocks(db, client) }, 5000) client.login(config.DISCORD_TOKEN)