Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
HadziqM committed Nov 23, 2024
1 parent 1f5be5b commit cc5c6fe
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 146 deletions.
224 changes: 114 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ resolver = "2"
logger = { git = "https://github.com/HadziqM/myrustlib.git", subdir = "logger", features = [
"discord",
] }
appflow = { git = "https://github.com/HadziqM/myrustlib.git", subdir = "appflow", features = [
"update",
] }
sysdir = { git = "https://github.com/HadziqM/myrustlib.git", subdir = "sysdir" }
appflow = { git = "https://github.com/HadziqM/myrustlib.git", subdir = "appflow" }
macros = { git = "https://github.com/HadziqM/myrustlib.git", subdir = "macros" }

# async runtime
Expand Down
53 changes: 24 additions & 29 deletions common/src/database/raw.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub struct DbCard {
pub char_id: i32,
pub user_id: i64,
pub name: String,
pub gr: i32,
pub hrp: i32,
pub login: i32,
pub weapon_type: i32,
pub char_id: Option<i32>,
pub user_id: Option<i64>,
pub name: Option<String>,
pub gr: Option<i32>,
pub hrp: Option<i32>,
pub login: Option<i32>,
pub weapon_type: Option<i32>,
pub username: String,
pub guild_id: Option<i64>,
pub guild_name: Option<String>,
Expand All @@ -17,19 +17,19 @@ pub struct DbEvent {
pub pity: i32,
pub latest_bounty: String,
pub latest_bounty_time: i64,
pub title: i32,
pub bronze: i32,
pub silver: i32,
pub gold: i32,
pub name: String,
pub title: Option<i32>,
pub bronze: Option<i32>,
pub silver: Option<i32>,
pub gold: Option<i32>,
pub name: Option<String>,
pub char_id: i32,
}

pub struct DbUserData {
// Character_id
pub cid: i32,
// Account_id
pub aid: i32,
// User_id
pub uid: Option<i32>,
}

pub struct DbAccountData {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl DbCard {
"https://media.discordapp.net/attachments/1068440173479739393/1068440373132800080/SAF.png",
"https://media.discordapp.net/attachments/1068440173479739393/1068440372709167174/MS.png"
];
iconlist[self.weapon_type as usize].to_string()
iconlist[self.weapon_type.unwrap_or_default() as usize].to_string()
}
pub fn g_name(&self) -> String {
match &self.guild_name {
Expand All @@ -83,22 +83,17 @@ impl DbCard {
}
}
pub fn hrp(&self) -> u8 {
if self.hrp == 999 {
return 7;
} else if self.hrp > 299 {
return 6;
} else if self.hrp > 99 {
return 5;
} else if self.hrp > 50 {
return 4;
} else if self.hrp > 30 {
return 3;
} else if self.hrp > 1 {
return 2;
match self.hrp.unwrap_or(0) {
999 => 7,
300..=998 => 6,
100..=299 => 5,
51..=99 => 4,
31..=50 => 3,
1..=30 => 2,
_ => 1,
}
1
}
pub fn last_login(&self) -> String {
format!("<t:{}:R>", self.login)
format!("<t:{}:R>", self.login.unwrap_or_default())
}
}
8 changes: 8 additions & 0 deletions common/src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub struct SettingMarket {
pub struct SettingMain {
pub discord: DiscordBotSetting,
pub database: DatabaseSetting,
pub updater: GithubUpdaterSetting,
}

#[derive(Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -185,6 +186,13 @@ pub struct DatabaseSetting {
pub port: u16,
pub database: String,
}
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct GithubUpdaterSetting {
pub repo: String,
pub owner: String,
pub token: Option<String>,
pub app_name: String,
}

#[cfg(test)]
mod test {
Expand Down
71 changes: 71 additions & 0 deletions database/src/card.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use common::database::raw::{DbCard, DbEvent, DbUserData};

use crate::*;

impl Db {
pub async fn fetch_event(&self, discord_id: impl ToString) -> DbResult<DbEvent> {
Ok(sqlx::query_as!(
DbEvent,
"SELECT
characters.name as name,char_id,bounty,gacha,pity,latest_bounty,latest_bounty_time,
title,bronze,silver,gold
FROM discord
JOIN characters on discord.char_id=characters.id
WHERE discord_id=$1",
discord_id.to_string()
)
.fetch_one(&**self)
.await?)
}

pub async fn fetch_card(&self, cid: i32) -> DbResult<DbCard> {
Ok(sqlx::query_as!(
DbCard,
"SELECT characters.id as char_id, user_id,characters.name as name,gr,hrp,weapon_type,
characters.last_login as login,username,guild_id,guilds.name as guild_name
FROM characters
INNER JOIN users ON characters.user_id = users.id
LEFT OUTER JOIN guild_characters ON characters.id = guild_characters.character_id
LEFT OUTER JOIN guilds ON guild_characters.guild_id = guilds.id
WHERE characters.id=$1",
cid
)
.fetch_one(&**self)
.await?)
}

async fn fetch_all_character_id(&self, uid: i64) -> DbResult<Vec<i32>> {
let row = sqlx::query!("SELECT id FROM characters WHERE user_id=$1", uid)
.fetch_all(&**self)
.await?;
let mut cid = Vec::new();
for i in row {
cid.push(i.id)
}
Ok(cid)
}

pub async fn fetch_all_card(&self, user: i64) -> DbResult<Vec<DbCard>> {
let cid = self.fetch_all_character_id(user).await?;
let mut card = Vec::new();
for i in cid {
card.push(self.fetch_card(i).await?);
}
Ok(card)
}

pub async fn fetch_user_data(&self, discord_id: impl ToString) -> DbResult<DbUserData> {
let did = discord_id.to_string();

Ok(sqlx::query_as!(
DbUserData,
"SELECT user_id as uid, char_id as cid
FROM discord_register
LEFT JOIN discord ON discord_register.discord_id=discord.discord_id
WHERE discord.discord_id=$1",
did
)
.fetch_one(&**self)
.await?)
}
}
1 change: 1 addition & 0 deletions database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use thiserror::Error;

pub mod account;
pub mod card;

#[derive(Debug, Error)]
pub enum DbError {
Expand Down
1 change: 1 addition & 0 deletions rain-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ logger.workspace = true
lazy_static.workspace = true
thiserror.workspace = true
common = { path = "../common" }
database = { path = "../database" }
23 changes: 23 additions & 0 deletions rain-bot/src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::sync::Arc;

use serenity::{
all::{CommandInteraction, Context, CreateCommand},
async_trait,
};

use crate::setup::{App, CommandInteractionTrait, MyResult};

pub struct TestCommand;

#[async_trait]
impl CommandInteractionTrait for TestCommand {
fn name(&self) -> String {
"test".to_string()
}
fn command(&self) -> serenity::all::CreateCommand {
CreateCommand::new("test")
}
async fn handle(&self, app: Arc<App>, cmd: CommandInteraction, ctx: Context) -> MyResult<()> {
Ok(())
}
}
37 changes: 31 additions & 6 deletions rain-bot/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use common::setting::SettingAll;
use database::DbError;
use serenity::all::{
ChannelId, Color, Context, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter,
CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage, User, UserId,
Expand All @@ -7,25 +8,49 @@ use thiserror::Error;

#[derive(Debug, Error)]
pub enum MyError {
#[error("Test")]
Test,
#[error("{0}")]
Custom(String),
#[error("sqlx error: {0}")]
Db(DbError),
}

enum Severity {
impl From<DbError> for MyError {
fn from(err: DbError) -> Self {
match err {
DbError::Sqlx(_) => Self::Db(err),
DbError::Custom(str) => Self::Custom(str),
}
}
}

pub enum Severity {
Critical,
FalsePossitive,
CanBeHandledManually,
}

impl MyError {
pub fn severity(&self) -> Severity {
todo!()
match self {
Self::Custom(_) => Severity::CanBeHandledManually,
Self::Db(_) => Severity::Critical,
}
}
pub fn advice(&self) -> String {
todo!()
match self {
Self::Custom(_) => {
String::from("Error writen by author themself, please read carefully")
}
Self::Db(_) => String::from(
"Please report this error to author, or wait till database connection stabilize",
),
}
}
pub fn log(&self) {
todo!()
match self {
Self::Custom(str) => log::warn!("Custom Error: {}", str),
Self::Db(err) => log::error!("Db Error: {}", err),
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions rain-bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ use std::sync::Arc;

use appflow::Appflow;
use common::{setting::SettingAll, SYSDIR};
use database::Db;
use logger::Mylogger;
use serenity::{all::GatewayIntents, Client};
use setup::{App, DiscordHandler};
use tokio::sync::RwLock;

pub mod command;
pub mod error;
pub mod setup;
pub mod utils;

impl Appflow for App {
async fn update_config(self: Arc<Self>) -> appflow::GithubUpdater {
let setting = self.setting.read().await;
let update = &setting.main.updater;
appflow::GithubUpdater {
repo: update.repo.clone(),
owner: update.owner.clone(),
token: update.token.clone(),
app_name: update.app_name.clone(),
}
}

async fn main_process(self: Arc<Self>) {
let setting = self.setting.read().await;
Mylogger::webhook_url(&setting.main.discord.webhook, setting.main.discord.author)
Expand All @@ -37,6 +51,7 @@ impl Appflow for App {
async fn main() {
let setting = SettingAll::load_all();
let app = App {
db: Db::connect(&setting).await.unwrap(),
setting: Arc::new(RwLock::new(setting)),
};
app.init().await
Expand Down
2 changes: 2 additions & 0 deletions rain-bot/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use common::setting::SettingAll;
use database::Db;
use serenity::all::*;
use std::{collections::HashMap, sync::Arc};
use tokio::sync::RwLock;
Expand All @@ -9,6 +10,7 @@ pub type MyResult<T> = Result<T, MyError>;

pub struct App {
pub setting: Arc<RwLock<SettingAll>>,
pub db: Db,
}

pub struct DiscordHandler {
Expand Down
Empty file added rain-bot/src/utils.rs
Empty file.

0 comments on commit cc5c6fe

Please sign in to comment.