hangitbot/src/main.rs

87 lines
2.3 KiB
Rust
Raw Normal View History

2023-12-24 10:48:40 +00:00
mod commands;
mod config;
mod db_controller;
mod messages;
use clap::Parser;
use commands::{CommandHandler, Commands};
use config::Args;
use teloxide::{
prelude::*,
requests::{Request, Requester},
utils::command::BotCommands,
};
use wd_log::{
log_debug_ln, log_error_ln, log_info_ln, log_panic, set_level, set_prefix, DEBUG, INFO,
};
#[tokio::main]
async fn main() {
let args = Args::parse();
set_prefix("hangitboot");
if args.debug {
set_level(DEBUG);
log_debug_ln!("{:?}", args);
} else {
set_level(INFO);
}
let command_handler = match CommandHandler::new(&args).await {
Err(err) => log_panic!("{}", err),
Ok(c) => c,
};
command_handler.init().await.unwrap();
let bot = Bot::new(args.tgbot_token.to_owned())
.set_api_url(reqwest::Url::parse(&args.api_url.as_str()).unwrap());
2023-12-26 04:44:12 +00:00
get_me(&bot).await;
2023-12-24 10:48:40 +00:00
register_commands(&bot).await;
Commands::repl(bot, move |bot: Bot, message: Message, cmd: Commands| {
let command_handler = command_handler.clone();
2024-01-13 04:43:07 +00:00
2023-12-24 10:48:40 +00:00
async move {
2024-01-13 04:43:07 +00:00
let r = match cmd {
Commands::Help => command_handler.help_handler(&bot, &message).await,
Commands::About => command_handler.about_handler(&bot, &message).await,
Commands::Top => command_handler.top_handler(&bot, &message).await,
Commands::HangIt => command_handler.hangit_handler(&bot, &message).await,
2023-12-24 10:48:40 +00:00
};
2024-01-13 04:43:07 +00:00
match r {
Ok(_r) => {
log_debug_ln!("will send: {:?}", _r.text());
Ok(())
},
Err(err) => {
log_error_ln!("{:?}", err);
Err(err)
}
}
2023-12-24 10:48:40 +00:00
}
})
.await;
}
async fn register_commands(bot: &Bot) {
if let Err(error) = bot.set_my_commands(Commands::bot_commands()).send().await {
log_error_ln!("{:?}", error);
} else {
log_info_ln!("commands registered")
}
}
2023-12-26 04:44:12 +00:00
async fn get_me(bot: &Bot) {
match bot.get_me().send().await {
Ok(result) => log_info_ln!(
"connect succeed: id={}, botname=\"{}\"",
result.id,
result.username()
),
Err(error) => log_panic!("{}", error),
}
}