-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made the mentions as a database entry
- Loading branch information
1 parent
0fa6878
commit 329cbc9
Showing
18 changed files
with
196 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
-- Add migration script here | ||
CREATE TABLE user_stats ( | ||
user_id BIGINT UNSIGNED NOT NULL, | ||
guild_id BIGINT UNSIGNED NOT NULL, | ||
experience_points INTEGER UNSIGNED NOT NULL, | ||
level INTEGER UNSIGNED NOT NULL, | ||
PRIMARY KEY (user_id, guild_id) | ||
user_id BIGINT UNSIGNED NOT NULL, | ||
guild_id BIGINT UNSIGNED NOT NULL, | ||
experience_points INTEGER UNSIGNED NOT NULL, | ||
level INTEGER UNSIGNED NOT NULL, | ||
PRIMARY KEY (user_id, guild_id) | ||
); | ||
CREATE INDEX idx_user_id ON user_stats (user_id); | ||
CREATE INDEX idx_guild_id ON user_stats (guild_id); | ||
|
||
CREATE TABLE bot_mentions ( | ||
mentions BIGINT UNSIGNED NOT NULL | ||
); | ||
CREATE INDEX idx_mentions ON bot_mentions (mentions); | ||
INSERT INTO bot_mentions (mentions) VALUES (0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::enums::schemas::{LevelsSchema, MentionsSchema}; | ||
use std::collections::HashMap; | ||
|
||
use lazy_static::lazy_static; | ||
|
||
pub const DATABASE_FILENAME: &str = "database/bot_database.sqlite"; | ||
pub const DATABASE_USERS: &str = "user_stats"; | ||
pub const MENTIONS_TABLE_NAME: &str = "bot_mentions"; | ||
|
||
lazy_static! { | ||
#[derive(Debug)] | ||
pub(crate) static ref LEVELS_TABLE: HashMap<LevelsSchema, &'static str> = { | ||
use crate::enums::schemas::LevelsSchema as DbSch; | ||
|
||
HashMap::from([ | ||
(DbSch::UserId, "user_id"), | ||
(DbSch::GuildId, "guild_id"), | ||
(DbSch::ExperiencePoints, "experience_points"), | ||
(DbSch::Level, "level"), | ||
(DbSch::LastQueryTimestamp, "last_query_timestamp") | ||
]) | ||
}; | ||
#[derive(Debug)] | ||
pub(crate) static ref MENTIONS_TABLE: HashMap<MentionsSchema, &'static str> = { | ||
use crate::enums::schemas::MentionsSchema as DbSch; | ||
|
||
HashMap::from([ | ||
(DbSch::Mentions, "mentions"), | ||
]) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
pub mod bot_data; | ||
pub mod command_data; | ||
pub mod database_interactions; | ||
pub mod database; | ||
pub mod embed_media; | ||
pub mod user_data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::data::command_data::Error; | ||
use crate::data::database::{MENTIONS_TABLE, MENTIONS_TABLE_NAME}; | ||
use crate::enums::schemas::MentionsSchema; | ||
use sqlx::sqlite::SqliteQueryResult; | ||
use sqlx::Row; | ||
use sqlx::SqlitePool; | ||
|
||
pub async fn fetch_mentions(db: &SqlitePool) -> Result<i64, Error> { | ||
let query = format!( | ||
"SELECT `{}` FROM `{}`", | ||
MENTIONS_TABLE[&MentionsSchema::Mentions], | ||
MENTIONS_TABLE_NAME, | ||
); | ||
let sql = sqlx::query(&query).fetch_optional(db).await?; | ||
|
||
let row = match sql { | ||
Some(row) => row, | ||
None => return Err(format!("Couldn't find a row to select (SQL: {})", query).into()), | ||
}; | ||
|
||
let queried_mentions = row.get::<i64, &str>(MENTIONS_TABLE[&MentionsSchema::Mentions]); | ||
|
||
Ok(queried_mentions) | ||
} | ||
|
||
pub async fn update_mentions( | ||
db: &SqlitePool, | ||
updated_mentions: i64, | ||
) -> Result<SqliteQueryResult, sqlx::Error> { | ||
let query = format!( | ||
"UPDATE `{}` SET `{}` = ?", | ||
MENTIONS_TABLE_NAME, | ||
MENTIONS_TABLE[&MentionsSchema::Mentions] | ||
); | ||
|
||
sqlx::query(&query).bind(updated_mentions).execute(db).await | ||
} | ||
|
||
pub async fn add_mentions(db: &SqlitePool, n: i64) -> Result<i64, Error> { | ||
let fetched_mentions = fetch_mentions(db).await?; | ||
|
||
update_mentions(db, fetched_mentions + n).await?; | ||
|
||
fetch_mentions(db).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.