This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
115 lines (96 loc) · 3.16 KB
/
main.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
import {REST} from '@discordjs/rest';
import {Routes} from 'discord-api-types/v9';
import Calendar from './getEvents.js';
import {Client, Intents, MessageEmbed} from 'discord.js';
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const GUILD_ID = "927943017468416030";
const CLIENT_ID = "928685167034376233";
const TOKEN = process.env.DISCORD_TOKEN;
const commands = [
{
name: 'help',
description: "Voir la page d'aide"
}, {
name: 'mois',
description: 'Voir les rendus pour le mois'
}, {
name: 'semaine',
description: 'Voir les rendus pour la semaine'
}
];
/**
* Fetch and format upcoming events
* @param timeframe "week" or "month"
* @returns {Promise<MessageEmbed>} MessageEmbed object to directly send to discord
*/
async function createEventsEmbeds(timeframe) {
let calendar = await new Calendar;
switch (timeframe) {
case "week":
await calendar.fetch_week();
break;
case "month":
await calendar.fetch_month();
break;
}
return new MessageEmbed()
.setColor('#f58820')
.setTitle(`Rendus pour la semaine ${calendar.week_number()}`)
.setURL('https://moodle.univ-ubs.fr/')
.setThumbnail('https://ozna.me/moodle_logo.png')
.addFields(calendar.events)
.setTimestamp();
}
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity('Moodle', { type: 'WATCHING' });
});
client.on('interactionCreate', async interaction => {
let res;
if (!interaction.isCommand()) return;
switch (interaction.commandName) {
case "help":
let msg = [];
commands.forEach(command => {
msg.push({
name: `\`${command.name}\``,
value: command.description,
inline: true
});
})
res = new MessageEmbed()
.setColor('#f58820')
.setTitle("Page d'aide")
.setDescription(`Vous avez ${commands.length} commandes à votre disposition :`)
.setURL('https://github.com/finxol/moodisc')
.setThumbnail('https://ozna.me/moodle_logo.png')
.addFields(msg)
.setTimestamp();
await interaction.reply({ embeds: [res] });
break;
case "mois":
res = await createEventsEmbeds("month");
await interaction.reply({ embeds: [res] });
break;
case "semaine":
res = await createEventsEmbeds("week");
await interaction.reply({ embeds: [res] });
break;
default:
break;
}
});
client.login(TOKEN);
const rest = new REST({ version: '9' }).setToken(TOKEN);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();