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

103 lines
3.5 KiB
Rust

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::GeoffreyFormatter;
use crate::context::GeoffreyContext;
use crate::error::BotError;
use serenity::utils::MessageBuilder;
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(ctx: &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 formatter = GeoffreyFormatter::new(ctx.settings.clone());
formatter.push(
&MessageBuilder::new()
.push("The following items match '")
.push_bold_safe(args.query)
.push_line("':")
.build(),
);
for item in resp {
formatter
.push_item_listing(&item.listing)
.push(" @ ")
.push(&item.shop_name)
.push(" ")
.push(&item.shop_loc.to_string())
.push_new_line();
}
formatter.build()
}
}
}