This repository has been archived by the owner on Aug 18, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (90 loc) · 2.33 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
process.title = 'Rush'
const Config = require('./config.json')
const Websocket = require('ws')
const Discordie = require('discordie')
const bot = new Discordie({
autoReconnect: true
})
const EventEmitter = require('events')
const Dispatch = new EventEmitter()
const ReqDir = require('require-directory')
let modules = ReqDir(module, './modules')
let WS
function init () {
WS = new Websocket(Config.bezerkURI)
}
init()
for (var mod in modules) {
if (typeof modules[mod].init !== 'function') console.warn('Cannot load a module due to the init function being invalid.')
else modules[mod].init(Dispatch, bot.Dispatcher)
}
bot.connect({
token: Config.token
})
bot.Dispatcher.on('DISCONNECTED', () => {
if (WS !== undefined) WS.close()
WS = undefined
})
bot.Dispatcher.on('GATEWAY_READY', () => {
console.log('Rush is ready!')
send('IDENTIFY_LISTENER', ['*']) // we want fucking EVERYTHING
})
Dispatch.on('QUESTION', (k) => {
k.shard !== undefined ? send(k.op, k.c, k.shard) : send(k.op, k.c)
})
WS.on('message', (c) => {
var data = JSON.parse(c)
receive(data)
})
WS.on('close', () => {
console.log('Lost connection, attempting to reconnect...')
init()
})
function send (opCode, data, shard) {
if (!shard) {
WS.send(JSON.stringify({
op: opCode,
c: data
}))
} else {
WS.send(JSON.stringify({
op: opCode,
shard: shard,
c: data
}))
}
}
function receive (data) {
switch (data.op) {
case 'HELLO': {
console.log('Hello from Bezerk!')
break
}
case 'OK': {
console.log('Bezerk connection established!')
send('COUNT', '')
break
}
case 'COUNT_REPLY' : {
console.log(`Bezerk is connected to ${data.c.shards} shards, and ${data.c.listeners} listeners.`)
Dispatch.emit(data.op, data)
break
}
case 'SHARD_JOINED' : {
console.log(`Shard ${data.c} just connected to Bezerk`)
Dispatch.emit(data.op, data)
Dispatch.emit('ANY', data)
break
}
case 'SHARD_LEFT' : {
console.log(`Shard ${data.c} dropped from Bezerk`)
Dispatch.emit(data.op, data)
Dispatch.emit('ANY', data)
break
}
default: {
Dispatch.emit(data.op, data)
Dispatch.emit('ANY', data)
}
}
}