-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathRandomBot.scala
46 lines (41 loc) · 1.33 KB
/
RandomBot.scala
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
import cats.instances.future._
import cats.syntax.functor._
import com.bot4s.telegram.api.declarative.Commands
import com.bot4s.telegram.future.Polling
import com.bot4s.telegram.methods.SendDice
import scala.util.Try
import scala.concurrent.Future
/**
* Generates random values.
*/
class RandomBot(token: String) extends ExampleBot(token) with Polling with Commands[Future] {
val rng = new scala.util.Random(System.currentTimeMillis())
onCommand("coin" or "flip") { implicit msg =>
reply(if (rng.nextBoolean()) "Head!" else "Tail!").void
}
onCommand("real" | "double" | "float") { implicit msg =>
reply(rng.nextDouble().toString).void
}
onCommand("/die" | "roll") { implicit msg =>
reply("⚀⚁⚂⚃⚄⚅" (rng.nextInt(6)).toString).void
}
onCommand("random" or "rnd") { implicit msg =>
withArgs {
case Seq(Int(n)) if n > 0 =>
reply(rng.nextInt(n).toString).void
case _ => reply("Invalid argumentヽ(ಠ_ಠ)ノ").void
}
}
onCommand("choose" | "pick" | "select") { implicit msg =>
withArgs { args =>
replyMd(if (args.isEmpty) "No arguments provided." else args(rng.nextInt(args.size))).void
}
}
onCommand("auto") { implicit msg =>
request(SendDice(msg.chat.id)).void
}
// Extractor
object Int {
def unapply(s: String): Option[Int] = Try(s.toInt).toOption
}
}