forked from SSnowly/warden-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
160 lines (144 loc) · 5.22 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
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
const express = require('express');
const { PrismaClient } = require('@prisma/client');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const prisma = new PrismaClient();
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// good think to have if you remmeber the link its better with /discord than discord link imo
app.get('/discord', async (req, res) => {
res.redirect("https://discord.gg/MVNZR73Ghf")
})
// do not tocuh like seriusly if you touch it it may break, it is hold up by a string and its trying its best to survive
app.get('/', async (req, res) => {
const { view = 'users', page = 1, search = '' } = req.query; // this is for no reason, i could've done /users and /badservers but this is easier and a one page solution while still using one page
const pageSize = 25; // you can change this, this just changes how much is shown, does not help the database requests :kekw: (i mean it does but count is allways got)
try {
let dataType;
let data, count;
const skip = (page - 1) * pageSize;
if (isNaN(data)) {
dataType == "name"
} else {
dataType == "id"
}
console.log(dataType);
if (view === 'badservers') {
if (dataType === "name") {
count = await prisma.badServers.count({
where: {
name: {
contains: search,
}
}
});
data = await prisma.badServers.findMany({
where: {
name: {
contains: search,
}
},
include: {
Users: true
},
skip,
take: pageSize
});
} else {
count = await prisma.badServers.count({
where: {
id: {
contains: search,
}
}
});
data = await prisma.badServers.findMany({
where: {
id: {
contains: search,
}
},
include: {
Users: true
},
skip,
take: pageSize
});
}
} else {
count = await prisma.users.count({
where: {
id: {
contains: search,
}
}
});
data = await prisma.users.findMany({
where: {
id: {
contains: search,
}
},
skip,
take: pageSize
});
}
const totalPages = Math.ceil(count / pageSize);
res.render('index', { view, data, page, totalPages, search });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Internal server error');
}
});
// this is what allows u to do url/users with body of a userid and returns data.
app.get('/user', async (req, res) => {
const { user_id } = req.query;
if (!user_id) {
return res.status(400).json({ success: false, message: 'Discord user ID is required' });
}
try {
const user = await prisma.users.findUnique({
where: {
id: user_id,
},
});
const userExists = !!user;
if (userExists) {
return res.json({ success: userExists, userid: user.id, last_username: user.last_username, status: user.status });
} else {
return res.json({ success: false, message: "This user is not in the database"})
}
} catch (error) {
console.error('Error checking user:', error);
return res.status(500).json({ success: false, message: 'Internal server error' });
}
});
app.get('/server', async (req, res) => {
const { server_id } = req.query;
if (!server_id) {
return res.status(400).json({ success: false, message: 'Discord Server ID is required' });
}
try {
const server = await prisma.badServers.findUnique({
where: {
id: server_id,
},
});
const serverExists = !!server;
if (serverExists) {
return res.json({ success: serverExists, id: server.id, name: server.name, reason: server.reason });
} else {
return res.json({ success: false, message: "This server is not in the database"})
}
} catch (error) {
console.error('Error checking user:', error);
return res.status(500).json({ success: false, message: 'Internal server error' });
}
});
// this starts the server, if you dont do this, it will actually not work so start IT
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});