-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
114 lines (94 loc) · 3.41 KB
/
plugin.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
var parseArgs = require('minimist');
var format = require('util').format;
var google = require('./google');
// Will not change if 2 instances of tennu launched
const helps = {
"google": [
"{{!}}google <query>",
"Search google from IRC."
]
};
var TennuGoogle = {
configDefaults: {
"agoogle": {
"limitResults": 1,
"maxUserDefinedLimit": 3
},
},
init: function(client, imports) {
const googleRequestFailed = 'Failed to fetch results from Google.';
var googleConfig = client.config("agoogle");
var limitResults = googleConfig.limitResults;
var maxUserDefinedLimit = googleConfig.maxUserDefinedLimit;
// Validate config values
if (maxUserDefinedLimit < 1 || maxUserDefinedLimit > 8) {
client._logger.warn(format('tennu-agoogle: maxUserDefinedLimit must be between 1 and 8. Caugfht "%s" Defaulting to 1.', maxUserDefinedLimit));
maxUserDefinedLimit = 1;
}
if (limitResults < 1 || limitResults > 8) {
client._logger.warn(format('tennu-agoogle: limitResults must be between 1 and 8. Caugfht "%s" Defaulting to 1.', limitResults));
limitResults = 1;
}
var minimistConfig = {
string: ['limit'],
default: {
limit: limitResults
},
alias: {
'limit': ['l'],
}
};
function handleSearch(IRCMessage) {
// Its important to note, the default minimist values are set via the config.
// Otherwise, changing limitResults would persist.
var sayArgs = parseArgs(IRCMessage.args, minimistConfig);
if (sayArgs.limit) {
var limit = parseInt(sayArgs.limit);
if (limit !== "NaN" && limit > 0 && limit <= maxUserDefinedLimit) {
limitResults = sayArgs.limit;
}
else {
return {
intent: "notice",
query: true,
message: format('%s is not valid. Please pass in 1-%s.', sayArgs.limit, maxUserDefinedLimit)
};
}
}
return google(sayArgs._.join(' '), limitResults).then(function(response) {
if (!response.responseData.results.length) {
return 'No results.';
}
var results = response.responseData.results.slice(0, limitResults);
return results.map(function(result) {
return format('%s - %s', result.titleNoFormatting, result.unescapedUrl);
});
}).catch(function(err) {
client._logger.error(googleRequestFailed);
client._logger.error(err);
return {
intent: 'notice',
query: true,
message: googleRequestFailed
};
});
};
function adminFail(err) {
return {
intent: 'notice',
query: true,
message: err
};
}
return {
handlers: {
"!google": handleSearch,
},
commands: ["google"],
help: {
"google": helps.google
}
};
}
};
module.exports = TennuGoogle;