import type {Interaction} from 'discord.js'; import {Client, Events, GatewayIntentBits, MessageFlags} from 'discord.js'; import type { SlashCommandBuilder, SlashCommandOptionsOnlyBuilder, } from 'discord.js'; import {sql, GuildSetting, initDb} from './database'; const BLITZCRANK_BANNER = ` ****++++++++++*+++ **+*+******+********+******+*++* **** *+*+**+*+*+*++++++++++++++*+++++*++++*** *=....... + **+*+*+++++++++++++++++++++++++++*++++++++++** ............= +****+*+*+++++++++#%%#######%*++++++++++++++**+**+** *.....=@@@@+......+*+++++++++++##*#*++++++++++***%*+++++****++++++*+*++ .... @@. ..@% .. ..*++++++++%***++=%=+=======+++++**@+++++++++*++++*++**+ .....=@-...:@#.....+*+++++%***++====+====--==-====++**#+++++++++**+++**+*+* .........@@@@-....-*++++#****++==---%:::::-------===+**#++++*++++++++++++***+ +.... +@. .........+++%***+++==--::==.::::::+##*-===+**%++++++++++++++++++*+++ *.......: .......--:.=@***++++=-::::.*....:=+:...-*-==+***+++++++++**+++++****+* *.......=%:......*+++****+%==-=*+::..--....*-.....=+==++*#++++++++++++++**++++*+** *+ ......+++++++++#+++*-::::::*....#....*+....:+:==++**%++++++++++++++*++++++++* *+++++*+*++++*++++#++#-:.......+...-+...:.#**#=-===+++*#+++++++*++++*++++**+++*+* *++*+++*+*+++++++++#*=#::.......+.::.#::::-=========+++**+++++++++++++++++++*+++*++ *+**+++++++++++++++*%*+=-........+-:::-#::---========+++#@=+++++++++++++++++++++++*+* ****+++*+**+*+++++++*%**+++:....+#=-----%-----========++@+===+++++++++++++++*+*+*++*** ***+++++++++++++*++++@****-+#%%*:===----=*-===========%#*@%%%%%%%%%%#*+++++++++++++++** **+*++*++++++++++*++++%#***++++++=========%======#%*==#***%###*******#%%%%@*++++++++++*+* **+*+++++++*+++++*++++*@*****++++++========#==#+===----=+#%#****++++*****@####++++++*+*+* +***++++*++++++++++++++@@#****++++++=======##===---------.%#*@%*+++++++**%#+**#++++*++*** *+*+*++++++++++++++++++==%@@#%@@+++++========%##:-----::%*##=#-::#*++++++*@=++#*#+++++++*++ *+*+*++++++*++++++++++=%%%@%@#####+++=======-%####+:=**#**--::--=##++++++==++####%+++++++** ***++++++++*+++++++++%%%@###%@#*#*%++=========@*#*##%#=-::--=++%+-#=+======+####**%++++*+*+ *+*+++++++++++++++++%%%#########%@%%*++======+%@%*##+*-*===#+-=====%=@#===#######**%+++++*+ *+*++++++++++++++++%%%####**********#%%%%%%#*+++*#%*#*+#*-:=========%::-*########*#++++++*+ *+*++++*++++++++++*##%##*****************+++++++++*#*##@*-===========%*##########*#++++*+*+ ***+*++*+++*++++++%###*%*****++++++++++++++***####%*#*@*##:===========%###########+++++*+*+ ++***++++*++++++++%#%*****##*+++++++++++**###%%+=====:@*##%-=========+#%########*++++*+++++ *+***++*+*+*++++++%#%%%#++**+++##+++++*####@===========:@###-======%%#%%######%++++++++**** ***+++*++++++++++##%%####%#*==+=++*####@==+#========----:@#%-=#%%##%*=%*#####+++++++*+*+* +***++*++++++++++##%%%########%%%%%%%*=====-%+-====----::-#%%%##%+-*+##+*#@++++++++++**+* *+*++++++*++++++#%%%%%#######%%@@@==========:*:---:::::#*%%%#+-#*##%%+:#++++++*++++*+*+* *++*+++*++++++++++%%%%######%%@@%@*=======---.*+::.-+*###*+**###%%+:=#*#++++*++++++***+ +*++*+**++*++++++++%%%####%@@@@@@%%=====---::.+++**##+++=*%%%%*:-*****#%#*%#*++*++*+* ++*+++++++*++++++++++@%%%@@@@@@@@%%-==--::.:#*%##+-++###%#*::#***#**%=:-#**%++++*+++ **+++*+++++++++++++++++++%@@@@@@@@%%:-:.=**#%#%*+*###%#=::+*####*%#*=:#***#*+++**+ *++***++*+++++++++++++++++++++*#%@%@-#*#%*+*=*%%#%#+::+*#######*%#**##***%+++*+** *++**+++*++++++++++++++++++++++%#%#%%**-+####%#=::+*####%#####*%*####**%+++++++ +*+*+*+**+++*++*+++++++++++++*%#%#%*#%*%%#=:+*#####%***##**%#*@###*%*++++** ***+*++++++*++++++++*+++++++++%#%#%%%#=-+###########*-::-+##%#%%%%+****++ *+++++++++++++++++++++++++++++%%%%++############*%*=-#*###*@#%%%+**+*+ *+++*+*+*++++++++++++++++++++#%#####%##################*%#*#%***+++ ++*+*+**+*+***++++*++++***++*%###%#*%%###%*##%%#####@%%%%***+** *+*++++*+++++++++++++++++++*%#%%+=:..:=+=%#%%%%%%%%%%*+*+*+ +*++***++++++++++++++++++++%%#*#*#######%#%%%%%%#+**+* **+*******+++**++**++*++#%%#########%%#%%%**++*+ ++**+*++++*+*+++++++++@#######%%%#*++***** **+*+*****++********##**+*++*+**+ +******************+ "FIRED UP AND READY TO SERVE!" `; const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent, ], }); import {Routes} from 'discord.js'; import {guildId, appId, token, remindersChannelId} from './config.json'; import {REST} from 'discord.js'; const rest = new REST(); rest.setToken(token); interface Command { data: SlashCommandBuilder | SlashCommandOptionsOnlyBuilder; execute: (interaction: any) => Promise; initialize: (any) => Promise; } import {Collection} from 'discord.js'; const commands = new Collection(); import PingCommand from './commands/calendar/ping'; import RemindCommand from './commands/calendar/remind'; import QuoteCommand from './commands/quotes/quote'; import NagCommand from './commands/calendar/nag/nag'; import UnnagCommand from './commands/calendar/nag/unnag'; import CheckinCommand from './commands/calendar/nag/checkin'; import {Manager} from './commands/calendar/nag/service'; const nagManager = new Manager({ client: client, db: sql, }); nagManager.start(); console.debug(`${remindersChannelId}`); commands.set('ping', PingCommand({client: client, db: sql})); commands.set( 'remind', RemindCommand({ client: client, db: sql, publicChannel: remindersChannelId, responseMode: 'public', }), ); commands.set('quote', QuoteCommand({})); commands.set( 'nag', NagCommand({ client: client, db: sql, }), ); commands.set( 'unnag', UnnagCommand({ client: client, db: sql, }), ); commands.set( 'checkin', CheckinCommand({ client: client, db: sql, }), ); async function syncCommands() { try { console.log(`Started refreshing slash commands`); const _data = await rest.put( Routes.applicationGuildCommands(appId, guildId), { body: commands.mapValues(cmd => cmd.data.toJSON()), }, ); console.log(`Successfully reloaded slash commands`); } catch (error) { console.error(error); } } client.on(Events.InteractionCreate, async (interaction: Interaction) => { if (!interaction.isChatInputCommand()) { return; } try { const command = commands.get(interaction.commandName); if (command == null) { await interaction.followUp(`No such command ${interaction.commandName}`); return; } command.execute(interaction); } catch (error) { console.error(error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'There was an error while executing this command', flags: MessageFlags.Ephemeral, }); } } // TODO }); client.once(Events.ClientReady, async readyClient => { await syncCommands(); initDb(); // TODO GuildSetting.sync(); // TODO for (const [_name, cmd] of commands) { await cmd?.initialize({ client: client, db: sql, }); } // Print banner for (const ln of BLITZCRANK_BANNER.split('\n')) { console.log(ln); } console.log(`Logged in as ${readyClient.user.tag}`); }); client.login(token);