-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlibScheduler.js
More file actions
146 lines (128 loc) · 5.88 KB
/
libScheduler.js
File metadata and controls
146 lines (128 loc) · 5.88 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { Client } = require('discord.js');
const RANDOM_INTERVAL_MS = 2 * 60 * 60 * 1000; // 2 hours
const UPDATE_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
class Scheduler {
constructor(client) {
this.client = client;
this.randomInterval = null;
this.updateInterval = null;
this.lastRandomRun = null;
this.lastUpdateRun = null;
}
getTimeUntilNextRandom() {
if (!this.lastRandomRun) return 'Not started yet';
const nextRun = new Date(this.lastRandomRun.getTime() + RANDOM_INTERVAL_MS);
const now = new Date();
const diff = nextRun - now;
const hours = Math.floor(diff / 3600000);
const minutes = Math.floor((diff % 3600000) / 60000);
return `${hours} hours and ${minutes} minutes`;
}
getTimeUntilNextUpdate() {
if (!this.lastUpdateRun) return 'Not started yet';
const nextRun = new Date(this.lastUpdateRun.getTime() + UPDATE_INTERVAL_MS);
const now = new Date();
const diff = nextRun - now;
const hours = Math.floor(diff / 3600000);
const minutes = Math.floor((diff % 3600000) / 60000);
return `${hours} hours and ${minutes} minutes`;
}
start() {
// Run random command every 2 hours
this.randomInterval = setInterval(async () => {
try {
this.lastRandomRun = new Date();
// Get all guilds the bot is in
const guilds = this.client.guilds.cache;
for (const [guildId, guild] of guilds) {
try {
// Find the #general channel in each guild
const generalChannel = guild.channels.cache.find(
channel => channel.name === 'science-and-news' && channel.type === 'GUILD_TEXT'
);
if (!generalChannel) {
console.error(`Could not find #science-and-news channel in guild ${guild.name}`);
continue;
}
// Create a message-like object for the random command
const randomMessage = {
channel: generalChannel,
reply: async (content) => {
await generalChannel.send(content);
}
};
// Execute random command with 'general' argument
const randomCommand = this.client.commands.get('random');
if (randomCommand) {
await randomCommand.execute(randomMessage, ['general']);
console.log(`Random command executed in guild ${guild.name}`);
} else {
console.error('Random command not found');
}
} catch (guildError) {
console.error(`Error processing guild ${guild.name}:`, guildError);
}
}
} catch (error) {
console.error('Error in random scheduled task:', error);
}
}, RANDOM_INTERVAL_MS);
// Run update command once a day
this.updateInterval = setInterval(async () => {
try {
this.lastUpdateRun = new Date();
// Get all guilds the bot is in
const guilds = this.client.guilds.cache;
for (const [guildId, guild] of guilds) {
try {
// Find the #general channel in each guild
const generalChannel = guild.channels.cache.find(
channel => channel.name === 'science-and-news' && channel.type === 'GUILD_TEXT'
);
if (!generalChannel) {
console.error(`Could not find #science-and-news channel in guild ${guild.name}`);
continue;
}
// Create a message-like object for the update command
const updateMessage = {
channel: generalChannel,
reply: async (content) => {
await generalChannel.send(content);
}
};
// Execute update command
const updateCommand = this.client.commands.get('update');
if (updateCommand) {
await updateCommand.execute(updateMessage, []);
console.log(`Update command executed in guild ${guild.name}`);
} else {
console.error('Update command not found');
}
} catch (guildError) {
console.error(`Error processing guild ${guild.name}:`, guildError);
}
}
} catch (error) {
console.error('Error in update scheduled task:', error);
}
}, UPDATE_INTERVAL_MS);
// Set initial run times
this.lastRandomRun = new Date();
this.lastUpdateRun = new Date();
console.log('Scheduler started:');
console.log(`- Next random command in: ${this.getTimeUntilNextRandom()}`);
console.log(`- Next update command in: ${this.getTimeUntilNextUpdate()}`);
}
stop() {
if (this.randomInterval) {
clearInterval(this.randomInterval);
this.randomInterval = null;
}
if (this.updateInterval) {
clearInterval(this.updateInterval);
this.updateInterval = null;
}
console.log('Scheduler stopped');
}
}
module.exports = Scheduler;