-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (80 loc) · 2.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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,
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!');
const scheduleTime = process.env.CRON_SCHEDULE || '0 5 * * *'; // Fallback to 5 AM if not specified
cron.schedule(scheduleTime, () => {
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);
if (!channel) {
reject('Failed to retrieve channel.');
return;
}
const today = new Date();
const dateString = today.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
channel.threads.create({
name: dateString,
autoArchiveDuration: 1440, // 1 day
reason: 'Daily thread for discussions'
}).then(thread => {
console.log(`Created thread: ${thread.name}`);
// Parse and add users to the thread
const userIds = process.env.USER_IDS.split(',');
userIds.forEach(userId => {
thread.members.add(userId.trim()).then(() => {
console.log(`Added user ${userId} to thread: ${thread.name}`);
}).catch(error => {
console.error(`Failed to add user ${userId} to thread:`, error);
});
});
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);