copper/src/commands/eco/balance.rs

85 lines
2.2 KiB
Rust

use crate::User;
use crate::client::Client;
use crate::client::ClientEvent;
use crate::client::Player;
use crate::commands::Command;
use crate::commands::argument::{ArgumentSpec, ArgumentType, ParsedArgument, ParsedArguments};
use std::sync::Arc;
use async_trait::async_trait;
use sqlx::Pool;
use sqlx::Postgres;
pub struct BalanceCommand {
pool: Arc<Pool<Postgres>>,
}
impl BalanceCommand {
pub fn new(pool: Arc<Pool<Postgres>>) -> Self {
Self { pool }
}
}
#[async_trait]
impl Command for BalanceCommand {
fn name(&self) -> &'static str {
"balance"
}
fn aliases(&self) -> &[&'static str] {
&["bal"]
}
fn category(&self) -> &'static str {
"eco"
}
fn description(&self) -> &'static str {
"View your balance."
}
fn argument_spec(&self) -> &'static [ArgumentSpec] {
&[ArgumentSpec {
name: "user_id",
arg_type: ArgumentType::String,
required: false,
default: None,
children: &[],
}]
}
async fn constructed(&mut self, _: Client) {}
async fn event(&mut self, _: Client, _: ClientEvent) {}
async fn execute(
&mut self,
client: Client,
player: Player,
args: ParsedArguments,
command_user: User,
) {
let user = match args.get("user_id") {
Some(ParsedArgument::String(s)) => {
let id = s.as_str();
sqlx::query_as::<_, User>("SELECT * FROM users WHERE _id = $1")
.bind(id)
.fetch_optional(self.pool.as_ref())
.await
.unwrap()
}
_ => Some(command_user),
};
match user {
Some(user) => {
let mut who: String = "You have".to_string();
if user._id != player._id.as_str() {
who = format!("{} has ", player._id);
}
client
.message(format!("{} {} coins.", who, user.balance))
.await;
}
None => {
client.message("No user found.").await;
}
}
}
}