Skip to content

Commit

Permalink
Make /promo command stateful
Browse files Browse the repository at this point in the history
To simplify its usage.
  • Loading branch information
Leonid Kozarin committed Jul 8, 2024
1 parent 7400557 commit 6000501
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ commands:
line: "➖ <b>%{line}</b>"
promo:
description: "Activate a promo code"
request: "Enter a promo code:"
success:
template: "You has activated this promo code successfully! %{ending}"
singular: "Your dick has grown by <b>%{growth}</b> cm in one chat."
Expand Down
1 change: 1 addition & 0 deletions locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ commands:
line: "➖ <b>%{line}</b>"
promo:
description: "Активировать промокод"
request: "Введи промокод:"
success:
template: "Промокод успешно активирован! %{ending}"
singular: "Твой писюн вырос на <b>%{growth}</b> см в одном чате."
Expand Down
48 changes: 43 additions & 5 deletions src/handlers/promo.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use once_cell::sync::Lazy;
use rust_i18n::t;
use teloxide::Bot;
use teloxide::dispatching::dialogue::InMemStorage;
use teloxide::macros::BotCommands;
use teloxide::payloads::AnswerInlineQuerySetters;
use teloxide::prelude::{InlineQuery, Requester};
use teloxide::prelude::{Dialogue, InlineQuery, Requester};
use teloxide::types::{Message, User};
use crate::handlers::{ensure_lang_code, HandlerResult, reply_html};
use crate::{metrics, repo};
Expand All @@ -20,16 +21,53 @@ static PROMO_CODE_FORMAT_REGEXP: Lazy<regex::Regex> = Lazy::new(||
#[command(rename_rule = "lowercase")]
pub enum PromoCommands {
#[command(description = "promo")]
Promo(String)
Promo(String),
}

pub async fn promo_cmd_handler(bot: Bot, msg: Message, cmd: PromoCommands,
#[derive(Clone, Default)]
pub enum PromoCommandState {
#[default]
Start,
Requested,
}

pub type PromoCodeDialogue = Dialogue<PromoCommandState, InMemStorage<PromoCommandState>>;

pub async fn promo_cmd_handler(bot: Bot, msg: Message, cmd: PromoCommands, dialogue: PromoCodeDialogue,
repos: repo::Repositories) -> HandlerResult {
metrics::CMD_PROMO.invoked_by_command.inc();
let PromoCommands::Promo(code) = cmd;
let user = msg.from().ok_or("no from user")?;
let answer = match cmd {
PromoCommands::Promo(code) if code.is_empty() => {
dialogue.update(PromoCommandState::Requested).await?;

let answer = promo_activation_impl(repos.promo, user, &code).await?;
let lang_code = ensure_lang_code(msg.from());
t!("commands.promo.request", locale = &lang_code)
}
PromoCommands::Promo(code) => {
dialogue.exit().await?;

promo_activation_impl(repos.promo, user, &code).await?
},
};
reply_html(bot, msg, answer).await?;
Ok(())
}

pub async fn promo_requested_handler(bot: Bot, msg: Message, dialogue: PromoCodeDialogue,
repos: repo::Repositories) -> HandlerResult {
let answer = match msg.text() {
Some(code) => {
dialogue.exit().await?;

let user = msg.from().ok_or("no from user")?;
promo_activation_impl(repos.promo, user, code).await?
},
None => {
let lang_code = ensure_lang_code(msg.from());
t!("commands.promo.request", locale = &lang_code)
}
};
reply_html(bot, msg, answer).await?;
Ok(())
}
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use axum::Router;
use futures::future::join_all;
use reqwest::Url;
use rust_i18n::i18n;
use teloxide::dispatching::dialogue::InMemStorage;
use teloxide::prelude::*;
use teloxide::dptree::deps;
use teloxide::update_listeners::webhooks::{axum_to_router, Options};
use crate::handlers::{checks, HelpCommands, LoanCommands, PrivacyCommands, StartCommands};
use crate::handlers::{checks, HelpCommands, LoanCommands, PrivacyCommands, PromoCommandState, StartCommands};
use crate::handlers::{DickCommands, DickOfDayCommands, ImportCommands, PromoCommands};
use crate::handlers::pvp::{BattleCommands, BattleCommandsNoArgs};
use crate::handlers::stats::StatsCommands;
Expand Down Expand Up @@ -47,7 +48,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.branch(Update::filter_message().filter_command::<StatsCommands>().endpoint(handlers::stats::cmd_handler))
.branch(Update::filter_message().filter_command::<LoanCommands>().filter(checks::is_group_chat).endpoint(handlers::loan::cmd_handler))
.branch(Update::filter_message().filter_command::<ImportCommands>().filter(checks::is_group_chat).endpoint(handlers::import_cmd_handler))
.branch(Update::filter_message().filter_command::<PromoCommands>().filter(checks::is_not_group_chat).endpoint(handlers::promo_cmd_handler))
.branch(Update::filter_message().filter_command::<PromoCommands>().filter(checks::is_not_group_chat).enter_dialogue::<Message, InMemStorage<PromoCommandState>, PromoCommandState>()
.branch(dptree::case![PromoCommandState::Start].endpoint(handlers::promo_cmd_handler)))
.branch(Update::filter_message().enter_dialogue::<Message, InMemStorage<PromoCommandState>, PromoCommandState>()
.branch(dptree::case![PromoCommandState::Requested].endpoint(handlers::promo_requested_handler)))
.branch(Update::filter_message().filter(checks::is_not_group_chat).endpoint(checks::handle_not_group_chat))
.branch(Update::filter_inline_query().filter(checks::inline::is_group_chat).filter(handlers::pvp::inline_filter).endpoint(handlers::pvp::inline_handler))
.branch(Update::filter_inline_query().filter(handlers::promo_inline_filter).endpoint(handlers::promo_inline_handler))
Expand Down Expand Up @@ -97,7 +101,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
incrementor,
app_config,
help_container,
battle_locker
battle_locker,
InMemStorage::<PromoCommandState>::new()
];

match webhook_url {
Expand Down

0 comments on commit 6000501

Please sign in to comment.