From eadb12e999cfcf3782d21dc1fe28cdb7f27832ec Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 28 Dec 2024 00:23:04 +0100 Subject: [PATCH] Add example bot --- euphoxide-bot/Cargo.toml | 6 ++ euphoxide-bot/examples/examplebot.rs | 86 ++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 euphoxide-bot/examples/examplebot.rs diff --git a/euphoxide-bot/Cargo.toml b/euphoxide-bot/Cargo.toml index 58bcbcf..22f7c01 100644 --- a/euphoxide-bot/Cargo.toml +++ b/euphoxide-bot/Cargo.toml @@ -16,5 +16,11 @@ log = { workspace = true } tokio = { workspace = true, features = ["rt"] } tokio-tungstenite = { workspace = true } +[dev-dependencies] +anyhow = { workspace = true } +rustls = { workspace = true } +tokio = { workspace = true, features = ["full"] } +tokio-tungstenite = { workspace = true, features = ["rustls-tls-native-roots"] } + [lints] workspace = true diff --git a/euphoxide-bot/examples/examplebot.rs b/euphoxide-bot/examples/examplebot.rs new file mode 100644 index 0000000..5de40f0 --- /dev/null +++ b/euphoxide-bot/examples/examplebot.rs @@ -0,0 +1,86 @@ +use std::time::Duration; + +use async_trait::async_trait; +use euphoxide::api::Message; +use euphoxide_bot::{ + bot::Bot, + command::{ + bang::{General, Specific}, + basic::Described, + botrulez::{FullHelp, Ping, ShortHelp}, + Command, Commands, Context, Info, Propagate, + }, +}; +use tokio::sync::mpsc; + +struct Pyramid; + +#[async_trait] +impl Command for Pyramid { + fn info(&self, _ctx: &Context) -> Info { + Info::new().with_description("build a pyramid") + } + + async fn execute( + &self, + _arg: &str, + msg: &Message, + ctx: &Context, + _bot: &Bot, + ) -> euphoxide::Result { + let mut parent = msg.id; + + for _ in 0..3 { + let first = ctx.reply(parent, "brick").await?; + ctx.reply_only(parent, "brick").await?; + parent = first.await?.0.id; + tokio::time::sleep(Duration::from_secs(1)).await; + } + + ctx.reply_only(parent, "brick").await?; + Ok(Propagate::No) + } +} + +async fn run() -> anyhow::Result<()> { + let (event_tx, mut event_rx) = mpsc::channel(10); + + let commands = Commands::new() + .then(Described::hidden(General::new("ping", Ping::default()))) + .then(Described::hidden(Specific::new("ping", Ping::default()))) + .then(Described::hidden(General::new( + "help", + ShortHelp::new("/me demonstrates how to use euphoxide"), + ))) + .then(Described::hidden(Specific::new( + "help", + FullHelp::new().with_after("Created using euphoxide."), + ))) + .then(General::new("pyramid", Pyramid)); + + let bot: Bot = Bot::new_simple(commands, event_tx); + + bot.instances + .add_instance( + bot.server_config + .clone() + .instance("test") + .with_username("examplebot"), + ) + .await; + + while let Some(event) = event_rx.recv().await { + bot.handle_event(event); + } + + Ok(()) +} + +#[tokio::main] +async fn main() { + loop { + if let Err(err) = run().await { + println!("Error while running: {err}"); + } + } +}