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 pathchat.js
75 lines (63 loc) · 1.89 KB
/
chat.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
'use strict';
const xmlEntities = new require('html-entities').XmlEntities;
const zmq = require('zmq');
const uuid = require('uuid');
const redis = require('./redis');
const config = require('./config');
const proto = require('./proto');
const util = require('./util');
const zmqSocket = zmq.socket('push');
util.loadZMQConfig(config.zeromq.serverToBroker, zmqSocket);
const COLOR_CODES = /\u00a7./g;
const XML_TAGS = /<[^>]*>/g;
let discordBot;
module.exports = {
start() {
const zmqSocket = zmq.socket('sub');
util.loadZMQConfig(config.zeromq.brokerToServer, zmqSocket);
zmqSocket.on('message', (topic, messageProto) => {
var decoded = proto.ChatMessageOut.decode(messageProto);
if (decoded.type !== proto.MessageType.TEXT) {
return;
}
let targetChannel = null;
if (!decoded.to || decoded.to.type === proto.TargetType.ALL) {
targetChannel = config.channels.normal;
} else if(decoded.to.type === proto.TargetType.PERMISSION) {
decoded.to.filter.some(target => {
if (target === 'foxbukkit.opchat') {
targetChannel = config.channels.staff;
return true;
}
});
}
if (!targetChannel) {
return;
}
const filteredMessage = xmlEntities.decode(decoded.contents.replace(XML_TAGS, '').replace(COLOR_CODES, ''));
discordBot.sendMessage(targetChannel, filteredMessage);
});
zmqSocket.subscribe('CMO');
},
setBot (bot) {
discordBot = bot;
},
sendMessage (playerUuid, message) {
const context = uuid.v4();
return redis.hgetAsync('playerUUIDToName', playerUuid)
.then(playerName =>
zmqSocket.send((new proto.ChatMessageIn({
server: 'Discord',
from: {
uuid: util.writeProtobufUUID(playerUuid),
name: playerName,
},
timestamp: util.getUnixTime(),
context: util.writeProtobufUUID(context),
type: proto.MessageType.TEXT,
contents: message,
})).encode().toBuffer())
)
.thenReturn(context);
},
};