Add support for extras, conditionally displaying commands in the help command, and start working on moderation commands
This commit is contained in:
+120
-58
@@ -172,15 +172,48 @@ impl User {
|
||||
.into_rows_result()
|
||||
.map_err(|_| DatabaseError::ResultNotRows)?;
|
||||
|
||||
match result.single_row::<<User as Object>::Row>() {
|
||||
Ok(row) => Ok(Some(Self::from_row(row))),
|
||||
Err(error) => match error {
|
||||
SingleRowError::DeserializationFailed(_) | SingleRowError::TypeCheckFailed(_) => {
|
||||
Err(DatabaseError::DeserializationFailed)
|
||||
}
|
||||
SingleRowError::UnexpectedRowCount(_) => Ok(None),
|
||||
},
|
||||
get_single_row_from_result(&result).await
|
||||
}
|
||||
|
||||
pub async fn get_by_name(
|
||||
name: String,
|
||||
session: &Session,
|
||||
) -> Result<Option<Self>, DatabaseError> {
|
||||
let Some(user_by_name) = UserByName::get(name, session).await? else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Self::get_by_id(user_by_name.user_id, session).await
|
||||
}
|
||||
|
||||
/// Gets a user from the DB and sets their last seen timestamp or inserts a new user from Discord.
|
||||
///
|
||||
/// It also updates the user's username.
|
||||
///
|
||||
/// 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_and_set_last_seen(
|
||||
user: TwilightUser,
|
||||
session: &Session,
|
||||
) -> Result<Self, DatabaseError> {
|
||||
let db_user = User::get_by_id(user.id.get(), session).await?;
|
||||
|
||||
if let Some(mut db_user) = db_user {
|
||||
db_user.last_seen_at = UtcDateTime::now();
|
||||
db_user.name = user.name;
|
||||
|
||||
return Ok(db_user);
|
||||
}
|
||||
|
||||
let new_user = User::from(user);
|
||||
new_user.insert(session).await?;
|
||||
|
||||
Ok(new_user)
|
||||
}
|
||||
|
||||
pub async fn insert(&self, session: &Session) -> Result<(), DatabaseError> {
|
||||
@@ -272,34 +305,50 @@ impl From<TwilightUser> for User {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets a user from the DB and sets their last seen timestamp or inserts a new user from Discord.
|
||||
///
|
||||
/// It also updates the user's username.
|
||||
///
|
||||
/// 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?;
|
||||
#[derive(Debug, Clone)]
|
||||
struct UserByName {
|
||||
pub user_id: u64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
if let Some(mut db_user) = db_user {
|
||||
db_user.last_seen_at = UtcDateTime::now();
|
||||
db_user.name = user.name;
|
||||
impl<'a> Object<'a> for UserByName {
|
||||
type Row = (i64, String);
|
||||
|
||||
return Ok(db_user);
|
||||
fn from_row(row: Self::Row) -> Self {
|
||||
Self {
|
||||
user_id: i64_to_u64(row.0),
|
||||
name: row.1,
|
||||
}
|
||||
}
|
||||
|
||||
let new_user = User::from(user);
|
||||
new_user.insert(session).await?;
|
||||
fn to_row(value: Self) -> Self::Row {
|
||||
(u64_to_i64(value.user_id), value.name)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(new_user)
|
||||
impl UserByName {
|
||||
async fn get(name: String, session: &Session) -> Result<Option<Self>, DatabaseError> {
|
||||
let result = session
|
||||
.query_unpaged(
|
||||
r#"
|
||||
SELECT
|
||||
user_id,
|
||||
name
|
||||
FROM
|
||||
payphone.users_by_name
|
||||
WHERE
|
||||
name = ?
|
||||
LIMIT 1;
|
||||
"#,
|
||||
(name,),
|
||||
)
|
||||
.await
|
||||
.map_err(DatabaseError::QueryError)?
|
||||
.into_rows_result()
|
||||
.map_err(|_| DatabaseError::ResultNotRows)?;
|
||||
|
||||
get_single_row_from_result(&result).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -1192,37 +1241,50 @@ impl Message {
|
||||
.map_err(|_| DatabaseError::ResultNotRows)?;
|
||||
|
||||
if result.rows_num() == 0 {
|
||||
let message_by_propagation_id =
|
||||
MessageByPropagationId::get_by_propagation_message_id(message_id, session).await?;
|
||||
Self::get_by_propagation_message_id(message_id, session).await
|
||||
} else {
|
||||
get_single_row_from_result(&result).await
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(message_by_propagation_id) = message_by_propagation_id {
|
||||
let result = session
|
||||
.query_unpaged(
|
||||
r#"
|
||||
SELECT
|
||||
call_id,
|
||||
message_id,
|
||||
author_id,
|
||||
propagation_message_id,
|
||||
data
|
||||
FROM
|
||||
payphone.messages
|
||||
WHERE
|
||||
call_id = ? AND message_id = ?
|
||||
LIMIT 1;
|
||||
"#,
|
||||
(call_id, u64_to_i64(message_by_propagation_id.message_id)),
|
||||
)
|
||||
.await
|
||||
.map_err(DatabaseError::QueryError)?
|
||||
.into_rows_result()
|
||||
.map_err(|_| DatabaseError::ResultNotRows)?;
|
||||
pub async fn get_by_propagation_message_id(
|
||||
propagation_message_id: u64,
|
||||
session: &Session,
|
||||
) -> Result<Option<Self>, DatabaseError> {
|
||||
let message_by_propagation_id =
|
||||
MessageByPropagationId::get_by_propagation_message_id(propagation_message_id, session)
|
||||
.await?;
|
||||
|
||||
return get_single_row_from_result(&result).await;
|
||||
}
|
||||
if let Some(message_by_propagation_id) = message_by_propagation_id {
|
||||
let result = session
|
||||
.query_unpaged(
|
||||
r#"
|
||||
SELECT
|
||||
call_id,
|
||||
message_id,
|
||||
author_id,
|
||||
propagation_message_id,
|
||||
data
|
||||
FROM
|
||||
payphone.messages
|
||||
WHERE
|
||||
call_id = ? AND message_id = ?
|
||||
LIMIT 1;
|
||||
"#,
|
||||
(
|
||||
u64_to_i64(message_by_propagation_id.call_id),
|
||||
u64_to_i64(message_by_propagation_id.message_id),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.map_err(DatabaseError::QueryError)?
|
||||
.into_rows_result()
|
||||
.map_err(|_| DatabaseError::ResultNotRows)?;
|
||||
|
||||
return get_single_row_from_result(&result).await;
|
||||
}
|
||||
|
||||
get_single_row_from_result(&result).await
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub async fn insert(&self, session: &Session) -> Result<(), DatabaseError> {
|
||||
|
||||
Reference in New Issue
Block a user