-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_utils.js
More file actions
82 lines (77 loc) · 2.05 KB
/
common_utils.js
File metadata and controls
82 lines (77 loc) · 2.05 KB
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
// Common utils for HCNBot.
// Include Chat utils and so on.
const repl = require('repl')
const proc = require('process')
const sprintf = require('sprintf-js').sprintf
const defs = require('./defs')
const validateCfg = (cfg, st) => {
let ret = true;
if (cfg == undefined) return false;
if (typeof st == "object") {
for (var i in st) ret &= validateCfg(cfg[i], st[i]);
} else {
ret = typeof cfg == typeof st;
}
return ret;
}
exports.validateConfig = (cfg) => {
return validateCfg(
{
"account": {"email": "", "password": ""},
"server": {"host": "", "port": 0}
},
cfg
)
}
exports.initializeConsole = () => {
r = repl.start({prompt: '> ', eval: (a,b,c,d) => d(new repl.Recoverable()) })
lg = r.context.lg = (a) => {
a.split('\n').forEach((b) => {
const line = '' + r._prompt + r.line
const output = sprintf(`%s[2K%s[1G${b}\n${line}%s[${line.length + 1}G`, '\033', '\033', '\033')
proc.stdout.write(output)
})
}
r.defineCommand('ac', {
help: 'Send chat messages',
action(msg) {
this.displayPrompt(false)
if (this.context.bot.chat) this.context.bot.chat(`/ac ${msg}`)
this.displayPrompt(true)
}
})
r.defineCommand('gc', {
help: 'Send guild chat messages',
action(msg) {
this.displayPrompt(false)
if (this.context.bot.chat) this.context.bot.chat(`/gc ${msg}`)
this.displayPrompt(true)
}
})
r.defineCommand('pc', {
help: 'Send party chat messages',
action(msg) {
this.displayPrompt(false)
if (this.context.bot.chat) this.context.bot.chat(`/pc ${msg}`)
this.displayPrompt(true)
}
})
r.defineCommand('c', {
help: 'Send commands',
action(msg) {
this.displayPrompt(false)
if (this.context.bot.chat) this.context.bot.chat(`/${msg}`)
this.displayPrompt(true)
}
})
return [lg, r]
}
exports.generateBotArguments = (_) => {
return {
"username": _.account.email,
"password": _.account.password,
"host": _.server.host,
"port": _.server.port,
"version": defs.version
}
}