cleared up issues with the roles command

This commit is contained in:
jude-lafitteIII 2020-05-20 19:08:59 +01:00
parent 7265976948
commit cdc06cb2ae
2 changed files with 83 additions and 35 deletions

View File

@ -21,8 +21,15 @@ SELECT id, name, prefix, volume
.fetch_one(&db_pool)
.await;
match guild {
Ok(g) => Some(g),
guild.ok()
Err(e) => {
println!("Guild query issue: {:?}", e);
None
}
}
}
pub async fn create_from_guild(guild: Guild, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error>> {

View File

@ -132,25 +132,33 @@ async fn role_check(ctx: &Context, msg: &Message, _args: &mut Args) -> CheckResu
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let user_member = msg.member(&ctx).await;
let guild_opt = msg.guild(&ctx).await;
match user_member {
Some(member) => {
match guild_opt {
Some(guild) => {
let member_res = guild.member(*ctx, msg.author.id).await;
match member_res {
Ok(member) => {
let user_roles: String = member.roles
.iter()
.map(|r| (*r.as_u64()).to_string())
.collect::<Vec<String>>()
.join(", ");
println!("{}", user_roles);
let guild_id = *msg.guild_id.unwrap().as_u64();
let role_res = sqlx::query!(
"
SELECT COUNT(1) as count
FROM roles
WHERE guild_id = ? AND role IN (?)
WHERE
(guild_id = ? AND role IN (?)) OR
(role = ?)
",
guild_id, user_roles
guild_id, user_roles, guild_id
)
.fetch_one(&pool).await;
@ -170,8 +178,14 @@ SELECT COUNT(1) as count
}
}
Err(_) => {
CheckResult::Failure(Reason::User("Unexpected error looking up user roles".to_string()))
}
}
}
None => {
CheckResult::Failure(Reason::User("User has not got a sufficient role".to_string()))
CheckResult::Failure(Reason::User("Unexpected error looking up guild".to_string()))
}
}
}
@ -691,14 +705,28 @@ async fn upload_new_sound(ctx: &Context, msg: &Message, args: Args) -> CommandRe
#[command("roles")]
async fn set_allowed_roles(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if args.len() == 0 {
msg.channel_id.say(&ctx, "Usage: `?roles <role mentions or anything else to disable>`. Current roles: ").await?;
}
else {
let guild_id = *msg.guild_id.unwrap().as_u64();
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let guild_id = *msg.guild_id.unwrap().as_u64();
if args.len() == 0 {
let roles = sqlx::query!(
"
SELECT role
FROM roles
WHERE guild_id = ?
",
guild_id
)
.fetch_all(&pool)
.await?;
let all_roles = roles.iter().map(|i| format!("<@&{}>", i.role.to_string())).collect::<Vec<String>>().join(", ");
msg.channel_id.say(&ctx, format!("Usage: `?roles <role mentions or anything else to disable>`. Current roles: {}", all_roles)).await?;
}
else {
sqlx::query!(
"
@ -717,12 +745,25 @@ INSERT INTO roles (guild_id, role)
(?, ?)
",
guild_id, role
).execute(&pool).await?;
)
.execute(&pool)
.await?;
}
msg.channel_id.say(&ctx, "Specified roles whitelisted").await?;
}
else {
sqlx::query!(
"
INSERT INTO roles (guild_id, role)
VALUES
(?, ?)
",
guild_id, guild_id
)
.execute(&pool)
.await?;
msg.channel_id.say(&ctx, "Role whitelisting disabled").await?;
}
}