-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
236 lines (215 loc) · 6.65 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require("dotenv").config();
const cookieParser = require("cookie-parser");
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const serveIndex = require("serve-index");
const { Client, GatewayIntentBits, Partials } = require("discord.js");
const rateLimit = require("./Discord/DiscordEvents/rateLimit");
const ready = require("./Discord/DiscordEvents/ready");
const guildMemberAdd = require("./Discord/DiscordEvents/guildMemberAdd");
const message = require("./Discord/DiscordEvents/message");
const voiceStateUpdate = require("./Discord/DiscordEvents/voiceStateUpdate");
const presenceUpdate = require("./Discord/DiscordEvents/presenceUpdate");
const connexion = require("./routes/connexion");
const attribuerRole = require("./routes/attribuerRole");
const cron = require("./routes/cron");
const home = require("./routes/home");
/**
*
*
* MONITORING
*
*
* */
const Sentry = require("@sentry/node");
Sentry.init({
enableTracing: true,
});
/**
*
*
* Variables d'environnement
*
*
* */
[
["APP", process.env.APP],
["BOT_PREFIX", process.env.BOT_PREFIX],
["BOT_URL", process.env.BOT_URL],
["BOT_TOKEN", process.env.BOT_TOKEN],
["SERVER_ID", process.env.SERVER_ID],
["CHANNEL_ADMIN_ID", process.env.CHANNEL_ADMIN_ID],
["ROLE_ENSEIGNANT_ID", process.env.ROLE_ENSEIGNANT_ID],
["ROLE_ETUDIANT_ID", process.env.ROLE_ETUDIANT_ID],
["ROLE_ANCIEN_ETUDIANT_ID", process.env.ROLE_ANCIEN_ETUDIANT_ID],
["ROLE_VACANCES_ENSEIGNANT_ID", process.env.ROLE_VACANCES_ENSEIGNANT_ID],
["BOT_PREFIX", process.env.BOT_PREFIX],
["CATEGORY_AMPHI", process.env.CATEGORY_AMPHI],
["WEB_LISTEN", process.env.WEB_LISTEN],
["LOG_FILE", process.env.LOG_FILE],
["CRON_SECRET", process.env.CRON_SECRET],
["DISCORD_LISTEN", process.env.DISCORD_LISTEN],
].forEach((env) => {
if (!env[1]) {
console.error(`La variable d'env ${env[0]} n'est pas définie !`);
process.exit(-1);
}
});
if (!process.env.VACANCES) process.env.VACANCES = "0";
if (!process.env.BOT_URL)
process.env.BOT_URL = "l'url publique du bot n'est pas définie";
if (!process.env.LIEN_INVITATION_DISCORD)
process.env.LIEN_INVITATION_DISCORD = "pas de lien d'invitation";
const nameOverride = {};
/* eslint-disable no-restricted-syntax */
if (process.env.NAME_OVERRIDE) {
for (const userAndName of process.env.NAME_OVERRIDE.split(",")) {
const arrayUserAndName = userAndName.split(":");
nameOverride[arrayUserAndName[0]] = arrayUserAndName[1];
}
}
let bannedLoginUsers = [];
if (process.env.BANNED_LOGIN_USERS) {
bannedLoginUsers = process.env.BANNED_LOGIN_USERS.split(",");
}
/* eslint-enable no-restricted-syntax */
/**
*
*
* PARTIE DISCORD
*
*
* */
const intents = [
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
];
const client = new Client({ intents, partials: [Partials.Channel] });
if (process.env.WATCH_RATE_LIMIT) {
client.on("rateLimit", (rateLimitInfo) => rateLimit(rateLimitInfo));
}
const additionalRoles = {};
if (process.env.DISCORD_LISTEN === "1") {
/** Un tableau[channelTexte] = channelVocal associé */
/** Utilisé pour vérifier si channel voix existe déjà pour un chan texte */
const tableauChannelTexteAChannelVocal = [];
/** Structure : tableauChannelsVocauxEnCours[member.id] = listedesChannelsIDGenDyn */
/** Utilisé pour supprimer les chan qui ont été générés dynamiquement */
const tableauChannelsVocauxEnCours = [];
/** Quand le bot se lance */
client.on("ready", () => {
ready(client);
});
/**
* Quand un utilisateur rejoint le serveur, on lui envoie un message de bienvenue pour lui dire de se connecter au site etu
*/
client.on(
"guildMemberAdd",
(/** import("discord.js").GuildMember */ member) => {
guildMemberAdd(member);
},
);
/** Si le bot reçoit un message en privé, ou sur l'un des channels qu'il peut voir */
client.on(
"messageCreate",
async (/** import("discord.js").Message */ msg) => {
await message(
msg,
tableauChannelTexteAChannelVocal,
tableauChannelsVocauxEnCours,
);
},
);
if (process.env.WATCHED_MEMBERS) {
const watchedMembers = process.env.WATCHED_MEMBERS.split(",");
client.on(
"presenceUpdate",
async (
/** 'import("discord.js").Presence */ oldPresence,
/** 'import("discord.js").Presence */ newPresence,
) => {
await presenceUpdate(oldPresence, newPresence, watchedMembers);
},
);
}
client.on(
"voiceStateUpdate",
async (
/** import("discord.js").VoiceState */ oldState,
/** import("discord.js").VoiceState */ newState,
) => {
await voiceStateUpdate(
oldState,
newState,
tableauChannelTexteAChannelVocal,
tableauChannelsVocauxEnCours,
);
},
);
}
client.login(process.env.BOT_TOKEN).catch(console.error);
/**
*
*
* PARTIE WEB
*
*
* */
const app = express();
app.use(express.static(path.join(__dirname, "public")));
if (process.env.WEB_LISTEN === "1") {
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "twig");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
if (process.env.SITE_ETU_CLIENT_ID && process.env.SITE_ETU_CLIENT_SECRET) {
app.use("/connexion", connexion);
app.use(
"/attribuerrole",
attribuerRole(client, nameOverride, bannedLoginUsers, additionalRoles),
);
app.use(
`/cron/${process.env.CRON_SECRET}`,
cron(client, nameOverride, bannedLoginUsers, additionalRoles),
);
app.use("/", home);
} else {
app.get("/", (req, res) => {
res.send(
"La connexion avec le site etu n'est pas possible en raison d'une mauvaise configuration du bot, ou alors <a href='https://etu.utt.fr'>le site etu</a> n'est pas accessible. Ressayez plus tard.",
);
});
}
if (
process.env.DISCORD_CHAT_EXPORT_PATH &&
process.env.DISCORD_CHAT_EXPORTER_EXE_PATH
) {
app.use(
"/exports",
express.static("public/exports"),
serveIndex("public/exports", { icons: true }),
);
}
}
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use((err, req, res) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;