feat(commands): add "curse" command

This commit is contained in:
Kristofers Solo 2025-10-27 11:41:45 +02:00
parent 2347c1b440
commit 89dec3387b
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8
3 changed files with 36 additions and 2 deletions

30
src/commands.rs Normal file
View File

@ -0,0 +1,30 @@
use teloxide::{prelude::*, utils::command::BotCommands};
use crate::comments::{Comments, global_comments};
#[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase")]
pub enum Command {
/// Display this text.
#[command(aliases = ["h", "?"])]
Help,
/// Send a random comment
#[command()]
Curse,
}
pub async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
match cmd {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await?
}
Command::Curse => {
let comment = global_comments().map(Comments::build_caption);
bot.send_message(msg.chat.id, comment.unwrap_or_else(|| "To comment".into()))
.await?
}
};
Ok(())
}

View File

@ -1,3 +1,4 @@
pub mod commands;
pub mod comments; pub mod comments;
pub mod download; pub mod download;
pub mod error; pub mod error;

View File

@ -1,7 +1,8 @@
use dotenv::dotenv; use dotenv::dotenv;
use std::sync::Arc; use std::sync::Arc;
use teloxide::{Bot, prelude::Requester, respond, types::Message}; use teloxide::{Bot, prelude::Requester, repls::CommandReplExt, respond, types::Message};
use tg_relay_rs::{ use tg_relay_rs::{
commands::{Command, answer},
comments::{Comments, init_global_comments}, comments::{Comments, init_global_comments},
handlers::SocialHandler, handlers::SocialHandler,
telemetry::setup_logger, telemetry::setup_logger,
@ -38,6 +39,8 @@ async fn main() -> color_eyre::Result<()> {
Arc::new(tg_relay_rs::handlers::TwitterHandler), Arc::new(tg_relay_rs::handlers::TwitterHandler),
]; ];
Command::repl(bot.clone(), answer).await;
teloxide::repl(bot.clone(), move |bot: Bot, msg: Message| { teloxide::repl(bot.clone(), move |bot: Bot, msg: Message| {
// clone the handlers vector into the closure // clone the handlers vector into the closure
let handlers = handlers.clone(); let handlers = handlers.clone();
@ -58,7 +61,7 @@ async fn main() -> color_eyre::Result<()> {
.await; .await;
} }
}); });
// if one handler matcher, stop checking // if one handler matched, stop checking
break; break;
} }
} }