This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
/
mustache.js
96 lines (78 loc) · 2.78 KB
/
mustache.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
module.exports = function (bot) {
'use strict';
var unexisto = 'User {0} was not found in room {1} (sorry, mustache only ' +
'works there).';
function mustachify (args) {
var props = parseArgs(args),
usrid = props.usrid;
bot.log(props, '/mustache input');
// check for url passing
if (linkCheck(usrid)) {
finish(encodeURIComponent(usrid));
return;
}
if (!usrid) {
usrid = args.get('user_id');
}
else if (/\D/.test(usrid)) {
usrid = args.findUserId(usrid);
}
bot.log(usrid, '/mustache mapped');
if (!bot.users.hasOwnProperty(usrid)) {
return unexisto.supplant(usrid, bot.adapter.roomid);
}
else if (Number(usrid) === bot.adapter.userid) {
return [
'Nobody puts a mustache on me. Again.',
'Mustache me once, shame on you. Mustache me ---twice--- 9 times...'
].random();
}
var hash = bot.users[usrid].email_hash;
// SO now allows non-gravatar images. the email_hash will be a link to
// the image in that case, prepended with a ! for some reason
if (hash[0] === '!') {
finish(encodeURIComponent(hash.slice(1)) + '#.png');
}
else {
finish('http%3A%2F%2Fwww.gravatar.com%2Favatar%2F{0}%3Fs%3D256%26d%3Didenticon#.png'.supplant(hash));
}
function finish (src) {
bot.log(src, '/mustache finish');
args.directreply('http://mustachify.me/' + props.mustache + '?src=' + src);
}
function parseArgs (args) {
var parts = args.parse(),
last = parts.pop(),
ret = {};
// /mustache usrid mustache
// /mustache user-name mustache
// we've already `.pop`ed the mustache part, so we need to account for it
if (parts.length > 0 && !(/\D/).test(last)) {
ret.usrid = parts.join(' ');
ret.mustache = last;
}
// /mustache usrid
else {
ret.usrid = args.content;
ret.mustache = Math.rand(0, 5);
}
return ret;
}
}
function linkCheck (suspect) {
return suspect.startsWith('http') || suspect.startsWith('www');
}
var cmd = {
name: 'mustache',
fun: mustachify,
privileges: {
del: 'NONE'
},
description: 'Mustachifies a user. ' +
'`/mustache [link|usrid|username] [mustache=rand(0,5)]`'
};
bot.addCommand(cmd);
// #176, alias moustache to mustache
var moustache = Object.merge(cmd, { name: 'moustache' });
bot.addCommand(moustache);
};