Add examplebot
This commit is contained in:
parent
cbb668c9da
commit
59456c295b
2 changed files with 96 additions and 0 deletions
|
|
@ -14,6 +14,12 @@ tokio = { version = "1.42.0", features = ["macros", "sync", "time"] }
|
|||
tokio-tungstenite = "0.24.0"
|
||||
unicode-normalization = "0.1.24"
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = "1.0.94"
|
||||
rustls = "0.23.19"
|
||||
tokio = { version = "1.42.0", features = ["full"] }
|
||||
tokio-tungstenite = { version = "0.24.0", features = ["rustls-tls-native-roots"] }
|
||||
|
||||
[lints]
|
||||
rust.unsafe_code = { level = "forbid", priority = 1 }
|
||||
# Lint groups
|
||||
|
|
|
|||
90
examples/examplebot.rs
Normal file
90
examples/examplebot.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use euphoxide::{
|
||||
api::{Data, Message, Nick, Send},
|
||||
client::conn::{ClientConn, ClientConnHandle},
|
||||
};
|
||||
|
||||
async fn set_nick(conn: &ClientConnHandle) -> anyhow::Result<()> {
|
||||
conn.send_only(Nick {
|
||||
name: "examplebot".to_string(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_pong(conn: &ClientConnHandle, msg: Message) -> anyhow::Result<()> {
|
||||
conn.send_only(Send {
|
||||
content: "Pong!".to_string(),
|
||||
parent: Some(msg.id),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_pyramid(conn: &ClientConnHandle, msg: Message) -> anyhow::Result<()> {
|
||||
let mut parent = msg.id;
|
||||
|
||||
for _ in 0..3 {
|
||||
let first = conn
|
||||
.send(Send {
|
||||
content: "brick".to_string(),
|
||||
parent: Some(parent),
|
||||
})
|
||||
.await?;
|
||||
|
||||
conn.send_only(Send {
|
||||
content: "brick".to_string(),
|
||||
parent: Some(parent),
|
||||
})
|
||||
.await?;
|
||||
|
||||
parent = first.await?.0.id;
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
|
||||
conn.send_only(Send {
|
||||
content: "brick".to_string(),
|
||||
parent: Some(parent),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn on_data(conn: ClientConnHandle, data: Data) {
|
||||
let result = match data {
|
||||
Data::SnapshotEvent(_) => set_nick(&conn).await,
|
||||
Data::SendEvent(event) if event.0.content == "!ping" => send_pong(&conn, event.0).await,
|
||||
Data::SendEvent(event) if event.0.content == "!pyramid" => {
|
||||
send_pyramid(&conn, event.0).await
|
||||
}
|
||||
_ => Ok(()),
|
||||
};
|
||||
|
||||
if let Err(err) = result {
|
||||
println!("Error while responding: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn run() -> anyhow::Result<()> {
|
||||
let (mut conn, _) = ClientConn::connect("test", None).await?;
|
||||
|
||||
while let Some(packet) = conn.recv().await? {
|
||||
let data = packet.into_data()?;
|
||||
tokio::task::spawn(on_data(conn.handle(), data));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
loop {
|
||||
if let Err(err) = run().await {
|
||||
println!("Error while running: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue