Skip to content

Commit

Permalink
✨ Add print-help command
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Feb 22, 2024
1 parent c1df973 commit a79579d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/bot/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{Parser, Subcommand};
use traq_bot_http::payloads::{types::Message, DirectMessageCreatedPayload, MessageCreatedPayload};

pub mod help;
pub mod sudo;
pub mod webhook;

Expand All @@ -22,6 +23,8 @@ pub enum Commands {
#[command(subcommand)]
sudo: sudo::Sudo,
},
#[command(about = "cat help.md")]
PrintHelp,
}

impl<'a> Incomplete<&'a MessageCreatedPayload> for Commands {
Expand All @@ -31,6 +34,9 @@ impl<'a> Incomplete<&'a MessageCreatedPayload> for Commands {
match self {
Self::Webhook { wh } => CompletedCmds::Webhook(wh.complete(context)),
Self::Sudo { sudo } => CompletedCmds::Sudo(sudo.complete(context)),
Self::PrintHelp => CompletedCmds::PrintHelp(help::CompleteHelp::Channel(
context.message.channel_id.into(),
)),
}
}
}
Expand All @@ -42,6 +48,9 @@ impl<'a> Incomplete<&'a DirectMessageCreatedPayload> for Commands {
match self {
Self::Webhook { wh } => CompletedCmds::Webhook(wh.complete(context)),
Self::Sudo { sudo } => CompletedCmds::Sudo(sudo.complete(context)),
Self::PrintHelp => {
CompletedCmds::PrintHelp(help::CompleteHelp::Dm(context.message.user.id.into()))
}
}
}
}
Expand All @@ -50,6 +59,7 @@ impl<'a> Incomplete<&'a DirectMessageCreatedPayload> for Commands {
pub enum CompletedCmds {
Webhook(webhook::Complete),
Sudo(sudo::SudoCompleted),
PrintHelp(help::CompleteHelp),
}

impl Completed for CompletedCmds {
Expand All @@ -63,6 +73,7 @@ impl Completed for CompletedCmds {
Self::Sudo(sudo) => Commands::Sudo {
sudo: sudo.incomplete(),
},
Self::PrintHelp(_) => Commands::PrintHelp,
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/bot/src/cli/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use domain::{ChannelId, UserId};

#[derive(Debug, Clone, Copy)]
pub enum CompleteHelp {
Channel(ChannelId),
Dm(UserId),
}
2 changes: 2 additions & 0 deletions app/bot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub(crate) mod cli;
mod messages;
mod system;

static HELP_TEMPLATE: &str = include_str!("help.md");

#[derive(Debug, Clone)]
pub struct BotImpl {
pub name: String,
Expand Down
2 changes: 2 additions & 0 deletions app/bot/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use domain::{Error, Infra, MessageId, Result, StampId, TraqClient};
use crate::cli::{Cli, CompletedCmds, Incomplete};
use crate::BotImpl;

mod cmd_help;
mod cmd_sudo;
mod cmd_webhook;

Expand Down Expand Up @@ -35,6 +36,7 @@ impl BotImpl {
let res = match cmd {
Webhook(w) => self.handle_webhook_command(infra, w).await,
Sudo(s) => self.handle_sudo_command(infra, s).await,
PrintHelp(h) => self.handle_help_command(infra, h).await,
};
match res {
Ok(_) => {
Expand Down
26 changes: 26 additions & 0 deletions app/bot/src/messages/cmd_help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use domain::{Error, Infra, Result, TraqClient};

use super::BotImpl;
use crate::cli::help::CompleteHelp;

impl BotImpl {
pub(super) async fn handle_help_command<I>(&self, infra: &I, help: CompleteHelp) -> Result<()>
where
I: Infra,
Error: From<I::Error>,
{
let message = crate::HELP_TEMPLATE.replace("BOT_cnvtr", &self.name);
let client = infra.traq_client();
match help {
CompleteHelp::Channel(channel_id) => {
client.send_message(&channel_id, &message, false).await?;
}
CompleteHelp::Dm(user_id) => {
client
.send_direct_message(&user_id, &message, false)
.await?;
}
}
Ok(())
}
}

0 comments on commit a79579d

Please sign in to comment.