diff --git a/Cargo.lock b/Cargo.lock index 7c7a444..3280c09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -272,6 +272,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "urlencoding", ] [[package]] @@ -2373,6 +2374,12 @@ dependencies = [ "serde", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "utf-8" version = "0.7.6" diff --git a/Cargo.toml b/Cargo.toml index 71a0542..02e2099 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ reqwest = { version = "0.12.12", features = ["json"] } serde = "1.0.217" serde_json = "1.0.134" tokio = { version = "1.42.0", features = ["full"] } +urlencoding = "2.1.3" diff --git a/config.json b/config.json index b84b5f4..78befb0 100644 --- a/config.json +++ b/config.json @@ -5,5 +5,6 @@ "log_channel_id": 0, "name_approvals_channel_id": 0, "monitor_address": "127.0.0.1:8003", - "ofapi_endpoint": "api.example.xyz" + "ofapi_endpoint": "api.example.xyz", + "search_query_string": "https://www.google.com/search?q={}" } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 29347c3..4b95b71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,7 @@ struct Config { name_approvals_channel_id: u64, monitor_address: String, ofapi_endpoint: String, + search_query_string: Option, } impl Config { fn validate(&self) -> Option<&str> { @@ -89,6 +90,7 @@ struct Globals { name_approvals_channel: Option, monitor_address: String, ofapi_endpoint: String, + search_query_string: Option, // state: Mutex, reconnect_notification: Notify, @@ -434,7 +436,7 @@ async fn namereqs(ctx: poise::Context<'_, (), Error>) -> Result<()> { let channel = ctx.channel_id(); for req in reqs { - if let Err(e) = util::send_name_request_message(channel, &req).await { + if let Err(e) = util::send_name_request_message(channel, &req, &globals.search_query_string).await { println!("Failed to send name request message: {}", e); } } @@ -539,6 +541,7 @@ async fn main() { }, monitor_address: config.monitor_address, ofapi_endpoint: config.ofapi_endpoint, + search_query_string: config.search_query_string, // state: Mutex::new(state), reconnect_notification: Notify::new(), diff --git a/src/monitor.rs b/src/monitor.rs index bcfb4fd..6f27717 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -63,7 +63,7 @@ async fn handle_name_request_event( return Ok(()); }; let name_request: NameRequest = name_request_event.into(); - util::send_name_request_message(channel, &name_request).await?; + util::send_name_request_message(channel, &name_request, &globals.search_query_string).await?; Ok(()) } diff --git a/src/util.rs b/src/util.rs index 2f0df20..89bcfcf 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,17 +1,19 @@ use poise::serenity_prelude::{ButtonStyle, ChannelId, CreateButton}; +use urlencoding::encode; use crate::{send_message_with_buttons, NameRequest, Result}; pub(crate) async fn send_name_request_message( channel: ChannelId, name_request: &NameRequest, + search_template: &Option, ) -> Result<()> { let messsage = format!( "Name request from Player {}: **{}**", name_request.player_uid, name_request.requested_name ); - let buttons = vec![ + let mut buttons = vec![ CreateButton::new("namereq_approve") .label("Approve") .style(ButtonStyle::Success), @@ -20,6 +22,12 @@ pub(crate) async fn send_name_request_message( .style(ButtonStyle::Danger), ]; + if let Some(query_string) = search_template.as_ref() { + let encoded_name = encode(&name_request.requested_name).into_owned(); + let search_url = query_string.replace("{}", &encoded_name); + buttons.push(CreateButton::new_link(&search_url).label("Search")) + } + send_message_with_buttons(channel, &messsage, buttons).await?; Ok(()) }