Prepare rewrite

This commit is contained in:
Joscha 2024-12-05 11:30:32 +01:00
parent 095d2cea86
commit d22ccd767a
21 changed files with 6 additions and 2265 deletions

View file

@ -1,150 +0,0 @@
// TODO Add description
// TODO Clean up and unify test bots
use std::sync::Arc;
use async_trait::async_trait;
use clap::Parser;
use euphoxide::api::Message;
use euphoxide::bot::botrulez::{FullHelp, HasDescriptions, HasStartTime, Ping, ShortHelp, Uptime};
use euphoxide::bot::command::{Clap, ClapCommand, Context, General, Global, Hidden, Specific};
use euphoxide::bot::commands::Commands;
use euphoxide::bot::instance::{Event, ServerConfig};
use euphoxide::bot::instances::Instances;
use euphoxide::conn;
use jiff::Timestamp;
use log::error;
use tokio::sync::mpsc;
const HELP: &str = "I'm an example bot for https://github.com/Garmelon/euphoxide";
/// Kill this bot.
#[derive(Parser)]
struct KillArgs;
struct Kill;
#[async_trait]
impl ClapCommand<Bot, conn::Error> for Kill {
type Args = KillArgs;
async fn execute(
&self,
_args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &mut Bot,
) -> Result<bool, conn::Error> {
bot.stop = true;
ctx.reply(msg.id, "/me dies").await?;
Ok(true)
}
}
/// Do some testing.
#[derive(Parser)]
struct TestArgs {
/// How much testing to do.
#[arg(default_value_t = 1)]
amount: u64,
}
struct Test;
#[async_trait]
impl ClapCommand<Bot, conn::Error> for Test {
type Args = TestArgs;
async fn execute(
&self,
args: Self::Args,
msg: &Message,
ctx: &Context,
_bot: &mut Bot,
) -> Result<bool, conn::Error> {
let content = if args.amount == 1 {
format!("/me did {} test", args.amount)
} else {
format!("/me did {} tests", args.amount)
};
ctx.reply(msg.id, content).await?;
Ok(true)
}
}
struct Bot {
commands: Arc<Commands<Self, conn::Error>>,
start_time: Timestamp,
stop: bool,
}
impl HasDescriptions for Bot {
fn descriptions(&self, ctx: &Context) -> Vec<String> {
self.commands.descriptions(ctx)
}
}
impl HasStartTime for Bot {
fn start_time(&self) -> Timestamp {
self.start_time
}
}
#[tokio::main]
async fn main() {
// https://github.com/snapview/tokio-tungstenite/issues/353#issuecomment-2455247837
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
let (tx, mut rx) = mpsc::unbounded_channel();
let mut instances = Instances::new(ServerConfig::default());
let mut cmds = Commands::new();
cmds.add(Hidden(General::new("ping", Clap(Ping::default()))));
cmds.add(Specific::new("ping", Clap(Ping::default())));
cmds.add(Hidden(General::new("help", Clap(ShortHelp::new(HELP)))));
cmds.add(Specific::new("help", Clap(FullHelp::new(HELP, ""))));
cmds.add(Specific::new("uptime", Clap(Uptime)));
cmds.add(Specific::new("kill", Clap(Kill)));
cmds.add(Global::new("test", Clap(Test)));
let cmds = Arc::new(cmds);
let mut bot = Bot {
commands: cmds.clone(),
start_time: Timestamp::now(),
stop: false,
};
for room in ["test", "test2", "testing"] {
let tx_clone = tx.clone();
let instance = instances
.server_config()
.clone()
.room(room)
.username(Some("TestBot"))
.build(move |e| {
let _ = tx_clone.send(e);
});
instances.add(instance);
}
while let Some(event) = rx.recv().await {
instances.purge();
if instances.is_empty() {
break;
}
if let Event::Packet(config, packet, snapshot) = event {
let result = cmds
.handle_packet(&config, &packet, &snapshot, &mut bot)
.await;
if let Err(err) = result {
error!("{err}");
}
if bot.stop {
break;
}
}
}
}

View file

@ -1,146 +0,0 @@
//! Similar to the `testbot_manual` example, but using [`Instance`] to connect
//! to the room (and to reconnect).
use euphoxide::api::packet::ParsedPacket;
use euphoxide::api::{Data, Nick, Send};
use euphoxide::bot::botrulez;
use euphoxide::bot::instance::{ConnSnapshot, Event, ServerConfig};
use jiff::Timestamp;
use tokio::sync::mpsc;
const NICK: &str = "TestBot";
const HELP: &str = "I'm an example bot for https://github.com/Garmelon/euphoxide";
async fn on_packet(packet: ParsedPacket, snapshot: ConnSnapshot) -> Result<(), ()> {
let data = match packet.content {
Ok(data) => data,
Err(err) => {
println!("Error for {}: {err}", packet.r#type);
return Err(());
}
};
match data {
Data::HelloEvent(ev) => println!("Connected with id {}", ev.session.id),
Data::SnapshotEvent(ev) => {
for session in ev.listing {
println!("{:?} ({}) is already here", session.name, session.id);
}
// Here, a new task is spawned so the main event loop can
// continue running immediately instead of waiting for a reply
// from the server.
//
// We only need to do this because we want to log the result of
// the nick command. Otherwise, we could've just called
// tx.send() synchronously and ignored the returned Future.
let conn_tx_clone = snapshot.conn_tx.clone();
tokio::spawn(async move {
// Awaiting the future returned by the send command lets you
// (type-safely) access the server's reply.
let reply = conn_tx_clone
.send(Nick {
name: NICK.to_string(),
})
.await;
match reply {
Ok(reply) => println!("Set nick to {:?}", reply.to),
Err(err) => println!("Failed to set nick: {err}"),
};
});
}
Data::BounceEvent(_) => {
println!("Received bounce event, stopping");
return Err(());
}
Data::DisconnectEvent(_) => {
println!("Received disconnect event, stopping");
return Err(());
}
Data::JoinEvent(event) => println!("{:?} ({}) joined", event.0.name, event.0.id),
Data::PartEvent(event) => println!("{:?} ({}) left", event.0.name, event.0.id),
Data::NickEvent(event) => println!(
"{:?} ({}) is now known as {:?}",
event.from, event.id, event.to
),
Data::SendEvent(event) => {
println!("Message {} was just sent", event.0.id.0);
let content = event.0.content.trim();
let mut reply = None;
if content == "!ping" || content == format!("!ping @{NICK}") {
reply = Some("Pong!".to_string());
} else if content == format!("!help @{NICK}") {
reply = Some(HELP.to_string());
} else if content == format!("!uptime @{NICK}") {
if let Some(joined) = snapshot.state.joined() {
let delta = Timestamp::now() - joined.since;
reply = Some(format!(
"/me has been up for {}",
botrulez::format_duration(delta)
));
}
} else if content == "!test" {
reply = Some("Test successful!".to_string());
} else if content == format!("!kill @{NICK}") {
println!(
"I was killed by {:?} ({})",
event.0.sender.name, event.0.sender.id
);
// Awaiting the server reply in the main loop to ensure the
// message is sent before we exit the loop. Otherwise, there
// would be a race between sending the message and closing
// the connection as the send function can return before the
// message has actually been sent.
let _ = snapshot
.conn_tx
.send(Send {
content: "/me dies".to_string(),
parent: Some(event.0.id),
})
.await;
return Err(());
}
if let Some(reply) = reply {
// If you are not interested in the result, you can just
// throw away the future returned by the send function.
println!("Sending reply...");
snapshot.conn_tx.send_only(Send {
content: reply,
parent: Some(event.0.id),
});
println!("Reply sent!");
}
}
_ => {}
}
Ok(())
}
#[tokio::main]
async fn main() {
// https://github.com/snapview/tokio-tungstenite/issues/353#issuecomment-2455247837
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
let (tx, mut rx) = mpsc::unbounded_channel();
let _instance = ServerConfig::default()
.room("test")
.username(Some("TestBot"))
.build(move |e| {
let _ = tx.send(e);
});
while let Some(event) = rx.recv().await {
if let Event::Packet(_config, packet, snapshot) = event {
if on_packet(packet, snapshot).await.is_err() {
break;
}
}
}
}

View file

@ -1,159 +0,0 @@
//! Similar to the `testbot_manual` example, but using [`Instance`] to connect
//! to the room (and to reconnect).
use euphoxide::api::packet::ParsedPacket;
use euphoxide::api::{Data, Nick, Send};
use euphoxide::bot::botrulez;
use euphoxide::bot::instance::{ConnSnapshot, Event, ServerConfig};
use euphoxide::bot::instances::Instances;
use jiff::Timestamp;
use tokio::sync::mpsc;
const NICK: &str = "TestBot";
const HELP: &str = "I'm an example bot for https://github.com/Garmelon/euphoxide";
async fn on_packet(packet: ParsedPacket, snapshot: ConnSnapshot) -> Result<(), ()> {
let data = match packet.content {
Ok(data) => data,
Err(err) => {
println!("Error for {}: {err}", packet.r#type);
return Err(());
}
};
match data {
Data::HelloEvent(ev) => println!("Connected with id {}", ev.session.id),
Data::SnapshotEvent(ev) => {
for session in ev.listing {
println!("{:?} ({}) is already here", session.name, session.id);
}
// Here, a new task is spawned so the main event loop can
// continue running immediately instead of waiting for a reply
// from the server.
//
// We only need to do this because we want to log the result of
// the nick command. Otherwise, we could've just called
// tx.send() synchronously and ignored the returned Future.
let conn_tx_clone = snapshot.conn_tx.clone();
tokio::spawn(async move {
// Awaiting the future returned by the send command lets you
// (type-safely) access the server's reply.
let reply = conn_tx_clone
.send(Nick {
name: NICK.to_string(),
})
.await;
match reply {
Ok(reply) => println!("Set nick to {:?}", reply.to),
Err(err) => println!("Failed to set nick: {err}"),
};
});
}
Data::BounceEvent(_) => {
println!("Received bounce event, stopping");
return Err(());
}
Data::DisconnectEvent(_) => {
println!("Received disconnect event, stopping");
return Err(());
}
Data::JoinEvent(event) => println!("{:?} ({}) joined", event.0.name, event.0.id),
Data::PartEvent(event) => println!("{:?} ({}) left", event.0.name, event.0.id),
Data::NickEvent(event) => println!(
"{:?} ({}) is now known as {:?}",
event.from, event.id, event.to
),
Data::SendEvent(event) => {
println!("Message {} was just sent", event.0.id.0);
let content = event.0.content.trim();
let mut reply = None;
if content == "!ping" || content == format!("!ping @{NICK}") {
reply = Some("Pong!".to_string());
} else if content == format!("!help @{NICK}") {
reply = Some(HELP.to_string());
} else if content == format!("!uptime @{NICK}") {
if let Some(joined) = snapshot.state.joined() {
let delta = Timestamp::now() - joined.since;
reply = Some(format!(
"/me has been up for {}",
botrulez::format_duration(delta)
));
}
} else if content == "!test" {
reply = Some("Test successful!".to_string());
} else if content == format!("!kill @{NICK}") {
println!(
"I was killed by {:?} ({})",
event.0.sender.name, event.0.sender.id
);
// Awaiting the server reply in the main loop to ensure the
// message is sent before we exit the loop. Otherwise, there
// would be a race between sending the message and closing
// the connection as the send function can return before the
// message has actually been sent.
let _ = snapshot
.conn_tx
.send(Send {
content: "/me dies".to_string(),
parent: Some(event.0.id),
})
.await;
return Err(());
}
if let Some(reply) = reply {
// If you are not interested in the result, you can just
// throw away the future returned by the send function.
println!("Sending reply...");
snapshot.conn_tx.send_only(Send {
content: reply,
parent: Some(event.0.id),
});
println!("Reply sent!");
}
}
_ => {}
}
Ok(())
}
#[tokio::main]
async fn main() {
// https://github.com/snapview/tokio-tungstenite/issues/353#issuecomment-2455247837
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
let (tx, mut rx) = mpsc::unbounded_channel();
let mut instances = Instances::new(ServerConfig::default());
for room in ["test", "test2", "testing"] {
let tx_clone = tx.clone();
let instance = instances
.server_config()
.clone()
.room(room)
.username(Some("TestBot"))
.build(move |e| {
let _ = tx_clone.send(e);
});
instances.add(instance);
}
while let Some(event) = rx.recv().await {
instances.purge();
if instances.is_empty() {
break;
}
if let Event::Packet(_config, packet, snapshot) = event {
if on_packet(packet, snapshot).await.is_err() {
break;
}
}
}
}

View file

@ -1,142 +0,0 @@
//! A small bot that doesn't use the `bot` submodule. Meant to show how the main
//! parts of the API fit together.
use std::error::Error;
use std::time::Duration;
use euphoxide::api::packet::ParsedPacket;
use euphoxide::api::{Data, Nick, Send};
use euphoxide::bot::botrulez;
use euphoxide::conn::{Conn, ConnTx, State};
use jiff::Timestamp;
const TIMEOUT: Duration = Duration::from_secs(10);
const DOMAIN: &str = "euphoria.leet.nu";
const ROOM: &str = "test";
const NICK: &str = "TestBot";
const HELP: &str = "I'm an example bot for https://github.com/Garmelon/euphoxide";
async fn on_packet(packet: ParsedPacket, conn_tx: &ConnTx, state: &State) -> Result<(), ()> {
let data = match packet.content {
Ok(data) => data,
Err(err) => {
println!("Error for {}: {err}", packet.r#type);
return Err(());
}
};
match data {
Data::HelloEvent(event) => println!("Connected with id {}", event.session.id),
Data::SnapshotEvent(event) => {
for session in event.listing {
println!("{:?} ({}) is already here", session.name, session.id);
}
// Here, a new task is spawned so the main event loop can
// continue running immediately instead of waiting for a reply
// from the server.
//
// We only need to do this because we want to log the result of
// the nick command. Otherwise, we could've just called
// tx.send() synchronously and ignored the returned Future.
let conn_tx_clone = conn_tx.clone();
tokio::spawn(async move {
// Awaiting the future returned by the send command lets you
// (type-safely) access the server's reply.
let reply = conn_tx_clone
.send(Nick {
name: NICK.to_string(),
})
.await;
match reply {
Ok(reply) => println!("Set nick to {:?}", reply.to),
Err(err) => println!("Failed to set nick: {err}"),
};
});
}
Data::BounceEvent(_) => {
println!("Received bounce event, stopping");
return Err(());
}
Data::DisconnectEvent(_) => {
println!("Received disconnect event, stopping");
return Err(());
}
Data::JoinEvent(event) => println!("{:?} ({}) joined", event.0.name, event.0.id),
Data::PartEvent(event) => println!("{:?} ({}) left", event.0.name, event.0.id),
Data::NickEvent(event) => println!(
"{:?} ({}) is now known as {:?}",
event.from, event.id, event.to
),
Data::SendEvent(event) => {
println!("Message {} was just sent", event.0.id.0);
let content = event.0.content.trim();
let mut reply = None;
if content == "!ping" || content == format!("!ping @{NICK}") {
reply = Some("Pong!".to_string());
} else if content == format!("!help @{NICK}") {
reply = Some(HELP.to_string());
} else if content == format!("!uptime @{NICK}") {
if let Some(joined) = state.joined() {
let delta = Timestamp::now() - joined.since;
reply = Some(format!(
"/me has been up for {}",
botrulez::format_duration(delta)
));
}
} else if content == "!test" {
reply = Some("Test successful!".to_string());
} else if content == format!("!kill @{NICK}") {
println!(
"I was killed by {:?} ({})",
event.0.sender.name, event.0.sender.id
);
// Awaiting the server reply in the main loop to ensure the
// message is sent before we exit the loop. Otherwise, there
// would be a race between sending the message and closing
// the connection as the send function can return before the
// message has actually been sent.
let _ = conn_tx
.send(Send {
content: "/me dies".to_string(),
parent: Some(event.0.id),
})
.await;
return Err(());
}
if let Some(reply) = reply {
// If you are not interested in the result, you can just
// throw away the future returned by the send function.
println!("Sending reply...");
conn_tx.send_only(Send {
content: reply,
parent: Some(event.0.id),
});
println!("Reply sent!");
}
}
_ => {}
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// https://github.com/snapview/tokio-tungstenite/issues/353#issuecomment-2455247837
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
let (mut conn, _) = Conn::connect(DOMAIN, ROOM, false, None, TIMEOUT).await?;
while let Ok(packet) = conn.recv().await {
if on_packet(packet, conn.tx(), conn.state()).await.is_err() {
break;
}
}
Ok(())
}