-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.rs
69 lines (57 loc) · 1.96 KB
/
game.rs
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
use tbot::{errors, prelude::*, state::Chats, types::message::From, Bot};
use tokio::sync::Mutex;
const GAME: &str = "";
const GOOD_PHRASE: &str = "tbot good";
const BAD_PHRASE: &str = "tbot bad";
const GOOD_MULTIPLIER: i32 = 10;
const BAD_MULTIPLIER: i32 = 100;
const SCORE_NOT_MODIFIED: &str = "Bad Request: BOT_SCORE_NOT_MODIFIED";
#[tokio::main]
async fn main() {
let mut bot = Bot::from_env("BOT_TOKEN")
.stateful_event_loop(Mutex::new(Chats::new()));
bot.command("game", |context, chats| async move {
let call_result = context.send_game(GAME).call().await;
let message = match call_result {
Ok(message) => message,
Err(err) => {
dbg!(err);
return;
}
};
chats.lock().await.insert(&*context, message.id);
});
bot.text(|context, chats| async move {
let message_id = {
let chats = chats.lock().await;
match chats.get(&*context) {
Some(id) => *id,
None => return,
}
};
let user = match &context.from {
Some(From::User(user)) => user,
_ => return,
};
let text = context.text.value.to_lowercase();
let good_score = text.matches(GOOD_PHRASE).count() as i32;
let bad_score = text.matches(BAD_PHRASE).count() as i32;
let score = GOOD_MULTIPLIER * good_score - BAD_MULTIPLIER * bad_score;
let score = score.max(1) as u32;
let call_result = context
.set_message_game_score(message_id, user.id, score)
.is_forced(true)
.call()
.await;
match call_result {
Ok(_) => (),
Err(errors::MethodCall::RequestError {
ref description, ..
}) if description == SCORE_NOT_MODIFIED => (),
Err(err) => {
dbg!(err);
}
}
});
bot.polling().start().await.unwrap();
}