2021-12-04 02:33:29 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2021-12-01 04:13:56 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use reqwest::Method;
|
|
|
|
use serenity::model::interactions::application_command::{
|
2021-12-13 02:30:38 +00:00
|
|
|
ApplicationCommandInteraction, ApplicationCommandOptionType,
|
2021-12-01 04:13:56 +00:00
|
|
|
};
|
2021-12-04 02:33:29 +00:00
|
|
|
|
|
|
|
use geoffrey_models::models::parameters::selling_params::{ItemSort, Order, SellingParams};
|
|
|
|
use geoffrey_models::models::response::selling_listing::SellingListing;
|
|
|
|
|
2021-12-04 03:42:57 +00:00
|
|
|
use crate::bot::arg_parse::{option_to_order, option_to_sort, option_to_string};
|
2021-12-04 02:33:29 +00:00
|
|
|
use crate::bot::commands::{BotCommand, CommandError};
|
2021-12-13 02:30:38 +00:00
|
|
|
use serenity::builder::CreateApplicationCommand;
|
2021-12-01 04:13:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:30:38 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-12-01 04:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn process_arguments(
|
|
|
|
command_interaction: ApplicationCommandInteraction,
|
|
|
|
) -> Result<Self::ApiParams, CommandError> {
|
|
|
|
let options = command_interaction.data.options;
|
2021-12-04 03:42:57 +00:00
|
|
|
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();
|
2021-12-01 04:13:56 +00:00
|
|
|
|
|
|
|
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();
|
2021-12-04 03:42:57 +00:00
|
|
|
writeln!(resp_str, "The following items match:").unwrap();
|
2021-12-01 04:13:56 +00:00
|
|
|
for item in resp {
|
|
|
|
writeln!(
|
|
|
|
resp_str,
|
2021-12-19 21:55:59 +00:00
|
|
|
"**{}**, {} for {}D: {} {}",
|
2021-12-01 04:13:56 +00:00
|
|
|
item.listing.item.name,
|
|
|
|
item.listing.count_per_price,
|
2021-12-19 21:55:59 +00:00
|
|
|
item.listing.price,
|
2021-12-01 04:13:56 +00:00
|
|
|
item.shop_name,
|
|
|
|
item.shop_loc
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
resp_str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|