This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
131 lines (108 loc) · 2.88 KB
/
util.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
'use strict';
const uuid = require('uuid');
const Long = require('long');
const redis = require('./redis');
const config = require('./config');
const ROLE_MAPPING = config.roles;
const MANAGED_ROLES = {};
for (const i in ROLE_MAPPING) {
MANAGED_ROLES[ROLE_MAPPING[i]] = true;
}
let guild;
module.exports.loadZMQConfig = function loadZMQConfig (config, socket) {
config.forEach(function (srv) {
switch (srv.mode.toLowerCase()) {
case 'bind':
socket.bind(srv.uri);
break;
case 'connect':
socket.connect(srv.uri);
break;
}
});
};
function getDiscordIDForUser (uuid) {
return redis.hgetAsync('discordlinks:mc-to-discord', uuid);
}
module.exports.getDiscordIDForUser = getDiscordIDForUser;
function getMCUUIDForUser (user) {
return redis.hgetAsync('discordlinks:discord-to-mc', user.id ? user.id : user);
}
module.exports.getMCUUIDForUser = getMCUUIDForUser;
function getMCRoleForUser (user) {
return getMCUUIDForUser(user)
.then(uuid => {
if (uuid) {
return redis.hgetAsync('playergroups', uuid);
}
return null;
})
.then(role => {
if (role && ROLE_MAPPING[role]) {
return role;
}
return null;
});
}
module.exports.getMCRoleForUser = getMCRoleForUser;
function syncMCRolesForUser (userId) {
return getMCRoleForUser(userId)
.then(redisMCRole => {
const correctManagedDiscordRole = ROLE_MAPPING[redisMCRole];
const user = guild.members.get(userId);
// We already have the valid role if it is "none"
let hasInvalidRoles = false;
let hasValidRole = !correctManagedDiscordRole;
const rawRoles = user.roles.array();
const correctRoles = [];
rawRoles.forEach(role => {
const roleId = role.id;
if (!MANAGED_ROLES[roleId]) {
correctRoles.push(roleId);
return;
}
if (roleId === correctManagedDiscordRole) {
hasValidRole = true;
} else {
hasInvalidRoles = true;
}
});
if (!hasValidRole || hasInvalidRoles) {
if (correctManagedDiscordRole) {
correctRoles.push(correctManagedDiscordRole);
}
return user.setRoles(correctRoles);
}
});
}
module.exports.syncMCRolesForUser = syncMCRolesForUser;
module.exports.writeProtobufUUID = function writeProtobufUUID (uuidUuid) {
var buffer = new ArrayBuffer(16); // 16 bytes = 128 bits
var view = new Uint8Array(buffer);
uuid.parse(uuidUuid, view, 0);
var dataview = new DataView(buffer);
return {
msb: new Long(dataview.getInt32(4), dataview.getInt32(0)),
lsb: new Long(dataview.getInt32(12), dataview.getInt32(8))
};
};
module.exports.getUnixTime = function () {
return Math.floor(Date.now() / 1000);
};
module.exports._setGuild = function (_guild) {
guild = _guild;
};
const subRedis = redis.newClient();
subRedis.on('message', (channel, message) => {
getDiscordIDForUser(message)
.then(id => {
if (!guild) {
return;
}
if (id) {
return syncMCRolesForUser(id);
}
})
.catch(err => console.error(err));
});
subRedis.subscribe('playerRankUpdate');