-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
130 lines (114 loc) · 4.29 KB
/
server.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
// server.js
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const DiscordStrategy = require('passport-discord').Strategy;
const fs = require('fs');
const publicFolders = fs.readdirSync('public');
const path = require('path');
require('dotenv').config();
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./botDatabase.db');
const bodyParser = require('body-parser');
const app = express();
// Session setup must be before passport initialization
app.use(session({
secret: 'some secret',
resave: false,
saveUninitialized: false,
}));
// Passport setup
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((obj, done) => done(null, obj));
passport.use(new DiscordStrategy({
clientID: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
callbackURL: 'http://localhost:3000/auth/discord/callback',
scope: ['identify', 'guilds']
}, (accessToken, refreshToken, profile, done) => {
done(null, profile);
}));
// Serve static files with specific routes
publicFolders.forEach(folder => {
if (folder !== 'dashboard' && folder !== 'login') {
app.use(`/${folder}`, express.static(path.join('public', folder)));
}
});
// Serve dashboard and login under the root route based on authentication status
app.get('/', (req, res) => {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, 'public', 'dashboard', 'index.html'));
} else {
res.sendFile(path.join(__dirname, 'public', 'login', 'index.html'));
}
});
// Serve static files for dashboard and login explicitly
app.use('/dashboard', express.static(path.join('public', 'dashboard')));
app.use('/login', express.static(path.join('public', 'login')));
// Authentication routes
app.get('/auth/discord', passport.authenticate('discord'));
app.get('/auth/discord/callback', passport.authenticate('discord', {
failureRedirect: '/'
}), (req, res) => {
res.redirect('/');
});
// Provide user data
app.get('/get-user', ensureAuthenticated, (req, res) => {
res.json(req.user);
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
// dashboard API routes
app.get('/bot-status', (req, res) => {
const totalServers = client.guilds.cache.size;
const totalMembers = client.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0);
res.json({ servers: totalServers, members: totalMembers });
});
app.get('/get-server-config', ensureAuthenticated, (req, res) => {
const serverId = req.query.serverId;
const guild = client.guilds.cache.get(serverId);
if (!guild) {
return res.status(404).send('Server not found');
}
if (guild.ownerId !== req.user.id) {
return res.status(403).send('Unauthorized: You are not the owner of this server');
}
db.get(`SELECT * FROM servers WHERE server_id = ?`, [serverId], (err, row) => {
if (err) {
console.error('Failed to retrieve server config:', err);
res.status(500).send('Error fetching server configuration');
} else {
res.json({
welcome_enabled: row.welcome_enabled,
goodbye_enabled: row.goodbye_enabled
});
}
});
});
app.use(bodyParser.json()); // Middleware to parse JSON bodies
app.post('/update-server-config', ensureAuthenticated, (req, res) => {
const { serverId, configKey, configValue } = req.body;
const guild = client.guilds.cache.get(serverId);
if (!guild) {
return res.status(404).send('Server not found');
}
if (guild.ownerId !== req.user.id) {
return res.status(403).send('Unauthorized: You are not the owner of this server');
}
const sql = `UPDATE servers SET ${configKey} = ? WHERE server_id = ?`;
db.run(sql, [configValue, serverId], (err) => {
if (err) {
console.error('Failed to update server config:', err);
res.status(500).send('Error updating server configuration');
} else {
res.send('Server configuration updated successfully');
}
});
});
app.listen(3000, () => console.log('Server running on http://localhost:3000'));