Geoffrey-rs/geoffrey_bot/src/bot/commands/selling.rs

96 lines
3.3 KiB
Rust

use std::fmt::Write;
use async_trait::async_trait;
use reqwest::Method;
use serenity::builder::CreateApplicationCommand;
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;
use crate::bot::formatters::display_item_listing;
use crate::context::GeoffreyContext;
use crate::error::BotError;
pub struct SellingCommand;
#[async_trait]
impl BotCommand for SellingCommand {
type ApiParams = SellingParams;
type ApiResp = Vec<SellingListing>;
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<Self::ApiParams, BotError> {
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(_: &GeoffreyContext, resp: Self::ApiResp, args: Self::ApiParams) -> 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 \"{}\":", args.query).unwrap();
for item in resp {
writeln!(
resp_str,
"{} @ {} {}",
display_item_listing(&item.listing),
item.shop_name,
item.shop_loc
)
.unwrap();
}
resp_str
}
}
}