mirror of
https://github.com/arthur-pbty/LazyBot.git
synced 2026-06-03 23:36:37 +02:00
add Discord Bot
This commit is contained in:
+43
-3
@@ -5,6 +5,9 @@ const session = require("express-session");
|
||||
const fetch = require("cross-fetch"); // fetch compatible Node
|
||||
const path = require("path");
|
||||
|
||||
// importer le bot
|
||||
const client = require("./bot");
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
@@ -14,7 +17,7 @@ const REDIRECT_URI = process.env.REDIRECT_URI;
|
||||
|
||||
// --- Session setup ---
|
||||
app.use(session({
|
||||
secret: "un_secret_long_et_complexe", // change-le en production
|
||||
secret: process.env.SESSION_SECRET,
|
||||
resave: false,
|
||||
saveUninitialized: true,
|
||||
}));
|
||||
@@ -24,7 +27,7 @@ app.use(express.static(path.join(__dirname, "public")));
|
||||
|
||||
// --- Route pour démarrer la connexion Discord ---
|
||||
app.get("/auth/discord", (req, res) => {
|
||||
const url = `https://discord.com/api/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(REDIRECT_URI)}&response_type=code&scope=identify`;
|
||||
const url = `https://discord.com/api/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(REDIRECT_URI)}&response_type=code&scope=identify%20guilds`;
|
||||
res.redirect(url);
|
||||
});
|
||||
|
||||
@@ -56,11 +59,17 @@ app.get("/auth/discord/callback", async (req, res) => {
|
||||
const userResponse = await fetch("https://discord.com/api/users/@me", {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
|
||||
const user = await userResponse.json();
|
||||
|
||||
// Récupérer la liste des guilds
|
||||
const guildsResponse = await fetch("https://discord.com/api/users/@me/guilds", {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
const guilds = await guildsResponse.json();
|
||||
|
||||
// Stocker l'utilisateur dans la session
|
||||
req.session.user = user;
|
||||
req.session.guilds = guilds;
|
||||
|
||||
// Rediriger vers la page HTML
|
||||
res.redirect("/welcome.html");
|
||||
@@ -79,5 +88,36 @@ app.get("/api/user", (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/guilds", (req, res) => {
|
||||
const userGuilds = req.session.guilds; // toutes les guilds de l'utilisateur
|
||||
if (!userGuilds) return res.status(401).json({ error: "Utilisateur non connecté" });
|
||||
|
||||
// Liste des guilds où le bot est présent
|
||||
const botGuildIds = client.guilds.cache.map(g => g.id);
|
||||
|
||||
// Filtrer : bot présent + admin
|
||||
const validGuilds = userGuilds.filter(g => {
|
||||
const hasAdmin = (g.permissions & 0x8) === 0x8; // flag admin
|
||||
const botHere = botGuildIds.includes(g.id);
|
||||
return hasAdmin && botHere;
|
||||
});
|
||||
|
||||
app.get("/invite-bot", (req, res) => {
|
||||
const clientId = process.env.CLIENT_ID;
|
||||
const redirectUri = encodeURIComponent(process.env.REDIRECT_URI);
|
||||
const permissions = 8; // Permissions administrateur
|
||||
const scope = encodeURIComponent("bot guilds applications.commands");
|
||||
|
||||
const url = `https://discord.com/oauth2/authorize?client_id=${clientId}&permissions=${permissions}&scope=${scope}&redirect_uri=${redirectUri}&response_type=code`;
|
||||
|
||||
// Tu peux juste renvoyer le lien
|
||||
res.json({ url });
|
||||
});
|
||||
|
||||
|
||||
res.json(validGuilds);
|
||||
});
|
||||
|
||||
|
||||
// --- Lancement du serveur ---
|
||||
app.listen(PORT, () => console.log(`Serveur lancé sur http://localhost:${PORT}`));
|
||||
|
||||
Reference in New Issue
Block a user