create more usable helper functions
This commit is contained in:
@@ -5,7 +5,7 @@ use once_cell::sync::Lazy;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use hex_color::HexColor;
|
use hex_color::HexColor;
|
||||||
use poise::serenity_prelude::{colours, Color, EditRole, Permissions};
|
use poise::serenity_prelude::{colours, Color, EditRole};
|
||||||
|
|
||||||
static COLORS: Lazy<HashMap<&'static str, Color>> = Lazy::new(|| {
|
static COLORS: Lazy<HashMap<&'static str, Color>> = Lazy::new(|| {
|
||||||
HashMap::from([
|
HashMap::from([
|
||||||
@@ -78,20 +78,8 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col
|
|||||||
|
|
||||||
let user = ctx.author();
|
let user = ctx.author();
|
||||||
|
|
||||||
let mut tx = ctx.data().database.begin().await?;
|
let role = super::edit_role(ctx, user.id, guild, EditRole::new().colour(color), &ctx.data().database).await?;
|
||||||
|
common::no_ping_reply(&ctx, format!("{}'s color has been updated.", guild.role(ctx, role).await?)).await?;
|
||||||
if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? {
|
|
||||||
let role = guild.role(ctx, role).await?;
|
|
||||||
guild.edit_role(ctx, role.id, EditRole::new().colour(color)).await?;
|
|
||||||
common::no_ping_reply(&ctx, format!("{}'s color has been updated.", role)).await?;
|
|
||||||
} else {
|
|
||||||
let role = guild.create_role(ctx, EditRole::new().colour(color).name(user.name.clone()).permissions(Permissions::empty())).await?;
|
|
||||||
super::update_user_role(user.id, guild, role.id, &mut *tx).await?;
|
|
||||||
let member = guild.member(ctx, user).await?;
|
|
||||||
member.add_role(ctx, role.id).await?;
|
|
||||||
tx.commit().await?;
|
|
||||||
common::no_ping_reply(&ctx, format!("{} has been given the new role {}.", user, role)).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
use crate::common::{Context, Error};
|
use crate::common::{Context, Error};
|
||||||
use sqlx::{PgExecutor, Row};
|
use sqlx::{PgExecutor, Row};
|
||||||
use poise::serenity_prelude::{RoleId, UserId, GuildId};
|
use poise::serenity_prelude::{EditRole, GuildId, Permissions, RoleId, UserId};
|
||||||
|
|
||||||
mod register;
|
mod register;
|
||||||
mod whois;
|
mod whois;
|
||||||
@@ -26,6 +26,35 @@ pub async fn role(_ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Edit a user's personal role, creates it with some default values if it doesn't exist.
|
||||||
|
pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result<RoleId, Error>
|
||||||
|
where E: PgExecutor<'a> + Clone,
|
||||||
|
{
|
||||||
|
if let Some(role) = get_user_role(user, guild, db.clone()).await? {
|
||||||
|
guild.role(ctx, role).await?.edit(ctx, edit).await?;
|
||||||
|
Ok(role)
|
||||||
|
} else {
|
||||||
|
create_role(ctx, user, guild, edit, db).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result<RoleId, Error>
|
||||||
|
where E: PgExecutor<'a>,
|
||||||
|
{
|
||||||
|
let def = EditRole::new()
|
||||||
|
.name(user.to_user(ctx).await?.name)
|
||||||
|
.permissions(Permissions::empty());
|
||||||
|
|
||||||
|
let member = guild.member(ctx, user).await?;
|
||||||
|
|
||||||
|
let mut role = guild.create_role(ctx, def).await?;
|
||||||
|
role.edit(ctx, edit).await?;
|
||||||
|
member.add_role(ctx, &role).await?;
|
||||||
|
update_user_role(user, guild, role.id, db).await?;
|
||||||
|
|
||||||
|
Ok(role.id)
|
||||||
|
}
|
||||||
|
|
||||||
/// Remove a row concerning a user's self role from the database
|
/// Remove a row concerning a user's self role from the database
|
||||||
pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result<(), Error>
|
pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result<(), Error>
|
||||||
where E: PgExecutor<'a>,
|
where E: PgExecutor<'a>,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
use crate::common::{self, Context, Error};
|
use crate::common::{self, Context, Error};
|
||||||
|
|
||||||
use poise::serenity_prelude::{EditRole, Permissions};
|
use poise::serenity_prelude::EditRole;
|
||||||
|
|
||||||
/// Change the name of your personal role
|
/// Change the name of your personal role
|
||||||
#[poise::command(slash_command, prefix_command)]
|
#[poise::command(slash_command, prefix_command)]
|
||||||
@@ -15,20 +15,8 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
|
|||||||
|
|
||||||
let user = ctx.author();
|
let user = ctx.author();
|
||||||
|
|
||||||
let mut tx = ctx.data().database.begin().await?;
|
let role = super::edit_role(ctx, user.id, guild, EditRole::new().name(name), &ctx.data().database).await?;
|
||||||
|
common::no_ping_reply(&ctx, format!("{} has been updated.", guild.role(ctx, role).await?)).await?;
|
||||||
if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? {
|
|
||||||
let role = guild.role(ctx, role).await?;
|
|
||||||
guild.edit_role(ctx, role.id, EditRole::new().name(name)).await?;
|
|
||||||
common::no_ping_reply(&ctx, format!("{} has been updated.", role)).await?;
|
|
||||||
} else {
|
|
||||||
let role = guild.create_role(ctx, EditRole::new().name(name).permissions(Permissions::empty())).await?;
|
|
||||||
super::update_user_role(user.id, guild, role.id, &mut *tx).await?;
|
|
||||||
let member = guild.member(ctx, user).await?;
|
|
||||||
member.add_role(ctx, role.id).await?;
|
|
||||||
tx.commit().await?;
|
|
||||||
common::no_ping_reply(&ctx, format!("{} has been given the new role {}.", user, role)).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user