-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
77 lines (60 loc) · 1.96 KB
/
index.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
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Intents } = require('discord.js');
const express = require('express')
const {fetchEvents} = require('./utils/eventfetcher')
const {generateCal} = require('./utils/icalgen.js')
const config = require('./config/config.json')
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
/*
* WEB SERVER
*/
const app = express()
const port = 3042
// router for fetching a guild's events in ical format
app.get('/cal/:guildID', (req, res) => {
const guild = client.guilds.cache.get(req.params.guildID)
if (guild) {
// if guild found fetch events, generate calendar and send the file
fetchEvents(guild).then(events => {
res.send(generateCal(events, guild.name))
})
} else {
res.status(404).send("guild not found")
}
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
/*
* DISCORD BOT
*/
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
// register slash commands, discord boilerplate
const commands = [{
name: 'events',
description: 'Sends an iCal link to the server events'
}];
const rest = new REST({ version: '9' }).setToken(config.token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
});
// called on command execs
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'events') {
await interaction.reply("Here's a link to the iCal feed: " + config.baseUrl + "/cal/" + interaction.guild.id);
}
});
client.login(config.token);