-
Notifications
You must be signed in to change notification settings - Fork 0
/
indes.js
138 lines (89 loc) · 3.51 KB
/
indes.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
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const axios = require('axios');
/* ---- bot permisssions allowed - to access msg by user (msg content permission) ----- */
/*--msg reply (guild messages)--- */
// guild - server
// intents includes the bot permissions allowed
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const apiKey = process.env.API_KEY;
//"Sona_8112:e3fb4eb67bae124d409b4c83808a56239c007479";
const reminders = new Map();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
setInterval(checkfun, 60 * 1000);
});
// manual typing by user - command -'cp' - to get the contest details as msg in the discord server.
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
// command -- cp (to get details at any time)
if (message.content === "cp") {
// api fetched details
try {
const response = await axios.get("https://clist.by/api/v4/contest/", {
headers: {
"Authorization": `ApiKey ${apiKey}`
}
});
const contests = response.data.objects;
// filtered data including contests which are upcoming , (compared using present time (new Date()) and start time of contest)
const data = contests.filter(contest => new Date(contest.start) > new Date());
if (!data || data.length === 0) {
message.reply("No contests found !");
} else {
let list = data.map(contest => {
return `**${contest.event}** hosted by **${contest.host}**\nStarts: ${new Date(contest.start).toLocaleString()}\nEnds: ${new Date(contest.end).toLocaleString()}\nLink: ${contest.href}`;
}).join('\n\n');
if (list.length > 2000) {
const msgsplit = splitIntoChunks(list, 2000);
for (let part of msgsplit) {
await message.reply(part);
}
} else {
message.reply(list);
}
}
} catch (error) {
console.log(error);
}
}
});
async function checkfun() {
try {
const response = await axios.get("https://clist.by/api/v4/contest/", {
headers: {
"Authorization": `ApiKey ${apiKey}`
}
});
const contests = response.data.objects;
const upcontests = contests.filter(contest => {
const start = new Date(contest.start);
const present = new Date();
const diff = start - present;
const val = diff > 0 && diff <= 30*60*1000;
return val;
});
upcontests.forEach(contest => {
if (!reminders.has(contest.id)) {
const remindtime = new Date(contest.start) - new Date() - 30*60*1000;
setTimeout(() => {
send_reminder(contest);
reminders.delete(contest.id);
}, remindtime);
reminders.set(contest.id, true);
}
});
} catch (error) {
console.log("Error checking for upcoming contests:", error);
}
}
function send_reminder(contest) {
const remindermsg = `**Reminder!**\n\n Contest **${contest.event}** hosted by **${contest.host}** is starting in 30 minutes!\nLink: ${contest.href}`;
const channel = client.channels.cache.find(channel => channel.name === 'general');
if (channel) {
channel.send(remindermsg);
} else {
console.log("error");
}
}
client.login(process.env.TOKEN);