-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (65 loc) · 2.27 KB
/
Copy pathindex.js
File metadata and controls
87 lines (65 loc) · 2.27 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
import { Client } from 'discord.js';
import { config } from 'dotenv';
import cron from 'node-cron';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration.js';
import relativeTime from 'dayjs/plugin/relativeTime.js';
config();
dayjs.extend(duration);
dayjs.extend(relativeTime);
const client = new Client({
intents: ['Guilds', 'DirectMessages']
});
// Constants
const TARGET_DATE = '2025-02-01';
const TARGET_USER_ID = process.env.TARGET_USER_ID;
let messageCount = 0;
if (!TARGET_USER_ID) {
throw new Error('Missing TARGET_USER_ID!!');
}
if (!process.env.DISCORD_TOKEN) {
throw new Error('Missing DISCORD_TOKEN!!');
}
const getRemainingHours = (targetDate) => {
const now = dayjs();
const diff = targetDate.diff(now);
const hours = Math.floor(dayjs.duration(diff).asHours());
return `${hours} hours`;
};
const sendCountdownMessage = async () => {
try {
const now = dayjs();
const targetDate = dayjs(TARGET_DATE);
const currentTimestamp = Math.floor(now.valueOf() / 1000);
if (now.isAfter(targetDate)) {
console.log('Target date has passed. Stopping bot...');
process.exit(0);
}
const remainingTime = getRemainingHours(targetDate);
const user = await client.users.fetch(TARGET_USER_ID);
const message = `Time Sent:<t:${currentTimestamp}:F>\nJust letting you know that February 1st, 2025 is ${remainingTime} away!`;
await user.send(message);
messageCount++;
const ordinal = (n) => {
const s = ["th", "st", "nd", "rd"];
const v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
console.log(`DM sent to ${user.tag} - ${ordinal(messageCount)} message`);
} catch (error) {
console.error('Error sending DM:', error);
}
};
client.once('ready', () => {
console.log(`Bot is ready! Logged in as ${client.user?.tag}`);
cron.schedule('0 * * * *', sendCountdownMessage);
sendCountdownMessage();
});
client.on('error', error => {
console.error('Discord client error:', error);
});
client.login(process.env.DISCORD_TOKEN)
.catch(error => {
console.error('Failed to login:', error);
process.exit(1);
});