Add more information to mc item command
Signed-off-by: trivernis <trivernis@protonmail.com>pull/22/head
parent
6f4d9df4b7
commit
8209c31469
@ -0,0 +1,53 @@
|
|||||||
|
use crate::providers::minecraft::ItemFullInformation;
|
||||||
|
use crate::utils::error::BotResult;
|
||||||
|
use serenity::client::Context;
|
||||||
|
use serenity::model::channel::Message;
|
||||||
|
use serenity::model::prelude::ChannelId;
|
||||||
|
|
||||||
|
pub async fn create_item_message(
|
||||||
|
ctx: &Context,
|
||||||
|
channel_id: ChannelId,
|
||||||
|
item: ItemFullInformation,
|
||||||
|
) -> BotResult<Message> {
|
||||||
|
let message = channel_id
|
||||||
|
.send_message(ctx, |m| {
|
||||||
|
m.embed(|mut e| {
|
||||||
|
e = e
|
||||||
|
.title(&item.name)
|
||||||
|
.thumbnail(format!(
|
||||||
|
"https://minecraftitemids.com/item/128/{}.png",
|
||||||
|
item.id
|
||||||
|
))
|
||||||
|
.field("Name", &item.name, false)
|
||||||
|
.field("Stack Size", item.stack_size, false);
|
||||||
|
|
||||||
|
if let Some(durability) = item.durability {
|
||||||
|
e.field("Durability", durability, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(food) = &item.food {
|
||||||
|
e.field("Saturation", food.saturation, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(block) = &item.block {
|
||||||
|
e.field("Hardness", block.hardness.unwrap_or(0f32), true)
|
||||||
|
.field(
|
||||||
|
"Blast Resistance",
|
||||||
|
block.blast_resistance.unwrap_or(0f32),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.field("Transparent", block.transparent, true)
|
||||||
|
.field("Emission Level", block.emit_light, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !item.enchantments.is_empty() {
|
||||||
|
e.field("Enchantments", item.enchantments.join(", "), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
e
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(message)
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
pub mod item;
|
@ -0,0 +1,47 @@
|
|||||||
|
use crate::utils::error::{BotError, BotResult};
|
||||||
|
use minecraft_data_rs::api::Api;
|
||||||
|
use minecraft_data_rs::models::block::Block;
|
||||||
|
use minecraft_data_rs::models::food::Food;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct ItemFullInformation {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
pub enchantments: Vec<String>,
|
||||||
|
pub durability: Option<u32>,
|
||||||
|
pub stack_size: u8,
|
||||||
|
pub food: Option<Food>,
|
||||||
|
pub block: Option<Block>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_item_full_information(name: &str, api: &Api) -> BotResult<ItemFullInformation> {
|
||||||
|
let items_by_name = api.items.items_by_name()?;
|
||||||
|
let item = items_by_name.get(name).ok_or(BotError::Msg(format!(
|
||||||
|
"The item `{}` could not be found",
|
||||||
|
name
|
||||||
|
)))?;
|
||||||
|
let enchantments_by_category = api.enchantments.enchantments_by_category()?;
|
||||||
|
let mut enchantments = Vec::new();
|
||||||
|
|
||||||
|
if let Some(enchant_categories) = &item.enchant_categories {
|
||||||
|
enchantments = enchant_categories
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|c| enchantments_by_category.get(c))
|
||||||
|
.flatten()
|
||||||
|
.map(|e| e.display_name.clone())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
}
|
||||||
|
let food_by_name = api.foods.foods_by_name()?;
|
||||||
|
let blocks_by_name = api.blocks.blocks_by_name()?;
|
||||||
|
|
||||||
|
log::trace!("Item is {:?}", item);
|
||||||
|
Ok(ItemFullInformation {
|
||||||
|
id: item.name.clone(),
|
||||||
|
name: item.display_name.clone(),
|
||||||
|
enchantments,
|
||||||
|
durability: item.durability.clone(),
|
||||||
|
stack_size: item.stack_size,
|
||||||
|
food: food_by_name.get(name).cloned(),
|
||||||
|
block: blocks_by_name.get(name).cloned(),
|
||||||
|
})
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
pub(crate) mod music;
|
pub(crate) mod music;
|
||||||
pub(crate) mod qalc;
|
pub(crate) mod qalc;
|
||||||
pub(crate) mod settings;
|
pub(crate) mod settings;
|
||||||
|
pub(crate) mod minecraft;
|
||||||
|
Loading…
Reference in New Issue