Initial DB changes
+ Includes some refactoring as well + More to come + clippy + fmt
This commit is contained in:
+81
-4
@@ -1,21 +1,98 @@
|
||||
use crate::config::BotConfig;
|
||||
use crate::{command, group, GlobalData};
|
||||
use json::JsonValue;
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::{Args, CommandResult};
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::model::channel::{AttachmentType, Message};
|
||||
use serenity::model::prelude::UserId;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[group]
|
||||
#[commands(reload)]
|
||||
#[commands(reload, dump_db, load_db)]
|
||||
pub struct ADMIN;
|
||||
|
||||
pub fn is_admin(user_id: &UserId, cfg: &BotConfig) -> bool {
|
||||
cfg.admins.contains(user_id)
|
||||
}
|
||||
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn reload(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
global_data.reload().await?;
|
||||
if !is_admin(&msg.author.id, &global_data.cfg) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
global_data.reload().await?;
|
||||
msg.reply(&ctx.http, "Reload done ;)").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
async fn dump_db(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
if !is_admin(&msg.author.id, &global_data.cfg) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let db_dump = global_data.db.dump_db()?;
|
||||
|
||||
let output = db_dump.pretty(4);
|
||||
|
||||
msg.author
|
||||
.id
|
||||
.create_dm_channel(&ctx.http)
|
||||
.await?
|
||||
.send_message(&ctx.http, |m| {
|
||||
m.content("The current DB state")
|
||||
.add_file(AttachmentType::Bytes {
|
||||
data: Cow::from(output.as_bytes()),
|
||||
filename: "db.json".to_string(),
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
async fn load_db(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
if !is_admin(&msg.author.id, &global_data.cfg) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(attachment) = msg.attachments.first() {
|
||||
let db_bytes = attachment.download().await?;
|
||||
let db_string = String::from_utf8(db_bytes)?;
|
||||
|
||||
let json_value: JsonValue = match json::parse(&db_string) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
msg.reply(&ctx.http, format!("Error parsing json: {}", e))
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
match global_data.db.import_db(json_value) {
|
||||
Ok(_) => {
|
||||
msg.reply(&ctx.http, "Database imported successfully")
|
||||
.await?
|
||||
}
|
||||
Err(err) => {
|
||||
msg.reply(&ctx.http, format!("Error importing db: {}", err))
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::config::{Channel, GlobalData};
|
||||
use crate::user::UserError;
|
||||
use crate::error::Error;
|
||||
use crate::user::{give_funds, try_take_funds, UserError};
|
||||
use crate::{command, group};
|
||||
use rand::seq::IteratorRandom;
|
||||
use rand::{thread_rng, Rng};
|
||||
@@ -10,7 +11,6 @@ use serenity::model::misc::EmojiIdentifier;
|
||||
use serenity::model::prelude::{Emoji, Message};
|
||||
use serenity::prelude::Mentionable;
|
||||
use serenity::utils::MessageBuilder;
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -45,7 +45,7 @@ impl From<UserError> for RaceError {
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for RaceError {}
|
||||
impl std::error::Error for RaceError {}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum RaceMessage {
|
||||
@@ -133,10 +133,10 @@ async fn add_bet(
|
||||
|
||||
let global = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
global
|
||||
.cfg
|
||||
.user_manager
|
||||
.try_take_funds(bet.author, bet.amount)?;
|
||||
try_take_funds(&global.db, bet.author, bet.amount).map_err(|err| match err {
|
||||
Error::UserError(e) => RaceError::BetFundError(e),
|
||||
_ => panic!("Recv'ed error when trying to bet: {}", err),
|
||||
})?;
|
||||
|
||||
if !racers.iter().any(|r| r.emoji.id == bet.emoji) {
|
||||
return Err(RaceError::RacerNotFound);
|
||||
@@ -296,10 +296,7 @@ async fn race(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
winner_msg.mention(&winner);
|
||||
winner_msg.push_line("");
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
.user_manager
|
||||
.give_funds(winner, payout as u32);
|
||||
give_funds(&global_data.db, winner, payout as u32)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::config::GlobalData;
|
||||
use crate::error::Error;
|
||||
use crate::user::UserError;
|
||||
use crate::user::{get_user, give_funds, transfer_funds, UserError};
|
||||
use crate::{command, group};
|
||||
use rand::{thread_rng, Rng};
|
||||
use serenity::client::Context;
|
||||
@@ -23,7 +23,7 @@ async fn balance(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
|
||||
let user = args.parse::<UserId>().unwrap_or(msg.author.id);
|
||||
|
||||
let wallet = global_data.cfg.user_manager.get_user(user);
|
||||
let wallet = get_user(&global_data.db, user)?;
|
||||
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
@@ -72,12 +72,8 @@ async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
if let Err(e) = global_data
|
||||
.cfg
|
||||
.user_manager
|
||||
.transfer_funds(msg.author.id, target, amount)
|
||||
{
|
||||
if let UserError::NotEnoughFunds = e {
|
||||
if let Err(e) = transfer_funds(&global_data.db, msg.author.id, target, amount) {
|
||||
if let Error::UserError(UserError::NotEnoughFunds) = e {
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
"Sorry pal, I can't give credit. Come back when you're a bit mmmm richer.",
|
||||
@@ -120,10 +116,7 @@ pub async fn give_coin(
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
.user_manager
|
||||
.give_funds(user, number_of_coins as u32);
|
||||
give_funds(&global_data.db, user, number_of_coins as u32)?;
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
|
||||
@@ -141,6 +141,8 @@ pub async fn after(
|
||||
.save(&global_data.args.cfg_path)
|
||||
.await
|
||||
.expect("Error saving config");
|
||||
|
||||
global_data.db.db.flush_async().await.unwrap();
|
||||
}
|
||||
Err(why) => {
|
||||
println!("Command '{}' returned error {:?}", command_name, why);
|
||||
|
||||
+8
-17
@@ -1,6 +1,7 @@
|
||||
use crate::discord::motivate::create_image;
|
||||
use crate::error::Error;
|
||||
use crate::inventory::{InventoryError, ItemData, ItemType};
|
||||
use crate::user::UserError;
|
||||
use crate::user::{get_user, give_item, try_take_funds, try_use_item, UserError};
|
||||
use crate::{command, group, GlobalData};
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::{thread_rng, Rng};
|
||||
@@ -46,7 +47,7 @@ async fn inventory(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
|
||||
let mut inv_msg = MessageBuilder::new();
|
||||
|
||||
let user = global_data.cfg.user_manager.get_user(msg.author.id);
|
||||
let user = get_user(&global_data.db, msg.author.id)?;
|
||||
|
||||
if user.inventory.inventory.is_empty() {
|
||||
msg.reply(&ctx, "Sorry your inventory is empty.").await?;
|
||||
@@ -86,11 +87,7 @@ async fn buy(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
};
|
||||
|
||||
if let Some(item_slot) = global_data.cfg.bot_inventory.get_item(item) {
|
||||
if let Err(err) = global_data
|
||||
.cfg
|
||||
.user_manager
|
||||
.try_take_funds(msg.author.id, item_slot.value() as u32)
|
||||
{
|
||||
if let Err(err) = try_take_funds(&global_data.db, msg.author.id, item_slot.value() as u32) {
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
format!(
|
||||
@@ -116,10 +113,7 @@ async fn buy(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
}
|
||||
};
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
.user_manager
|
||||
.give_item(msg.author.id, item, 1, item_data);
|
||||
give_item(&global_data.db, msg.author.id, item, 1, item_data)?;
|
||||
|
||||
msg.reply(&ctx, format!("Congrats, you now own a '{}'", item))
|
||||
.await?;
|
||||
@@ -150,14 +144,11 @@ async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
}
|
||||
};
|
||||
|
||||
let item_data = match global_data
|
||||
.cfg
|
||||
.user_manager
|
||||
.try_use_item(msg.author.id, item)
|
||||
{
|
||||
let item_data = match try_use_item(&global_data.db, msg.author.id, item) {
|
||||
Ok(i) => i,
|
||||
Err(err) => {
|
||||
if let UserError::InventoryError(InventoryError::NotEnoughItems) = err {
|
||||
if let Error::UserError(UserError::InventoryError(InventoryError::NotEnoughItems)) = err
|
||||
{
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
"Looks like you don't have enough of that item to use it",
|
||||
|
||||
Reference in New Issue
Block a user