Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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={}"
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct Config {
name_approvals_channel_id: u64,
monitor_address: String,
ofapi_endpoint: String,
search_query_string: Option<String>,
}
impl Config {
fn validate(&self) -> Option<&str> {
Expand Down Expand Up @@ -89,6 +90,7 @@ struct Globals {
name_approvals_channel: Option<ChannelId>,
monitor_address: String,
ofapi_endpoint: String,
search_query_string: Option<String>,
//
state: Mutex<State>,
reconnect_notification: Notify,
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
10 changes: 9 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
) -> 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),
Expand All @@ -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(())
}
Loading