import 'dotenv/config' import { type ChatInputCommandInteraction, Client, GatewayIntentBits, Partials, Collection } from 'discord.js' import loadCommands from './loadCommands.js' import deployCommands from './deployCommands.js' import discordMessageUpdate from './tasks/discordMessageUpdate.js' import { makeWorkerUtils, type WorkerUtils, type RunnerOptions, run } from 'graphile-worker' import loadEvents from './loadEvents.js' export interface ExecuteArguments { interaction: ChatInputCommandInteraction; workerUtils: WorkerUtils } if (!process.env.AUTOMATION_USER_JWT) throw new Error(`AUTOMATION_USER_JWT was missing from env`); if (!process.env.DISCORD_TOKEN) throw new Error("DISCORD_TOKEN was missing from env"); if (!process.env.DISCORD_CHANNEL_ID) throw new Error("DISCORD_CHANNEL_ID was missing from env"); if (!process.env.WORKER_CONNECTION_STRING) throw new Error("WORKER_CONNECTION_STRING was missing from env"); const preset: GraphileConfig.Preset = { worker: { connectionString: process.env.WORKER_CONNECTION_STRING, concurrentJobs: 1, fileExtensions: [".js", ".ts"] }, }; async function setupGraphileWorker() { const runnerOptions: RunnerOptions = { preset, taskList: { 'discordMessageUpdate': discordMessageUpdate } } const runner = await run(runnerOptions) if (!runner) throw new Error('failed to initialize graphile worker'); await runner.promise } async function setupWorkerUtils() { const workerUtils = await makeWorkerUtils({ preset }); await workerUtils.migrate() return workerUtils } async function setupDiscordBot(commands: any[], workerUtils: WorkerUtils) { console.log(`setup()`) if (!commands) throw new Error('commands passed to setup() was missing'); console.log(`Create a new client instance`) let client: any = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, ], partials: [ Partials.Message, Partials.Channel, Partials.Reaction, ] }); client.commands = new Collection(); commands.forEach((c) => client.commands.set(c.data.name, c)) // Log in to Discord with your client's token client.login(process.env.DISCORD_TOKEN); await loadEvents(client, workerUtils) } async function main() { console.log(`main()`) const commands = await loadCommands() if (!commands) throw new Error('there were no commands available to be loaded.'); await deployCommands(commands.map((c) => c.data.toJSON())) console.log(`${commands.length} commands deployed: ${commands.map((c) => c.data.name).join(', ')}`) const workerUtils = await setupWorkerUtils() setupDiscordBot(commands, workerUtils) setupGraphileWorker() } main().catch((e) => { console.error("error during main() function") console.error(e) process.exit(3) })