-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add daily thread creation and manual trigger via slash command
- Loading branch information
Showing
1 changed file
with
62 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |