forked from xhan/qqbot
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmain.js
executable file
·74 lines (67 loc) · 2.05 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
#!/usr/bin/env node
'use strict';
(function() {
var log = new (require('log'))('debug');
var auth = require("./src/qqauth-qrcode");
var api = require("./src/qqapi");
var QQBot = require("./src/qqbot");
var defaults = require('./src/defaults');
var config = require('./config');
var KEY_COOKIES = 'qq-cookies';
var KEY_AUTH = 'qq-auth';
/*
* 获取接口需要的cookie和token
* @param isneedlogin : 是否需要登录,or本地获取
* @param options : 配置文件涉及的内容
* @callback (cookies,auth_info)
*/
var get_tokens = function(isneedlogin, options, callback) {
var auth_info, cookies;
if (isneedlogin) {
return auth.login(options, function(cookies, auth_info) {
defaults.data(KEY_COOKIES, cookies);
defaults.data(KEY_AUTH, auth_info);
defaults.save();
return callback(cookies, auth_info);
});
} else {
cookies = defaults.data(KEY_COOKIES);
auth_info = defaults.data(KEY_AUTH);
log.info("skip login");
return callback(cookies, auth_info);
}
};
var run = function() {
"starting qqbot ...";
var isneedlogin, params;
params = process.argv.slice(-1)[0] || '';
isneedlogin = params.trim() !== 'nologin';
return get_tokens(isneedlogin, config, function(cookies, auth_info) {
var bot;
bot = new QQBot(cookies, auth_info, config);
bot.on_die(function() {
if (isneedlogin) {
return run();
}
});
return bot.update_all_members(function(ret) {
if(ret) {
log.info("Entering runloop, Enjoy!");
return bot.runloop();
} else {
log.error("获取信息失败,再次尝试");
return bot.update_all_members(function(ret) {
if(ret) {
log.info("Entering runloop, Enjoy!");
return bot.runloop();
} else {
log.error("获取信息失败,请重新运行");
process.exit(1);
}
});
}
});
});
};
run();
}).call(this);