Add minecraft durability command

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/2/head
trivernis 3 years ago
parent f630161356
commit 56a939a244
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -10,6 +10,13 @@
</Attribute>
</value>
</entry>
<entry key="/src/main.rs">
<value>
<Attribute>
<option name="separator" value=":" />
</Attribute>
</value>
</entry>
</map>
</option>
</component>

50
Cargo.lock generated

@ -12,6 +12,12 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
[[package]]
name = "anyhow"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b"
[[package]]
name = "async-trait"
version = "0.1.48"
@ -313,6 +319,12 @@ dependencies = [
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "h2"
version = "0.3.2"
@ -443,6 +455,30 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "include_dir"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d58bdeb22b1c4691106c084b1063781904c35d0f22eda2a283598968eac61a"
dependencies = [
"glob",
"include_dir_impl",
"proc-macro-hack",
]
[[package]]
name = "include_dir_impl"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327869970574819d24d1dca25c891856144d29159ab797fa9dc725c5c3f57215"
dependencies = [
"anyhow",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "indexmap"
version = "1.6.2"
@ -560,6 +596,19 @@ dependencies = [
"unicase",
]
[[package]]
name = "minecraft-data-rs"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd60c77ef4902ad297b11bc7f8ad8857177d856e763ab92eb717f33c83e0e254"
dependencies = [
"include_dir",
"serde",
"serde_derive",
"serde_json",
"thiserror",
]
[[package]]
name = "miniz_oxide"
version = "0.4.4"
@ -1088,6 +1137,7 @@ name = "tobi-rs"
version = "0.1.0"
dependencies = [
"dotenv",
"minecraft-data-rs",
"parking_lot",
"rusqlite",
"serde",

@ -15,4 +15,5 @@ rusqlite = "0.24"
serde_derive = "1.0.125"
serde = "1.0.125"
thiserror = "1.0.24"
parking_lot = "0.11.1"
parking_lot = "0.11.1"
minecraft-data-rs = "0.1.0"

@ -1,9 +1,13 @@
use crate::commands::minecraft::Minecraft;
use crate::database::{get_database, Database};
use crate::utils::error::{BotError, BotResult};
use crate::utils::store::{Store, StoreData};
use serenity::async_trait;
use serenity::client::EventHandler;
use serenity::client::{Context, EventHandler};
use serenity::framework::standard::macros::hook;
use serenity::framework::standard::CommandResult;
use serenity::framework::StandardFramework;
use serenity::model::channel::Message;
use serenity::Client;
struct Handler;
@ -25,13 +29,26 @@ pub async fn get_client() -> BotResult<Client> {
}
pub fn get_framework() -> StandardFramework {
StandardFramework::default().configure(|c| {
c.prefix(
dotenv::var("BOT_PREFIX")
.unwrap_or("~!".to_string())
.as_str(),
)
.allow_dm(true)
.ignore_bots(true)
})
StandardFramework::default()
.configure(|c| {
c.prefix(
dotenv::var("BOT_PREFIX")
.unwrap_or("~!".to_string())
.as_str(),
)
.allow_dm(true)
.ignore_bots(true)
})
.group(&crate::commands::minecraft::MINECRAFT_GROUP)
.group(&crate::GENERAL_GROUP)
.after(after_hook)
}
#[hook]
async fn after_hook(ctx: &Context, msg: &Message, cmd_name: &str, error: CommandResult) {
// Print out an error if it happened
if let Err(why) = error {
let _ = msg.channel_id.say(&ctx, format!("{}", why)).await;
println!("Error in {}: {:?}", cmd_name, why);
}
}

@ -0,0 +1,42 @@
use serenity::model::channel::Message;
use crate::utils::store::{Store, StoreData};
use serenity::client::Context;
use serenity::framework::standard::{
macros::{command, group},
Args, CommandError, CommandResult,
};
#[group]
#[commands(durability)]
pub(crate) struct Minecraft;
#[command]
pub(crate) async fn durability(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if args.is_empty() {
return Err(CommandError::from("You need to provide an item name"));
}
let data = ctx.data.read().await;
let store = data.get::<Store>().expect("Failed to get store");
let item_name = args.message().to_lowercase();
let items_by_name = store.minecraft_data_api.items.items_by_name()?;
let item = items_by_name
.get(&item_name)
.ok_or(CommandError::from(format!(
"The item `{}` could not be found",
item_name
)))?;
msg.channel_id
.say(
ctx,
format!(
"The durability for `{}` is `{:?}`",
item.display_name, item.durability
),
)
.await?;
Ok(())
}

@ -0,0 +1 @@
pub(crate) mod minecraft;

@ -1,5 +1,5 @@
use crate::client::get_client;
use serenity::client::{Context, EventHandler};
use serenity::client::Context;
use serenity::framework::standard::{
macros::{command, group},
CommandResult,
@ -7,7 +7,9 @@ use serenity::framework::standard::{
use serenity::model::channel::Message;
pub(crate) mod client;
mod commands;
pub(crate) mod database;
mod providers;
pub(crate) mod utils;
#[group]

@ -1,3 +1,4 @@
use serenity::framework::standard::CommandError;
use thiserror::Error;
pub type BotResult<T> = Result<T, BotError>;
@ -12,4 +13,7 @@ pub enum BotError {
#[error("Missing Bot Token")]
MissingToken,
#[error("Minecraft Data Error: {0}")]
MinecraftDataError(#[from] minecraft_data_rs::DataError),
}

@ -7,12 +7,16 @@ pub struct Store;
pub struct StoreData {
pub database: Arc<Mutex<Database>>,
pub minecraft_data_api: minecraft_data_rs::api::Api,
}
impl StoreData {
pub fn new(database: Database) -> StoreData {
Self {
database: Arc::new(Mutex::new(database)),
minecraft_data_api: minecraft_data_rs::api::Api::new(
minecraft_data_rs::api::versions::latest_stable().unwrap(),
),
}
}
}

Loading…
Cancel
Save