-
Notifications
You must be signed in to change notification settings - Fork 2
/
PollCommand.ts
94 lines (80 loc) · 2.14 KB
/
PollCommand.ts
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
import Message from '../ChatServer/Message';
import Command from '../Command';
import Tracery from '../tracery/Tracery';
import Emoji from '../util/emoji';
const grammar = {
errTooManyArgs: [
`Sorry <@!#author.id#> I can only do polls with up to #maxAnswers# choices at the moment`,
],
maxAnswers: () => PollCommand.answersEmoji.length,
helpText: [
[
'To start a poll use "!poll question" for a yes/no choice or "!poll question answer1 "answer 2"',
'Eg. `#example#`',
].join('\n'),
],
example: [
'!poll "What to play?" "Rocket League" Overwatch Factorio Minecraft',
'!poll "What should I eat for dinner?" Food',
'!poll "Is everyone having fun?"',
],
};
/**
*/
export default class PollCommand extends Command {
readonly name = 'poll';
readonly description =
'Allows you to create polls (much like other existing poll bots).\n' +
'`!poll "this is my question" yes no maybe "i don\'t know".`';
static readonly answersEmoji = [
Emoji.ZERO,
Emoji.ONE,
Emoji.TWO,
Emoji.THREE,
Emoji.FOUR,
Emoji.FIVE,
Emoji.SIX,
Emoji.SEVEN,
Emoji.EIGHT,
Emoji.NINE,
Emoji.TEN,
];
async run(message: Message, ...args: string[]) {
const channel = message.channel;
if (!channel.supportsReactions) {
await channel.sendText("This chat does't support polls");
return;
}
const tracery = new Tracery({ ...grammar, author: message.author });
if (args.length === 0) {
channel.sendText(tracery.generate('helpText'));
return;
}
if (args.length === 1) {
//Yes / no poll
const msg = await channel.sendText(args[0]);
if (msg) {
await msg.react(Emoji.THUMBS_UP);
await msg.react(Emoji.THUMBS_DOWN);
}
return;
}
const [question, ...answers] = args;
if (answers.length > PollCommand.answersEmoji.length) {
channel.sendText(tracery.generate('errTooManyArgs'));
return;
}
const pollMessage = `${question}${answers
.map(
(answer, index) =>
`\n${PollCommand.answersEmoji[index]}: ${answer}`
)
.join('')}`;
const msg = await channel.sendText(pollMessage);
if (msg) {
for (let i = 0; i < answers.length; i++) {
await msg.react(PollCommand.answersEmoji[i]);
}
}
}
}