first commit
This commit is contained in:
commit
0cb536b42b
38 changed files with 9044 additions and 0 deletions
72
src/commands/eco/balance.rs
Normal file
72
src/commands/eco/balance.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
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,
|
||||
}]
|
||||
}
|
||||
async fn constructed(&mut self, _: Client) {}
|
||||
async fn event(&mut self, _: Client, _: ClientEvent) {}
|
||||
|
||||
async fn execute(&mut self, client: Client, player: Player, args: ParsedArguments) {
|
||||
let id = match args.get("user_id") {
|
||||
Some(ParsedArgument::String(s)) => s.as_str(),
|
||||
_ => player._id.as_str(),
|
||||
};
|
||||
|
||||
let user = sqlx::query_as::<_, User>("SELECT * FROM users WHERE _id = $1")
|
||||
.bind(id)
|
||||
.fetch_optional(self.pool.as_ref())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
match user {
|
||||
Some(user) => {
|
||||
client
|
||||
.message(format!("You have {} coins.", user.balance))
|
||||
.await;
|
||||
}
|
||||
None => {
|
||||
client.message("No user found.").await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue