Add leaderboard command and XP winning system

This commit is contained in:
2025-08-21 02:14:56 -03:00
parent 4563c49245
commit 0318f002fc
11 changed files with 281 additions and 36 deletions
+37 -3
View File
@@ -88,6 +88,7 @@ pub struct User {
pub last_seen_at: UtcDateTime,
pub supporter_since: Option<UtcDateTime>,
pub supporter_until: Option<UtcDateTime>,
pub is_banned_from_leaderboard: Option<bool>,
}
impl<'a> Object<'a> for User {
@@ -101,6 +102,7 @@ impl<'a> Object<'a> for User {
OffsetDateTime,
Option<OffsetDateTime>,
Option<OffsetDateTime>,
Option<bool>,
);
fn from_row(row: Self::Row) -> Self {
@@ -114,6 +116,7 @@ impl<'a> Object<'a> for User {
last_seen_at: row.6.into(),
supporter_since: row.7.map(|i| i.into()),
supporter_until: row.8.map(|i| i.into()),
is_banned_from_leaderboard: row.9,
}
}
@@ -128,6 +131,7 @@ impl<'a> Object<'a> for User {
value.last_seen_at.into(),
value.supporter_since.map(|i| i.into()),
value.supporter_until.map(|i| i.into()),
Some(value.is_banned_from_leaderboard.unwrap_or(false)),
)
}
}
@@ -153,7 +157,8 @@ impl User {
joined_at,
last_seen_at,
supporter_since,
supporter_until
supporter_until,
is_banned_from_leaderboard
FROM
payphone.users
WHERE
@@ -191,8 +196,9 @@ impl User {
joined_at,
last_seen_at,
supporter_since,
supporter_until
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
supporter_until,
is_banned_from_leaderboard
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
"#,
Self::to_row(self.clone()),
)
@@ -261,10 +267,38 @@ impl From<TwilightUser> for User {
last_seen_at: UtcDateTime::now(),
supporter_since: None,
supporter_until: None,
is_banned_from_leaderboard: None,
}
}
}
/// Gets a user from the DB and sets their last seen timestamp or inserts a new user from Discord.
///
/// Arguments:
/// * `user` - The Twilight user to get or create.
/// * `session` - The Scylla session to use for database operations.
///
/// Returns:
/// [`Result<User, DatabaseError>`] - The user from the database or a new user created from
/// Discord.
pub async fn get_or_create_user_and_set_last_seen(
user: TwilightUser,
session: &Session,
) -> Result<User, DatabaseError> {
let db_user = User::get_by_id(user.id.get(), session).await?;
if let Some(mut user) = db_user {
user.last_seen_at = UtcDateTime::now();
return Ok(user);
}
let new_user = User::from(user);
new_user.insert(session).await?;
Ok(new_user)
}
#[derive(Debug, Clone)]
pub struct Server {
pub id: u64,