Skip to content

Commit

Permalink
refactor(dispatcher): more idiomatic structure
Browse files Browse the repository at this point in the history
  • Loading branch information
HitaloM committed Sep 17, 2024
1 parent c4212c0 commit 8e0026e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 88 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 0 additions & 51 deletions src/commands.rs

This file was deleted.

22 changes: 17 additions & 5 deletions src/handlers/bans.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

use anyhow::Result;
use teloxide::{prelude::*, types::ChatPermissions};
use anyhow::{Error, Result};
use teloxide::{dispatching::UpdateHandler, prelude::*, types::ChatPermissions};

use crate::Bot;

pub async fn kick_cmd(bot: &Bot, message: &Message) -> Result<()> {
use super::BansCommand;

pub fn schema() -> UpdateHandler<Error> {
dptree::entry().branch(
Update::filter_message()
.filter_command::<BansCommand>()
.branch(dptree::case![BansCommand::Kick].endpoint(kick))
.branch(dptree::case![BansCommand::Ban].endpoint(ban))
.branch(dptree::case![BansCommand::Mute].endpoint(mute)),
)
}

pub async fn kick(bot: Bot, message: Message) -> Result<()> {
if let Some(replied) = message.reply_to_message() {
bot.unban_chat_member(message.chat.id, replied.from.as_ref().unwrap().id)
.await?;
Expand All @@ -20,7 +32,7 @@ pub async fn kick_cmd(bot: &Bot, message: &Message) -> Result<()> {
Ok(())
}

pub async fn ban_cmd(bot: &Bot, message: &Message) -> Result<()> {
pub async fn ban(bot: Bot, message: Message) -> Result<()> {
if let Some(replied) = message.reply_to_message() {
bot.ban_chat_member(message.chat.id, replied.from.as_ref().unwrap().id)
.await?;
Expand All @@ -34,7 +46,7 @@ pub async fn ban_cmd(bot: &Bot, message: &Message) -> Result<()> {
Ok(())
}

pub async fn mute_cmd(bot: &Bot, message: &Message) -> Result<()> {
pub async fn mute(bot: Bot, message: Message) -> Result<()> {
if let Some(replied) = message.reply_to_message() {
bot.restrict_chat_member(
message.chat.id,
Expand Down
28 changes: 28 additions & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

use teloxide::macros::BotCommands;

pub mod bans;
pub mod start;

#[derive(BotCommands, Clone)]
#[command(
rename_rule = "lowercase",
description = "Commands for <i>start</i> handler:"
)]
pub enum StartCommand {
#[command(description = "Initialize the bot.")]
Start,
#[command(description = "Display help information.")]
Help,
}

#[derive(BotCommands, Clone)]
#[command(
rename_rule = "lowercase",
description = "Commands for <i>bans</i> handler:"
)]
pub enum BansCommand {
#[command(description = "Kick a user from the group (reply to the user).")]
Kick,
#[command(description = "Ban a user from the group (reply to the user).")]
Ban,
#[command(description = "Mute a user in the group (reply to the user).")]
Mute,
}
24 changes: 16 additions & 8 deletions src/handlers/start.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

use anyhow::Result;
use teloxide::{prelude::*, utils::command::BotCommands};
use anyhow::{Error, Result};
use teloxide::{dispatching::UpdateHandler, prelude::*, utils::command::BotCommands};

use crate::{
commands::{BansCommand, StartCommand},
Bot,
};
use crate::Bot;

pub async fn start_cmd(bot: &Bot, message: &Message) -> Result<()> {
use super::{BansCommand, StartCommand};

pub fn schema() -> UpdateHandler<Error> {
dptree::entry().branch(
Update::filter_message()
.filter_command::<StartCommand>()
.branch(dptree::case![StartCommand::Start].endpoint(start))
.branch(dptree::case![StartCommand::Help].endpoint(help)),
)
}

pub async fn start(bot: Bot, message: Message) -> Result<()> {
bot.send_message(message.chat.id, "<b>Hi!</b>").await?;
Ok(())
}

pub async fn help_cmd(bot: &Bot, message: &Message) -> Result<()> {
pub async fn help(bot: Bot, message: Message) -> Result<()> {
let text = format!(
"{}\n\n{}",
StartCommand::descriptions(),
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use teloxide::adaptors::{CacheMe, DefaultParseMode, Throttle};

pub mod commands;
mod config;
pub mod handlers;

Expand Down
23 changes: 6 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use teloxide::{
adaptors::throttle::Limits, prelude::*, types::ParseMode, update_listeners::Polling,
};

use hitsuki::{
commands::{BansCommand, StartCommand},
Config,
};
use hitsuki::{handlers, Config};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -26,21 +23,13 @@ async fn main() -> Result<()> {
.parse_mode(ParseMode::Html)
.cache_me();

let handler = Update::filter_message()
.branch(
dptree::entry()
.filter_command::<StartCommand>()
.endpoint(StartCommand::handler),
)
.branch(
dptree::entry()
.filter_command::<BansCommand>()
.endpoint(BansCommand::handler),
);
let handler = dptree::entry()
.branch(handlers::start::schema())
.branch(handlers::bans::schema());

let error_handler =
LoggingErrorHandler::with_custom_text("An error has occurred in the dispatcher");
let update_listener = Polling::builder(bot.clone())
let listener = Polling::builder(bot.clone())
.timeout(std::time::Duration::from_secs(10))
.drop_pending_updates()
.build();
Expand All @@ -50,7 +39,7 @@ async fn main() -> Result<()> {
.default_handler(|_| async move {}) // Don't warn about unhandled updates
.enable_ctrlc_handler()
.build()
.dispatch_with_listener(update_listener, error_handler)
.dispatch_with_listener(listener, error_handler)
.await;

Ok(())
Expand Down

0 comments on commit 8e0026e

Please sign in to comment.