From 5657a7b3ce91180a988c549734245b0e8b838b87 Mon Sep 17 00:00:00 2001 From: Fire-Swan <60622768+Fire-Swan@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:00:01 -0500 Subject: [PATCH] Add daily thread creation and manual trigger via slash command --- index.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 036d0bc..ae419f5 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,78 @@ const { Client, GatewayIntentBits } = require('discord.js'); +const { REST } = require('@discordjs/rest'); +const { Routes } = require('discord-api-types/v9'); const cron = require('node-cron'); -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] }); + +const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions] }); + +// Register slash commands +const commands = [ + { + name: 'createthread', + description: 'Manually create a thread for today’s results' + } +]; + +const rest = new REST({ version: '9' }).setToken(process.env.BOT_TOKEN); + +rest.put( + Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), + { body: commands } +).then(() => console.log('Successfully registered application commands.')) +.catch(console.error); client.once('ready', () => { console.log('Ready!'); // Schedule a daily thread creation at 5 AM - cron.schedule('20 16 * * *', () => { + cron.schedule('0 5 * * *', () => { + createThread(); + }); +}); + +client.on('interactionCreate', async interaction => { + if (!interaction.isCommand()) return; + + if (interaction.commandName === 'createthread') { + await interaction.deferReply(); + createThread().then(response => { + interaction.editReply(response); + }).catch(error => { + console.error('Error during thread creation:', error); + interaction.editReply('Failed to create thread.'); + }); + } +}); + +function createThread() { + return new Promise(async (resolve, reject) => { const guild = client.guilds.cache.get(process.env.GUILD_ID); + if (!guild) { + reject('Failed to retrieve guild.'); + return; + } + const channel = guild.channels.cache.get(process.env.CHANNEL_ID); - // Generate today's date string + if (!channel) { + reject('Failed to retrieve channel.'); + return; + } + const today = new Date(); - const dateString = today.toLocaleDateString('en-US', {year:'numeric', month:'long',day:'numeric'}); + const dateString = today.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); channel.threads.create({ name: `Results for ${dateString}`, autoArchiveDuration: 1440, // 1 day reason: 'Daily thread for discussions' - }).then(thread => console.log(`Created thread: ${thread.name}`)) - .catch(console.error); + }).then(thread => { + console.log(`Created thread: ${thread.name}`); + resolve(`Created thread: ${thread.name}`); + }).catch(error => { + console.error('Failed to create thread:', error); + reject('Failed to create thread.'); + }); }); -}); -client.login(process.env.BOT_TOKEN); +} + +client.login(process.env.BOT_TOKEN); \ No newline at end of file