-
Notifications
You must be signed in to change notification settings - Fork 2
/
DieInteraction.ts
122 lines (106 loc) · 2.7 KB
/
DieInteraction.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
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
import Interaction from '../ChatServer/Interaction';
import DashBot from '../DashBot';
import { EventForEmitter } from '../Events';
import sleep from '../util/sleep';
/**
* Rolls dice and flips coins.
*
* `roll dice`
*
* `roll d20`
*
* `roll d100000`
*
* `flip coin`
*/
export default class DieInteraction implements Interaction {
register(bot: DashBot) {
bot.on('message', this.onMessage.bind(this));
}
async onMessage(event: EventForEmitter<DashBot, 'message'>) {
const message = event.data;
const { textContent, author, channel } = message;
const match = /^roll (d(-?\d+)|dice)$/i.exec(textContent);
if (match) {
event.cancel();
let size = 0;
if (match[1] === 'dice') {
size = 6;
} else {
size = Number.parseInt(match![2]);
}
let positive = true;
if (size === 0) {
channel.sendText(
"hey what kind of bot do you take me for, there's no such thing as a " +
match![1]
);
return;
}
if (size === 1) {
channel.sendText(
`something tells me the result of rolling a ${match[1]} would be awfully predictable...`
);
return;
}
// this.bot.stats.recordUserTriggeredEvent(
// author.username,
// `roll d${size}`
// );
if (size < 0) {
size = size * -1;
positive = false;
}
const result = Math.floor(Math.random() * size) + 1;
(async () => {
const resultString = positive
? result.toFixed(0)
: '-' + result.toFixed(0);
const message = `Rolling a D${(
size * (positive ? 1 : -1)
).toFixed(0)}... ${resultString}`;
channel.sendTyping();
await sleep(500 + 55 * message.length);
await channel.sendText(message);
})();
return;
}
if (/^roll (d(-?\d+)e\d+)$/i.test(textContent)) {
event.cancel();
channel.sendText('get out of here with those silly numbers nerd');
return;
}
if (/(coin (toss|flip)|(toss|flip) coin)/i.test(textContent)) {
event.cancel();
const size = 11;
const result = Math.floor(Math.random() * size) + 1;
// this.bot.stats.recordUserTriggeredEvent(
// author.username,
// `flip coin`
// );
channel
.sendText('@' + author.username + ', flipping...')
.then(() => sleep(1000))
.then(() => {
if (result === 11) {
channel.sendText('Oh no i dropped it :(');
// this.bot.stats.recordUserTriggeredEvent(
// author.username,
// `flip coin: coins dropped`
// );
return;
}
channel.sendText(
((result - 1) % 2) + 1 === 1 ? 'Heads!' : 'Tails!'
);
// this.bot.stats.recordUserTriggeredEvent(
// author.username,
// `flip coin: ${
// ((result - 1) % 2) + 1 === 1 ? 'Heads!' : 'Tails!'
// }`
// );
});
return;
}
}
}