-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (142 loc) · 3.72 KB
/
index.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
const { Client } = require('discord.js');
const express = require('express');
const fetch = require('node-fetch');
const config = require('./config');
const app = express();
const client = new Client({
restTimeOffset: 1000
});
const botOnline = (req, res, next) => {
if (client.status === 0) {
next();
} else {
res.json({
ok: false,
message: 'The Discord bot is offline',
languages: 'errors.botserver.offline'
});
}
};
const isAuthenticated = (req, res, next) => {
if (req.get('Authorization') === config.webserver.authorization) {
next();
} else {
res.status(400).json({
ok: false,
message: 'You are not allowed to fetch the Bot server',
languages: 'errors.botserver.unauthorised'
});
}
};
const userExists = (req, res, next) => {
const member = req.guild.members.get(req.params.id);
if (member) {
req.member = member;
next();
} else {
res.json({
ok: false,
message: 'The user was not found by the server',
languages: 'errors.botserver.usernotfound'
});
}
};
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
app
.set('json spaces', 4)
.use(botOnline)
.use(isAuthenticated)
.use((req, res, next) => {
req.guild = client.guilds.get(config.guild);
if (req.guild) {
next();
} else {
res.json({
ok: false,
message: 'The guild was not found by the server',
languages: 'errors.botserver.guildnotfound'
});
}
})
.get('/status/:filter', (req, res) => {
res.json({
ok: true,
data: client.users.filter(user => user.presence.status === req.params.filter).array().map(user => user.id)
});
})
.get('/in/:id', userExists, (req, res) => {
res.json({
ok: true
});
})
.get('/fixroles', (req, res, next) => {
fetch(`${config.website}/api/v2/bots`)
.then(json => json.json())
.then((data) => {
if (data.ok) {
return data.data;
}
return new Error('Cannot fetch list of bots');
})
.then(data => data.filter(bot => bot.state === 'approved'))
.then((data) => {
const authors = {};
data.forEach(bot => bot.authors.forEach((author) => {
authors[author] = true;
}));
return Object.keys(authors);
})
.then((users) => {
const [add, remove] = req.guild.members
.filter(member => !member.user.bot)
.partition(member => users.includes(member.user.id));
return Promise.all(
add.map(member => member.addRole(config.roles.dev, 'ls.terminal.ink adjustments')),
remove.map(member => member.removeRole(config.roles.dev, 'ls.terminal.ink adjustments'))
);
})
.then(() => {
res.json({
ok: true
});
})
.catch((err) => {
next(err);
});
})
.get('/addrole/:id', userExists, (req, res, next) => {
req.member.addRole(config.roles.dev, 'ls.terminal.ink adjustments')
.then(() => res.json({
ok: true
}))
.catch(err => next(err));
})
.get('/removerole/:id', userExists, (req, res, next) => {
req.member.removeRole(config.roles.dev, 'ls.terminal.ink adjustments')
.then(() => res.json({
ok: true
}))
.catch(err => next(err));
})
.use((err, req, res, next) => {
if (err) {
res.json({
ok: false,
message: err.stack
});
} else {
next();
}
});
client.login(config.discord.token);
const http = app.listen(config.webserver.port);
// When CTRL+C is caught...
process.on('SIGINT', () => {
console.log('Goodnight!');
// Close the Express.js server
http.close(() => {
console.log('No longer listening');
});
});