-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
86 lines (79 loc) · 2.08 KB
/
main.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
const WebSocket = require('ws');
const Discord = require("discord.js");
const db = require(__dirname + "/db")
const client = new Discord.Client();
var options = require(__dirname + '/options');
ready = false
const wss = new WebSocket.Server({ host: 'localhost', port: 8080, verifyClient: LocalOnlyVerify});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
if(message != null) {
let spl = message.split(' ')
switch(spl.length) {
case 3:
switch(spl[0]) {
case 'role_add':
AddRole(spl[1], spl[2])
break
case 'role_remove':
RemoveRole(spl[1], spl[2])
break
}
break
default:
return
}
}
});
});
client.on('ready', () => {
ready = true
console.log('bot connected.')
db.PullQueue(function (queue, callback){
if (queue != null) {
for (i = 0; i < queue.length; i++) {
if (queue[i].adding) {
AddRole(queue[i].uid, queue[i].role)
}
else {
RemoveRole(queue[i].uid, queue[i].role)
}
}
callback()
}
})
});
function GetRole(roleName) {
let retRole = undefined
client.guilds.get(options.storageConfig.discord_guild_id).roles.forEach(
function(role) {
if (role.name.toUpperCase() == roleName.toUpperCase()) {
retRole = role
return
}
})
return retRole
}
function AddRole(user, role) {
if (ready) {
client.guilds.get(options.storageConfig.discord_guild_id).members.get(user).addRole(GetRole(role))
}
else {
db.WriteToRoleQueue(user, true, role)
}
}
function RemoveRole(user, role) {
if (ready) {
client.guilds.get(options.storageConfig.discord_guild_id).members.get(user).removeRole(GetRole(role))
}
else {
db.WriteToRoleQueue(user, false, role)
}
}
function LocalOnlyVerify(info) {
if(info.req.headers.host.split(':')[0] == '127.0.0.1' || !localOnly)
return true
return false
}
console.log('bot connecting')
client.login(options.storageConfig.discord_bot_token)