Skip to content

Commit

Permalink
feat: adapt tbot example to filtered subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
y5c4l3 committed Oct 18, 2023
1 parent 040e8aa commit f8035b3
Showing 1 changed file with 64 additions and 63 deletions.
127 changes: 64 additions & 63 deletions examples/tbot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use closur::bancho::{bot, multiplayer, Channel, Client, ClientOptions, Event};
use closur::bancho::{bot, multiplayer, Channel, Chat, Client, ClientOptions, Event};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

Expand All @@ -19,78 +19,79 @@ async fn main() -> Result<()> {
println!("[TBOT] Connected and authenticated, waiting for events...");
let title = "TBOT LOBBY".to_string();
let password = "tbot".to_string();
let response = writer.send_private_bot_command(bot::Command::Make(title.clone())).await?;
let response = writer
.send_private_bot_command(bot::Command::Make(title.clone()))
.await?;
let response = response.make();

// Channel is automatically joined after `!mp make`
let lobby = Channel::Multiplayer(response.id());
println!("[TBOT] Made and joined lobby \"{}\" ({})", title, lobby);
let mut writer = writer.into_channel_writer(lobby.clone());

writer.send_bot_command(bot::Command::Password(password.clone())).await?;
let mut lobby = client.join_match(response.id()).await?;
lobby
.send_bot_command(bot::Command::Password(password.clone()))
.await?;
println!("[TBOT] Set lobby password to \"{}\"", password);

let mut subscriber = client.events();
// This event subscriber will only receive events from the lobby.
let mut subscriber = lobby.events();
loop {
let event = subscriber.recv().await?;
if event.channel() == Some(&lobby) {
match event {
Event::Message { from, body, .. } => {
println!("[LOBBY] {}: {}", from.to_string(), body);
if body.starts_with("!info") {
writer.send_chat("TBOT from closur examples").await?;
} else if body.starts_with("!echo") {
let message = body.trim_start_matches("!echo").trim();
writer.send_chat(message).await?;
} else if body.starts_with("!start") {
writer.send_bot_command(bot::Command::Start(None)).await?;
} else if body.starts_with("!test") {
// Fetch settings for current lobby
let response = writer.send_bot_command(bot::Command::Settings).await?;
let settings = response.settings();
match event {
Event::Message { from, body, .. } => {
println!("[LOBBY] {}: {}", from.to_string(), body);
if body.starts_with("!info") {
lobby.send_chat("TBOT from closur examples").await?;
} else if body.starts_with("!echo") {
let message = body.trim_start_matches("!echo").trim();
lobby.send_chat(message).await?;
} else if body.starts_with("!start") {
lobby.send_bot_command(bot::Command::Start(None)).await?;
} else if body.starts_with("!invite") {
let message = Chat::new("Join my multiplayer game: ")
.append_link(title.as_str(), lobby.invite_link(None).as_str())
.to_string();
lobby.send_chat(message.as_str()).await?;
} else if body.starts_with("!test") {
// Fetch settings for current lobby
let response = lobby.send_bot_command(bot::Command::Settings).await?;
let settings = response.settings();

let slots = settings.valid_slots();
let with_highlight = slots
.iter()
.map(|x| x.user().name())
.collect::<Vec<_>>()
.join(", ");
let without_highlight = slots
.iter()
.map(|x| x.user().name_without_highlight())
.collect::<Vec<_>>()
.join(", ");
let slots = settings.valid_slots();
let with_highlight = slots
.iter()
.map(|x| x.user().name())
.collect::<Vec<_>>()
.join(", ");
let without_highlight = slots
.iter()
.map(|x| x.user().name_without_highlight())
.collect::<Vec<_>>()
.join(", ");

writer
.send_chat(
format!("Players (highlighted): {}", with_highlight).as_str(),
)
.await?;
writer
.send_chat(
format!("Players (without highlight): {}", without_highlight)
.as_str(),
)
.await?;
}
lobby
.send_chat(format!("Players (highlighted): {}", with_highlight).as_str())
.await?;
lobby
.send_chat(
format!("Players (without highlight): {}", without_highlight).as_str(),
)
.await?;
}
}
Event::Multiplayer(_, event) => match event {
multiplayer::Event::PlayerJoined { user, .. } => {
lobby
.send_chat(
format!("Hey, {}! This is TBOT lobby, have fun here.", user.name())
.as_str(),
)
.await?;
}
Event::Multiplayer(_, event) => match event {
multiplayer::Event::PlayerJoined { user, .. } => {
writer
.send_chat(
format!("Hey, {}! This is TBOT lobby, have fun here.", user.name())
.as_str(),
)
.await?;
}
_ => {}
},
Event::Part(_) => {
println!("See you next time.");
break;
},
_ => {},
_ => {}
},
Event::Part(_) => {
println!("See you next time.");
break;
}
_ => {}
}
}
Ok(())
Expand Down

0 comments on commit f8035b3

Please sign in to comment.