-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (55 loc) · 2.44 KB
/
app.js
File metadata and controls
67 lines (55 loc) · 2.44 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
import { Client, Events, GatewayIntentBits, EmbedBuilder } from "discord.js";
import { queryFinals, queryIterations } from './notion.js';
import { config } from 'dotenv';
config();
const bot_token = process.env.DISCORD_BOT_TOKEN;
const channel_id = process.env.DISCORD_CHANNEL_ID;
const user_map = JSON.parse(process.env.NOTION_TO_DISCORD_MAP);
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const formatAssignments = (assignments) => {
let message = ``;
for (const [key, value] of assignments) {
let date = value.date.substring(5).replace("-", "/");
if (value.assignedTo in user_map) {
message += `**${date}** — ${key} by <@${user_map[value.assignedTo]}>\n`;
} else if (value.assignedTo) {
message += `**${date}** — ${key} by *Unknown*\n`;
} else {
message += `**${date}** — ${key} by *Unassigned*\n`;
}
}
if (message === ``) {
message = `*None upcoming*\n`
}
return message
};
client.once(Events.ClientReady, async readyClient => {
const channel = client.channels.cache.get(channel_id);
const final_assignments = await queryFinals();
const iteration_assignments = await queryIterations();
// If there are no assignments, don't send a message
if (final_assignments.size == 0 && iteration_assignments.size == 0) {
await client.destroy();
process.exit(0);
}
let final_string = formatAssignments(final_assignments);
let iteration_string = formatAssignments(iteration_assignments);
const d = new Date();
const date = d.toLocaleString('en-US', { month: '2-digit', day: '2-digit' });
const next_date = new Date(d.getTime() + 7 * 24 * 60 * 60 * 1000).toLocaleString('en-US', { month: '2-digit', day: '2-digit' })
// const embed = new EmbedBuilder()
// .setTitle(`Upcoming Deadlines: ${date} - ${next_date}`)
// .setDescription(
// `**__Final Deadlines__**
// ${final_string}
// **__First Iteration Deadlines__**
// ${iteration_string}`
// )
// .setColor('#ff943d');
const message_text = `## Upcoming Deadlines: ${date} - ${next_date}\n**__Final Deadlines__**\n${final_string}\n**__First Iteration Deadlines__**\n${iteration_string}`;
// channel.send({ embeds: [embed] });
await channel.send({ content: message_text });
await client.destroy();
process.exit(0);
});
client.login(bot_token)