-
Notifications
You must be signed in to change notification settings - Fork 40
/
BotCommands.user.js
183 lines (171 loc) · 7.79 KB
/
BotCommands.user.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// ==UserScript==
// @name @Closey command auto complete
// @namespace https://github.com/SO-Close-Vote-Reviewers/UserScripts
// @version 0.4
// @description command completion for bot commands
// @author rene
// @match *://chat.stackoverflow.com/rooms/41570/so-close-vote-reviewers
// @grant none
// ==/UserScript==
/*global $:false, document:false, console:false */
function startAutoComplete(jquery) {
"use strict";
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function (searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
}
});
}
var $ = jquery,
inp = $('#input'), // where we type messages
parse = /(@closey\s+)([\w|\W]+)/i, // //parse botname and commands
cmds = [
// Public
'add', // [user id] to [group name] - Manually adds a user to the given permission group.
'alive', // A simple ping command to test if the bot is running.
'approve request [#]', // [#] - Approves a pending permission request.
'commands', // Shows most commands.
'commands full', // Shows all commands, broken down by category.
'help', // Prints info about this software.
'membership', // Shows a list of all permission groups, and the members in those permission groups.
'my membership', // Shows the permission groups you are a part of.
'reject request [#]', // [#] - Rejects a pending permission request.
'remove [user id] from [group name]', // [user id] from [group name] - Manually removes a user from the given permission group.
'request permission to [group name]', // [group name] - Submits a request for the user to be added to a given permission group.
'running commands', // Displays a list of all commands that the chat bot is currently running.
'status', // Tests if the chatbot is alive and shows simple info about it.
'view requests', // Shows all pending permission requests.
// Reviewer
'current tag', // Get the tag that has the most amount of manageable close queue items from the SEDE query.
'my audit stats', // Shows stats about your recorded audits.
'my stats', // Shows stats (count) of your reviews completed today.
'my stats details', // Shows a table of the review items you've completed today.
'my membership', // Shows the permission groups you are a part of.
'next [#] tags', // [#] tags - Displays the first X tags from the SEDE query to focus on.
'opt in', // Tells the bot to resume tracking your close vote reviewing.
'opt out', // Tells the bot to stop tracking your close vote reviewing.
'queue stats', // Shows the stats at the top of the /review/close/stats page.
'refresh tags', // Forces a refresh of the tags obtained from the SEDE query.
'room stats', // Shows stats summary about how effective the room is at processing close vote review items.
'room stats details', // Shows stats about how effective the room is at processing close vote review items.
// BotOwner
// 'add review', // [review id] [user id] - Manually adds a review to a user. Should only be used for testing.
'ping reviewers <message>', // <message> - The bot will send a message with an @reply to all users that have done reviews recently.
'start event', // Shows the current stats from the /review/close/stats page and the next 3 tags to work on.
'stop bot' // - The bot will leave the chat room and quit the running application.
]; // all known commands
// clear all hints and remove click handlers
function clearHints() {
$('#closey').find('li').each(function () { $(this).off('click'); });
$('#closey').remove();
}
// put the choose hint in the chat message text area
function complete(bot, command) {
return function () {
inp.val(bot + command);
clearHints();
};
}
// build on single le that holds the hint
function buildHint(value, bot) {
var li = $('<li></li>')
.css('display', 'inline-block')
.css('margin-left', '3px')
.css('margin-right', '3px')
.css('padding', '2px')
.css('border', 'solid 1px blue')
.text(value);
li.on('click', complete(bot, value));
return li;
}
function highlight(li) {
li.addClass('tab');
li.css('background-color', 'yellow');
return li.text();
}
function highlightNextHint() {
var setnext = false,
lif,
selected;
$('#closey').find('li').each(function () {
var li = $(this);
if (li.hasClass('tab')) {
setnext = true;
li.removeClass('tab');
li.css('background-color', 'white');
} else {
if (setnext) {
selected = highlight(li);
return false;
}
}
});
if (!setnext) {
lif = $('#closey').find('li');
if (lif.length > 0) {
selected = highlight($(lif[0]));
setnext = true;
}
}
return selected;
}
function handleKey(cmd, bot) {
var botcmd,
c,
container;
clearHints();
container = $('<ul id="closey"></ul>').css('text-align', 'left');
for (c = 0; c < cmds.length; c = c + 1) {
botcmd = cmds[c];
if (botcmd.startsWith(cmd) && botcmd !== cmd) {
container.append(buildHint(botcmd, bot));
}
}
$('#tabcomplete-container').append(container);
}
$(document).on('keydown', function (k) {
var BOT = 1,
COMMAND = 2,
result = parse.exec(inp.val()),
selected;
if (result !== null &&
result.length > COMMAND &&
k.keyCode === 9) {
selected = highlightNextHint();
if (selected !== undefined) {
k.preventDefault();
k.stopPropagation();
inp.val(result[BOT] + selected);
return true;
}
}
});
inp.on('keyup', function (e) {
var BOT = 1,
COMMAND = 2,
result = parse.exec(e.result);
console.log(e);
console.log(result);
if (e.keyCode !== 9) {
if (result !== null &&
result.length > COMMAND) {
handleKey(result[COMMAND], result[BOT]);
} else {
clearHints();
}
}
});
}
function getJquery() {
"use strict";
return $ || unsafeWindow.jQuery;
}
window.addEventListener('load',
function() {
startAutoComplete(getJquery());
});