Initial DB changes
+ Includes some refactoring as well + More to come + clippy + fmt
This commit is contained in:
parent
1db0c5b3c0
commit
766dc6f171
469
Cargo.lock
generated
469
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -17,6 +17,8 @@ ndm = "0.9.9"
|
||||
regex = "1.7.0"
|
||||
magick_rust = "0.17.0"
|
||||
songbird = "0.3.0"
|
||||
json = "0.12.4"
|
||||
j_db = {git = "https://git.jojodev.com/joeyahines/j_db"}
|
||||
|
||||
[dependencies.serenity]
|
||||
version = "0.11.5"
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
use crate::error::Error;
|
||||
use crate::error::Error::NoAlbumFound;
|
||||
use crate::imgur;
|
||||
use crate::imgur::Image;
|
||||
use crate::insult_compliment::InsultComplimentTemplate;
|
||||
use crate::inventory::InventoryManager;
|
||||
use crate::user::UserManager;
|
||||
use config::{Config, File};
|
||||
use j_db::database::Database;
|
||||
use rand::prelude::SliceRandom;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::model::prelude::UserId;
|
||||
@ -50,8 +49,8 @@ pub struct BotConfig {
|
||||
pub story_path: PathBuf,
|
||||
pub voice_path: PathBuf,
|
||||
pub nft_path: PathBuf,
|
||||
|
||||
pub user_manager: UserManager,
|
||||
pub db_path: PathBuf,
|
||||
pub admins: Vec<UserId>,
|
||||
|
||||
pub bot_inventory: InventoryManager,
|
||||
|
||||
@ -129,7 +128,7 @@ impl BotState {
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
let album = match self.albums.get(album_name) {
|
||||
None => return Err(NoAlbumFound),
|
||||
None => return Err(Error::NoAlbumFound),
|
||||
Some(a) => a,
|
||||
};
|
||||
|
||||
@ -157,11 +156,11 @@ impl BotState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GlobalData {
|
||||
pub args: Args,
|
||||
pub cfg: BotConfig,
|
||||
pub bot_state: BotState,
|
||||
pub db: Database,
|
||||
}
|
||||
|
||||
impl GlobalData {
|
||||
@ -169,6 +168,7 @@ impl GlobalData {
|
||||
Ok(Self {
|
||||
args,
|
||||
bot_state: BotState::new(&cfg).await?,
|
||||
db: Database::new(&cfg.db_path)?,
|
||||
cfg,
|
||||
})
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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",
|
||||
|
||||
14
src/error.rs
14
src/error.rs
@ -13,6 +13,7 @@ pub enum Error {
|
||||
TeraError(tera::Error),
|
||||
NoAlbumFound,
|
||||
UserError(user::UserError),
|
||||
DbError(j_db::error::JDbError),
|
||||
}
|
||||
|
||||
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 {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
@ -50,6 +63,7 @@ impl Display for Error {
|
||||
Error::TeraError(e) => write!(f, "Tera error: {}", e),
|
||||
Error::NoAlbumFound => write!(f, "No album found"),
|
||||
Error::UserError(e) => write!(f, "User error: {}", e),
|
||||
Error::DbError(e) => write!(f, "DB error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
158
src/user/mod.rs
158
src/user/mod.rs
@ -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 serenity::model::id::UserId;
|
||||
use std::fmt::{Display, Formatter};
|
||||
@ -29,93 +32,128 @@ impl From<InventoryError> for UserError {
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct User {
|
||||
pub id: Option<u64>,
|
||||
pub user_id: UserId,
|
||||
pub coin_count: i64,
|
||||
#[serde(default)]
|
||||
pub inventory: InventoryManager,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
pub struct UserManager {
|
||||
#[serde(default)]
|
||||
users: Vec<User>,
|
||||
impl User {
|
||||
pub fn new(user_id: UserId) -> Self {
|
||||
Self {
|
||||
id: None,
|
||||
user_id,
|
||||
coin_count: 0,
|
||||
inventory: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl UserManager {
|
||||
pub fn get_user(&mut self, discord_id: UserId) -> &mut User {
|
||||
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()
|
||||
}
|
||||
impl JdbModel for User {
|
||||
fn id(&self) -> Option<u64> {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn transfer_funds(
|
||||
&mut self,
|
||||
src: UserId,
|
||||
dest: UserId,
|
||||
amount: u32,
|
||||
) -> Result<(), UserError> {
|
||||
self.try_take_funds(src, amount)?;
|
||||
fn set_id(&mut self, id: u64) {
|
||||
self.id = Some(id)
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn give_funds(&mut self, discord_id: UserId, amount: u32) {
|
||||
let mut wallet = self.get_user(discord_id);
|
||||
pub fn give_funds(db: &Database, discord_id: UserId, amount: u32) -> Result<(), Error> {
|
||||
let mut wallet = get_user(db, discord_id)?;
|
||||
wallet.coin_count += amount as i64;
|
||||
}
|
||||
|
||||
pub fn try_take_funds(&mut self, discord_id: UserId, amount: u32) -> Result<(), UserError> {
|
||||
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;
|
||||
db.insert::<User>(wallet)?;
|
||||
|
||||
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> {
|
||||
self.get_user(discord_id).inventory.get_item(item)
|
||||
}
|
||||
wallet.coin_count -= amount as i64;
|
||||
|
||||
pub fn give_item(
|
||||
&mut self,
|
||||
db.insert::<User>(wallet)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn give_item(
|
||||
db: &Database,
|
||||
discord_id: UserId,
|
||||
item: ItemType,
|
||||
quantity: i64,
|
||||
item_data: Option<ItemData>,
|
||||
) {
|
||||
self.get_user(discord_id)
|
||||
.inventory
|
||||
.give_item(item, quantity, item_data);
|
||||
}
|
||||
) -> Result<(), Error> {
|
||||
let mut user = get_user(db, discord_id)?;
|
||||
|
||||
pub fn try_take_item(
|
||||
&mut self,
|
||||
user.inventory.give_item(item, quantity, item_data);
|
||||
|
||||
db.insert::<User>(user)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn try_take_item(
|
||||
db: &Database,
|
||||
discord_id: UserId,
|
||||
item: ItemType,
|
||||
quantity: i64,
|
||||
) -> Result<Option<ItemData>, UserError> {
|
||||
Ok(self
|
||||
.get_user(discord_id)
|
||||
.inventory
|
||||
.try_take_item(item, quantity)?)
|
||||
}
|
||||
) -> Result<Option<ItemData>, Error> {
|
||||
let mut user = get_user(db, discord_id)?;
|
||||
|
||||
pub fn try_use_item(
|
||||
&mut self,
|
||||
let item = user
|
||||
.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,
|
||||
item: ItemType,
|
||||
) -> Result<Option<ItemData>, UserError> {
|
||||
Ok(self.get_user(discord_id).inventory.try_use_item(item)?)
|
||||
}
|
||||
) -> Result<Option<ItemData>, Error> {
|
||||
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)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user