copper/src/commands/system/suggest.rs

95 lines
2.7 KiB
Rust

use crate::Configuration;
use crate::User;
use crate::client::Client;
use crate::client::ClientEvent;
use crate::client::Player;
use crate::commands::Command;
use crate::commands::argument::{ArgumentSpec, ArgumentType, ParsedArgument, ParsedArguments};
use async_trait::async_trait;
pub struct SuggestCommand {
conf: Configuration,
}
impl SuggestCommand {
pub fn new(conf: Configuration) -> Self {
Self { conf }
}
}
#[async_trait]
impl Command for SuggestCommand {
fn name(&self) -> &'static str {
"suggest"
}
fn aliases(&self) -> &[&'static str] {
&["suggestion", "sg"]
}
fn category(&self) -> &'static str {
"system"
}
fn description(&self) -> &'static str {
"Suggest something to the bot owner."
}
fn argument_spec(&self) -> &'static [ArgumentSpec] {
&[ArgumentSpec {
name: "text",
arg_type: ArgumentType::GreedyString,
required: true,
default: None,
children: &[],
}]
}
async fn constructed(&mut self, _: Client) {}
async fn event(&mut self, _: Client, _: ClientEvent) {}
async fn execute(&mut self, client: Client, player: Player, args: ParsedArguments, _: User) {
let suggestion = match args.get("text") {
Some(ParsedArgument::GreedyString(s)) if !s.is_empty() => s,
_ => {
client.message("Please provide a suggestion.").await;
return;
}
};
let ntfy_url = self.conf.commands.ntfy.clone();
if ntfy_url.is_none() {
client.message("Suggestions are currently disabled.").await;
return;
}
let username = player.name.clone();
let player_id_short = player._id.chars().take(6).collect::<String>();
let message_body = format!("{} ({}): {}", username, player_id_short, suggestion);
let res = reqwest::Client::new()
.post(ntfy_url.unwrap())
.header("Priority", "1")
.header("Title", "New Copper suggestion")
.body(message_body)
.send()
.await;
match res {
Ok(resp) if resp.status().is_success() => {
client
.message("Your suggestion has been sent. Thank you!")
.await;
}
Ok(resp) => {
client
.message(format!("Failed to send suggestion: HTTP {}", resp.status()))
.await;
}
Err(e) => {
client
.message(format!("Failed to send suggestion: {}", e))
.await;
}
}
}
}