Skip to content

Commit

Permalink
Add daily thread creation and manual trigger via slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
Fire-Swan authored Jun 5, 2024
1 parent aa196ed commit 5657a7b
Showing 1 changed file with 62 additions and 8 deletions.
70 changes: 62 additions & 8 deletions index.js
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);

0 comments on commit 5657a7b

Please sign in to comment.