Initial DB changes

+ Includes some refactoring as well
+ More to come
+ clippy + fmt
This commit is contained in:
Joey Hines 2023-01-18 17:35:24 -07:00
parent 1db0c5b3c0
commit 766dc6f171
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
10 changed files with 514 additions and 325 deletions

469
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,8 @@ ndm = "0.9.9"
regex = "1.7.0" regex = "1.7.0"
magick_rust = "0.17.0" magick_rust = "0.17.0"
songbird = "0.3.0" songbird = "0.3.0"
json = "0.12.4"
j_db = {git = "https://git.jojodev.com/joeyahines/j_db"}
[dependencies.serenity] [dependencies.serenity]
version = "0.11.5" version = "0.11.5"

View File

@ -1,11 +1,10 @@
use crate::error::Error; use crate::error::Error;
use crate::error::Error::NoAlbumFound;
use crate::imgur; use crate::imgur;
use crate::imgur::Image; use crate::imgur::Image;
use crate::insult_compliment::InsultComplimentTemplate; use crate::insult_compliment::InsultComplimentTemplate;
use crate::inventory::InventoryManager; use crate::inventory::InventoryManager;
use crate::user::UserManager;
use config::{Config, File}; use config::{Config, File};
use j_db::database::Database;
use rand::prelude::SliceRandom; use rand::prelude::SliceRandom;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serenity::model::prelude::UserId; use serenity::model::prelude::UserId;
@ -50,8 +49,8 @@ pub struct BotConfig {
pub story_path: PathBuf, pub story_path: PathBuf,
pub voice_path: PathBuf, pub voice_path: PathBuf,
pub nft_path: PathBuf, pub nft_path: PathBuf,
pub db_path: PathBuf,
pub user_manager: UserManager, pub admins: Vec<UserId>,
pub bot_inventory: InventoryManager, pub bot_inventory: InventoryManager,
@ -129,7 +128,7 @@ impl BotState {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let album = match self.albums.get(album_name) { let album = match self.albums.get(album_name) {
None => return Err(NoAlbumFound), None => return Err(Error::NoAlbumFound),
Some(a) => a, Some(a) => a,
}; };
@ -157,11 +156,11 @@ impl BotState {
} }
} }
#[derive(Debug)]
pub struct GlobalData { pub struct GlobalData {
pub args: Args, pub args: Args,
pub cfg: BotConfig, pub cfg: BotConfig,
pub bot_state: BotState, pub bot_state: BotState,
pub db: Database,
} }
impl GlobalData { impl GlobalData {
@ -169,6 +168,7 @@ impl GlobalData {
Ok(Self { Ok(Self {
args, args,
bot_state: BotState::new(&cfg).await?, bot_state: BotState::new(&cfg).await?,
db: Database::new(&cfg.db_path)?,
cfg, cfg,
}) })
} }

View File

@ -1,21 +1,98 @@
use crate::config::BotConfig;
use crate::{command, group, GlobalData}; use crate::{command, group, GlobalData};
use json::JsonValue;
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::standard::{Args, CommandResult}; 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] #[group]
#[commands(reload)] #[commands(reload, dump_db, load_db)]
pub struct ADMIN; pub struct ADMIN;
pub fn is_admin(user_id: &UserId, cfg: &BotConfig) -> bool {
cfg.admins.contains(user_id)
}
#[command] #[command]
#[only_in(guilds)]
async fn reload(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { async fn reload(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let mut data = ctx.data.write().await; let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap(); 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?; msg.reply(&ctx.http, "Reload done ;)").await?;
Ok(()) 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(())
}

View File

@ -1,5 +1,6 @@
use crate::config::{Channel, GlobalData}; 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 crate::{command, group};
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
@ -10,7 +11,6 @@ use serenity::model::misc::EmojiIdentifier;
use serenity::model::prelude::{Emoji, Message}; use serenity::model::prelude::{Emoji, Message};
use serenity::prelude::Mentionable; use serenity::prelude::Mentionable;
use serenity::utils::MessageBuilder; use serenity::utils::MessageBuilder;
use std::error::Error;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::time::Duration; 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)] #[derive(Clone, Debug)]
pub enum RaceMessage { pub enum RaceMessage {
@ -133,10 +133,10 @@ async fn add_bet(
let global = data.get_mut::<GlobalData>().unwrap(); let global = data.get_mut::<GlobalData>().unwrap();
global try_take_funds(&global.db, bet.author, bet.amount).map_err(|err| match err {
.cfg Error::UserError(e) => RaceError::BetFundError(e),
.user_manager _ => panic!("Recv'ed error when trying to bet: {}", err),
.try_take_funds(bet.author, bet.amount)?; })?;
if !racers.iter().any(|r| r.emoji.id == bet.emoji) { if !racers.iter().any(|r| r.emoji.id == bet.emoji) {
return Err(RaceError::RacerNotFound); return Err(RaceError::RacerNotFound);
@ -296,10 +296,7 @@ async fn race(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
winner_msg.mention(&winner); winner_msg.mention(&winner);
winner_msg.push_line(""); winner_msg.push_line("");
global_data give_funds(&global_data.db, winner, payout as u32)?;
.cfg
.user_manager
.give_funds(winner, payout as u32);
} }
} }
} }

View File

@ -1,6 +1,6 @@
use crate::config::GlobalData; use crate::config::GlobalData;
use crate::error::Error; use crate::error::Error;
use crate::user::UserError; use crate::user::{get_user, give_funds, transfer_funds, UserError};
use crate::{command, group}; use crate::{command, group};
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use serenity::client::Context; 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 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( msg.reply(
&ctx.http, &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 mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap(); let global_data = data.get_mut::<GlobalData>().unwrap();
if let Err(e) = global_data if let Err(e) = transfer_funds(&global_data.db, msg.author.id, target, amount) {
.cfg if let Error::UserError(UserError::NotEnoughFunds) = e {
.user_manager
.transfer_funds(msg.author.id, target, amount)
{
if let UserError::NotEnoughFunds = e {
msg.reply( msg.reply(
&ctx.http, &ctx.http,
"Sorry pal, I can't give credit. Come back when you're a bit mmmm richer.", "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 mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap(); let global_data = data.get_mut::<GlobalData>().unwrap();
global_data give_funds(&global_data.db, user, number_of_coins as u32)?;
.cfg
.user_manager
.give_funds(user, number_of_coins as u32);
global_data global_data
.cfg .cfg

View File

@ -141,6 +141,8 @@ pub async fn after(
.save(&global_data.args.cfg_path) .save(&global_data.args.cfg_path)
.await .await
.expect("Error saving config"); .expect("Error saving config");
global_data.db.db.flush_async().await.unwrap();
} }
Err(why) => { Err(why) => {
println!("Command '{}' returned error {:?}", command_name, why); println!("Command '{}' returned error {:?}", command_name, why);

View File

@ -1,6 +1,7 @@
use crate::discord::motivate::create_image; use crate::discord::motivate::create_image;
use crate::error::Error;
use crate::inventory::{InventoryError, ItemData, ItemType}; 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 crate::{command, group, GlobalData};
use rand::prelude::SliceRandom; use rand::prelude::SliceRandom;
use rand::{thread_rng, Rng}; 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 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() { if user.inventory.inventory.is_empty() {
msg.reply(&ctx, "Sorry your inventory is empty.").await?; 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 Some(item_slot) = global_data.cfg.bot_inventory.get_item(item) {
if let Err(err) = global_data if let Err(err) = try_take_funds(&global_data.db, msg.author.id, item_slot.value() as u32) {
.cfg
.user_manager
.try_take_funds(msg.author.id, item_slot.value() as u32)
{
msg.reply( msg.reply(
&ctx.http, &ctx.http,
format!( format!(
@ -116,10 +113,7 @@ async fn buy(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
} }
}; };
global_data give_item(&global_data.db, msg.author.id, item, 1, item_data)?;
.cfg
.user_manager
.give_item(msg.author.id, item, 1, item_data);
msg.reply(&ctx, format!("Congrats, you now own a '{}'", item)) msg.reply(&ctx, format!("Congrats, you now own a '{}'", item))
.await?; .await?;
@ -150,14 +144,11 @@ async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
} }
}; };
let item_data = match global_data let item_data = match try_use_item(&global_data.db, msg.author.id, item) {
.cfg
.user_manager
.try_use_item(msg.author.id, item)
{
Ok(i) => i, Ok(i) => i,
Err(err) => { Err(err) => {
if let UserError::InventoryError(InventoryError::NotEnoughItems) = err { if let Error::UserError(UserError::InventoryError(InventoryError::NotEnoughItems)) = err
{
msg.reply( msg.reply(
&ctx.http, &ctx.http,
"Looks like you don't have enough of that item to use it", "Looks like you don't have enough of that item to use it",

View File

@ -13,6 +13,7 @@ pub enum Error {
TeraError(tera::Error), TeraError(tera::Error),
NoAlbumFound, NoAlbumFound,
UserError(user::UserError), UserError(user::UserError),
DbError(j_db::error::JDbError),
} }
impl StdError for Error {} impl StdError for Error {}
@ -41,6 +42,18 @@ impl From<tera::Error> for Error {
} }
} }
impl From<user::UserError> for Error {
fn from(err: user::UserError) -> Self {
Self::UserError(err)
}
}
impl From<j_db::error::JDbError> for Error {
fn from(err: j_db::error::JDbError) -> Self {
Self::DbError(err)
}
}
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -50,6 +63,7 @@ impl Display for Error {
Error::TeraError(e) => write!(f, "Tera error: {}", e), Error::TeraError(e) => write!(f, "Tera error: {}", e),
Error::NoAlbumFound => write!(f, "No album found"), Error::NoAlbumFound => write!(f, "No album found"),
Error::UserError(e) => write!(f, "User error: {}", e), Error::UserError(e) => write!(f, "User error: {}", e),
Error::DbError(e) => write!(f, "DB error: {}", e),
} }
} }
} }

View File

@ -1,4 +1,7 @@
use crate::inventory::{InventoryError, InventoryManager, InventorySlot, ItemData, ItemType}; use crate::error::Error;
use crate::inventory::{InventoryError, InventoryManager, ItemData, ItemType};
use j_db::database::Database;
use j_db::model::JdbModel;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serenity::model::id::UserId; use serenity::model::id::UserId;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
@ -29,93 +32,128 @@ impl From<InventoryError> for UserError {
#[derive(Debug, Deserialize, Serialize, Clone)] #[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User { pub struct User {
pub id: Option<u64>,
pub user_id: UserId, pub user_id: UserId,
pub coin_count: i64, pub coin_count: i64,
#[serde(default)] #[serde(default)]
pub inventory: InventoryManager, pub inventory: InventoryManager,
} }
#[derive(Debug, Deserialize, Serialize, Clone, Default)] impl User {
pub struct UserManager { pub fn new(user_id: UserId) -> Self {
#[serde(default)] Self {
users: Vec<User>, id: None,
user_id,
coin_count: 0,
inventory: Default::default(),
}
}
} }
#[allow(dead_code)] impl JdbModel for User {
impl UserManager { fn id(&self) -> Option<u64> {
pub fn get_user(&mut self, discord_id: UserId) -> &mut User { self.id
if let Some(user_ndx) = self.users.iter().position(|u| u.user_id == discord_id) {
&mut self.users[user_ndx]
} else {
self.users.push(User {
user_id: discord_id,
coin_count: 100,
inventory: Default::default(),
});
self.users.last_mut().unwrap()
}
} }
pub fn transfer_funds( fn set_id(&mut self, id: u64) {
&mut self, self.id = Some(id)
src: UserId, }
dest: UserId,
amount: u32,
) -> Result<(), UserError> {
self.try_take_funds(src, amount)?;
self.give_funds(dest, amount); fn tree() -> String {
"Users".to_string()
}
}
pub fn get_user(db: &Database, discord_id: UserId) -> Result<User, Error> {
let user: Option<User> = db
.filter(|_, user: &User| user.user_id == discord_id)?
.next();
let user = match user {
None => db.insert::<User>(User::new(discord_id))?,
Some(user) => user,
};
Ok(user)
}
pub fn transfer_funds(db: &Database, src: UserId, dest: UserId, amount: u32) -> Result<(), Error> {
try_take_funds(db, src, amount)?;
give_funds(db, dest, amount)?;
Ok(()) Ok(())
} }
pub fn give_funds(&mut self, discord_id: UserId, amount: u32) { pub fn give_funds(db: &Database, discord_id: UserId, amount: u32) -> Result<(), Error> {
let mut wallet = self.get_user(discord_id); let mut wallet = get_user(db, discord_id)?;
wallet.coin_count += amount as i64; wallet.coin_count += amount as i64;
}
pub fn try_take_funds(&mut self, discord_id: UserId, amount: u32) -> Result<(), UserError> { db.insert::<User>(wallet)?;
let mut wallet = self.get_user(discord_id);
if wallet.coin_count < amount as i64 {
Err(UserError::NotEnoughFunds)
} else {
wallet.coin_count -= amount as i64;
Ok(()) Ok(())
} }
pub fn try_take_funds(db: &Database, discord_id: UserId, amount: u32) -> Result<(), Error> {
let mut wallet = get_user(db, discord_id)?;
if wallet.coin_count < amount as i64 {
return Err(UserError::NotEnoughFunds.into());
} }
pub fn get_item(&mut self, discord_id: UserId, item: ItemType) -> Option<&mut InventorySlot> { wallet.coin_count -= amount as i64;
self.get_user(discord_id).inventory.get_item(item)
}
pub fn give_item( db.insert::<User>(wallet)?;
&mut self,
Ok(())
}
pub fn give_item(
db: &Database,
discord_id: UserId, discord_id: UserId,
item: ItemType, item: ItemType,
quantity: i64, quantity: i64,
item_data: Option<ItemData>, item_data: Option<ItemData>,
) { ) -> Result<(), Error> {
self.get_user(discord_id) let mut user = get_user(db, discord_id)?;
.inventory
.give_item(item, quantity, item_data);
}
pub fn try_take_item( user.inventory.give_item(item, quantity, item_data);
&mut self,
db.insert::<User>(user)?;
Ok(())
}
#[allow(dead_code)]
pub fn try_take_item(
db: &Database,
discord_id: UserId, discord_id: UserId,
item: ItemType, item: ItemType,
quantity: i64, quantity: i64,
) -> Result<Option<ItemData>, UserError> { ) -> Result<Option<ItemData>, Error> {
Ok(self let mut user = get_user(db, discord_id)?;
.get_user(discord_id)
.inventory
.try_take_item(item, quantity)?)
}
pub fn try_use_item( let item = user
&mut self, .inventory
.try_take_item(item, quantity)
.map_err(UserError::InventoryError)?;
db.insert::<User>(user)?;
Ok(item)
}
pub fn try_use_item(
db: &Database,
discord_id: UserId, discord_id: UserId,
item: ItemType, item: ItemType,
) -> Result<Option<ItemData>, UserError> { ) -> Result<Option<ItemData>, Error> {
Ok(self.get_user(discord_id).inventory.try_use_item(item)?) let mut user = get_user(db, discord_id)?;
}
let item = user
.inventory
.try_use_item(item)
.map_err(UserError::InventoryError)?;
db.insert::<User>(user)?;
Ok(item)
} }