-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
322 lines (292 loc) · 10.3 KB
/
functions.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const { sendMessage } = require('./discord');
const { v4: uuidv4 } = require('uuid');
let activeTimers = {};
async function handlePingCommand(message, args) {
if (args.length < 3) {
console.log('ping received... pong!');
try {
await sendMessage(message, 'pong!');
} catch (error) {
console.error('Error sending pong message:', error);
return false;
}
return true;
}
console.log('ping received... but no pong for you! too many arguments');
return false;
}
async function handleTimeCommand(message, args) {
console.log('time received');
if (args.length > 2) {
for(let i = 2; i < args.length; i++) {
const timezone = args[i];
try { // Attempt to get the current time in the provided timezone
const time = new Date().toLocaleTimeString('en-US', { timeZone: timezone });
await sendMessage(message, `The current time in ${timezone} is: ${time}`);
console.log(`Time sent for timezone: ${timezone}`);
} catch (e) { // Handle invalid timezones
console.error('Invalid timezone:', timezone);
try {
await sendMessage(message, `${timezone} is not a timezone. (Example: utc)`);
console.log('Invalid timezone message sent');
} catch (error) {
console.error('Error sending invalid timezone message:', error);
return false;
}
}
}
return true;
} else { // Prompt the user to provide a timezone
try {
await sendMessage(message, 'Please provide a timezone.');
console.log('Timezone prompt sent');
} catch (error) {
console.error('Error sending timezone prompt:', error);
return false;
}
return true;
}
}
async function handleUnknownCommand(message) {
try {
await sendMessage(message, 'Unknown command - use `!clockwatch help` for a list of commands.');
console.log('Unknown command message sent');
} catch (error) {
console.error('Error sending unknown command message:', error);
return false;
}
return true;
}
async function handleHelpCommand(message) {
try {
await sendMessage(
message,
'Available commands:\n' +
'`!clockwatch ping` - check if the bot is online\n' +
'`!clockwatch time [timezone]` - get the current time in a timezone\n' +
'`!clockwatch timer [duration] [unit]` - set a timer for a duration (e.g., 5 min, 2 hours)\n' +
'`!clockwatch soon [unit]` - set a timer for a random duration (seconds for up to 15 minutes, minutes for up to 90 minutes, hours for up to 6 hours )\n' +
'`!clockwatch show` - shows all active timers and alarms\n' +
'`!clockwatch reset` - reset all timers'
);
console.log('Help message sent');
} catch (error) {
console.error('Error sending help message:', error);
return false;
}
return true;
}
async function handleTimerCommand(message, args) {
if (args.length < 4) {
try {
await sendMessage(message, 'Please provide a duration and unit (e.g., 5 min, 2 hours).');
return true;
}
catch (error) {
console.error('Error sending timer prompt:', error);
return false;
}
}
const duration = parseInt(args[2]);
const unit = args[3].toLowerCase();
if (isNaN(duration) || duration <= 0) {
try {
await sendMessage(message, 'Invalid duration. Please provide a positive whole number.');
return true;
}
catch (error) {
console.error('Error sending invalid duration message:', error);
return false;
}
}
let milliseconds;
switch (unit) {
case 'sec':
case 'second':
case 'seconds':
milliseconds = duration * 1000;
break;
case 'min':
case 'minute':
case 'minutes':
milliseconds = duration * 60 * 1000;
break;
case 'hour':
case 'hours':
milliseconds = duration * 60 * 60 * 1000;
break;
default:
try {
await sendMessage(message, 'Invalid unit. Please use seconds, minutes, or hours.');
return true;
}
catch (error) {
console.error('Error sending unit message:', error);
return false;
}
}
if (milliseconds > 86400000) {
try {
await sendMessage(message, 'Duration is too long. Please provide a time less than 24 hours.');
}
catch (error) {
console.error('Error sending duration message:', error);
return false;
}
return false;
}
const timerId = uuidv4(); // Generate a unique identifier for the timer
const serverId = message.guild.id;
if (!activeTimers[serverId]) {
activeTimers[serverId] = [];
}
try {
await sendMessage(message, `Timer set for ${duration} ${unit}.`);
activeTimers[serverId].push({ id: timerId, user: message.author.username, duration, unit, endTime: Date.now() + milliseconds });
}
catch (error) {
console.error('Error sending timer message:', error);
return false;
}
setTimeout(async () => {
try {
const timer = activeTimers[serverId].find(timer => timer.id === timerId);
if (timer) {
await sendMessage(message, `<@${message.author.id}> : ${duration} ${unit} have passed.`);
activeTimers[serverId] = activeTimers[serverId].filter(timer => timer.endTime > Date.now());
}
}
catch (error) {
console.error('Error sending timer message:', error);
return false;
}
}, milliseconds);
return true;
}
async function handleShowCommand(message) {
const serverId = message.guild.id;
try {
if (!activeTimers[serverId] || activeTimers[serverId].length === 0) {
await sendMessage(message, 'No active timers.');
} else {
let timerList = 'Active timers:\n';
activeTimers[serverId].forEach(timer => {
const timeLeftInMinutes = Math.round((timer.endTime - Date.now()) / 60000); // Convert milliseconds to minutes
timerList += `${timer.user}: ${timer.duration} ${timer.unit} (ends in ${timeLeftInMinutes} minutes)\n`;
});
await sendMessage(message, timerList);
}
console.log('Show command message sent');
} catch (error) {
console.error('Error sending show command message:', error);
return false;
}
return true;
}
async function handleResetCommand(message) {
const serverId = message.guild.id;
activeTimers[serverId] = [];
try {
await sendMessage(message, 'All timers reset.');
console.log('Reset command message sent');
} catch (error) {
console.error('Error sending reset command message:', error);
return false;
}
return true;
}
async function handleSoonCommand(message, args) {
if (args.length < 3) {
try {
await sendMessage(message, 'Please provide a unit (seconds for up to 15 minutes, minutes for up to 90 minutes, or hours for up to 6 hours).');
return true;
}
catch (error) {
console.error('Error sending unit prompt:', error);
return false;
}
}
const unit = args[2].toLowerCase();
let maxDuration;
let unitLabel;
switch (unit) {
case 'sec':
case 'second':
case 'seconds':
maxDuration = 900; // Maximum 15 minutes
unitLabel = 'seconds';
break;
case 'min':
case 'minute':
case 'minutes':
maxDuration = 90; // Maximum 90 minutes
unitLabel = 'minutes';
break;
case 'hour':
case 'hours':
maxDuration = 6; // Maximum 6 hours
unitLabel = 'hours';
break;
default:
try {
await sendMessage(message, 'Invalid unit. Please use seconds, minutes, or hours.');
return true;
}
catch (error) {
console.error('Error sending invalid unit message:', error);
return false;
}
}
const duration = Math.floor(Math.random() * maxDuration) + 1; // Random duration between 1 and maxDuration
let milliseconds;
switch (unitLabel) {
case 'seconds':
milliseconds = duration * 1000;
break;
case 'minutes':
milliseconds = duration * 60 * 1000;
break;
case 'hours':
milliseconds = duration * 60 * 60 * 1000;
break;
}
const timerId = uuidv4(); // Generate a unique identifier for the timer
const serverId = message.guild.id;
if (!activeTimers[serverId]) {
activeTimers[serverId] = [];
}
try {
await sendMessage(message, `Timer set for a random duration of ${duration} ${unitLabel}.`);
activeTimers[serverId].push({ id: timerId, user: message.author.username, duration, unit: unitLabel, endTime: Date.now() + milliseconds });
}
catch (error) {
console.error('Error sending timer message:', error);
return false;
}
setTimeout(async () => {
try {
const timer = activeTimers[serverId].find(timer => timer.id === timerId);
if (timer) {
await sendMessage(message, `<@${message.author.id}> : ${duration} ${unitLabel} have passed.`);
activeTimers[serverId] = activeTimers[serverId].filter(timer => timer.endTime > Date.now());
}
}
catch (error) {
console.error('Error sending timer message:', error);
return false;
}
}, milliseconds);
return true;
}
//async function handleAlarmCommand(message, args) {}
module.exports = {
handlePingCommand,
handleTimeCommand,
handleUnknownCommand,
handleHelpCommand,
handleTimerCommand,
handleShowCommand,
handleResetCommand,
handleSoonCommand,
//handleAlarmCommand,
};