-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
140 lines (119 loc) · 5.49 KB
/
plugin.js
File metadata and controls
140 lines (119 loc) · 5.49 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
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
const format = require('util').format;
const Poll = require('./poll');
const helps = {
"open": [
"{{!}}poll-open option-a option-b ... option-z",
"",
"Starts a poll in the channel.",
"Aliases: {{!}}poll {{!}}pollopen {{!}}open-poll {{!}}openpoll {{!}}poll-start {{!}}pollstart {{!}}start-poll {{!}}startpoll"
],
"close": [
"{{!}}poll-close",
"",
"Closes the poll in the channel, displaying the results of the poll.",
"Aliases: {{!}}pollclose {{!}}close-poll {{!}}closepoll {{!}}poll-end {{!}}pollend {{!}}end-poll {{!}}endpoll"
],
"vote": [
"{{!}}vote option",
"",
"Vote for an option in the current channel poll.",
"Aliases: {{!}}poll-vote"
]
};
module.exports = {
init: function (client, imports) {
// Dict<ChannelName, Poll>
const openPolls = Object.create(null);
const requiresPermission = imports.admin.requiresPermission || imports.admin.requiresAdmin;
const requiresPoller = function (handler) {
return requiresPermission(handler, "poller");
};
return {
handlers: {
"!poll !poll-open !pollopen !open-poll !openpoll !poll-start !pollstart !start-poll !startpoll": requiresPoller(function (command) {
if (command.isQuery) {
return "Sorry, polls can only be started in channels.";
}
if (openPolls[command.channel]) {
return "Cannot start a new poll while another poll is already active.";
}
if (command.args.length < 2) {
return "A poll must have at least two options.";
}
openPolls[command.channel] = Poll(command.args);
return "Poll started.";
}),
"!poll-close !pollclose !close-poll !closepoll !poll-end !pollend !end-poll !endpoll": requiresPoller(function (command) {
if (!openPolls[command.channel]) {
return "There is no open poll in here to close.";
}
const results = openPolls[command.channel].close();
delete openPolls[command.channel];
return [
"Poll is now closed! Results:",
Object.keys(results)
.map(function (key) { return [key, results[key]]; })
.sort(function (lhs, rhs) { return lhs[1] <= rhs[1]; })
.map(function (result) { return format("%s: %s", result[0], result[1]); })
.join(" | ")
];
}),
"!poll-vote !vote": function (command) {
if (!openPolls[command.channel]) {
return "There is no open poll to vote for.";
}
if (command.args.length === 0) {
return "You must specify an option.";
}
return openPolls[command.channel]
.vote(command.args[0], command.nickname)
.map(function () {
return format("%s voted for %s.", command.nickname, command.args[0]);
})
.unwrapOrElse(function (failureReason) {
switch (failureReason) {
case "no-option": return format("%s: %s is not an option!", command.nickname, command.args[0]);
case "already-voted": return format("%s: You've already voted for this poll.", command.nickname);
default:
client.error("PluginPoll", format("Unhandled failure reason in !poll-vote: %s", failureReason));
return format("Error: Unhandled failure reason in text replacement ('%s').", failureReason);
}
});
},
"privmsg": function (message) {
if (!openPolls[message.channel]) { return; }
const words = message.message.split(" ");
if (words.length !== 1) { return; }
const option = words[0];
const result = openPolls[message.channel].vote(option, message.nickname);
if (result.isOk()) {
return format("%s voted for %s.", message.nickname, option);
}
}
},
"commands": ["poll-open", "poll-close", "vote"],
"help": {
"poll": helps.open,
"poll-open": helps.open,
"pollopen": helps.open,
"open-poll": helps.open,
"openpoll": helps.open,
"poll-start": helps.open,
"pollstart": helps.open,
"start-poll": helps.open,
"startpoll": helps.open,
"poll-close": helps.close,
"pollclose": helps.close,
"close-poll": helps.close,
"closepoll": helps.close,
"poll-end": helps.close,
"pollend": helps.close,
"end-poll": helps.close,
"endpoll": helps.close,
"poll-vote": helps.vote,
"vote": helps.vote
}
};
},
requiresRoles: ["admin"]
};