use std::fmt::Write; use async_trait::async_trait; use reqwest::Method; use serenity::model::interactions::application_command::{ ApplicationCommandInteraction, ApplicationCommandOptionType, }; use geoffrey_models::models::parameters::selling_params::{ItemSort, Order, SellingParams}; use geoffrey_models::models::response::selling_listing::SellingListing; use crate::bot::arg_parse::{option_to_order, option_to_sort, option_to_string}; use crate::bot::commands::{BotCommand, CommandError}; use serenity::builder::CreateApplicationCommand; pub struct SellingCommand; #[async_trait] impl BotCommand for SellingCommand { type ApiParams = SellingParams; type ApiResp = Vec; fn command_name() -> String { "selling".to_string() } fn request_type() -> Method { Method::GET } fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { command .name(Self::command_name()) .description("Find items for sale.") .create_option(|option| { option .name("query") .description("Item to find") .kind(ApplicationCommandOptionType::String) .required(true) }) .create_option(|option| { option .name("sort") .description("How to sort items") .kind(ApplicationCommandOptionType::String) .add_string_choice(ItemSort::Price, ItemSort::Price) .add_string_choice(ItemSort::Restock, ItemSort::Restock) .required(false) }) .create_option(|option| { option .name("order") .description("Order of the item Search") .kind(ApplicationCommandOptionType::String) .add_string_choice(Order::Low, Order::Low) .add_string_choice(Order::High, Order::High) .required(false) }) } async fn process_arguments( command_interaction: ApplicationCommandInteraction, ) -> Result { let options = command_interaction.data.options; let query = option_to_string(options.get(0), "query")?; let sort = option_to_sort(options.get(1), "sort").ok(); let order = option_to_order(options.get(2), "order").ok(); Ok(SellingParams::new(query, sort, order)) } fn build_response(resp: Self::ApiResp) -> String { if resp.is_empty() { "No shops were found selling that, maybe I should start selling it...".to_string() } else { let mut resp_str = String::new(); writeln!(resp_str, "The following items match:").unwrap(); for item in resp { writeln!( resp_str, "**{}**, {} for {}D: {} {}", item.listing.item.name, item.listing.count_per_price, item.listing.price, item.shop_name, item.shop_loc ) .unwrap(); } resp_str } } }