cleared up issues with the roles command
This commit is contained in:
parent
7265976948
commit
cdc06cb2ae
@ -21,8 +21,15 @@ SELECT id, name, prefix, volume
|
|||||||
.fetch_one(&db_pool)
|
.fetch_one(&db_pool)
|
||||||
.await;
|
.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>> {
|
pub async fn create_from_guild(guild: Guild, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error>> {
|
||||||
|
65
src/main.rs
65
src/main.rs
@ -132,25 +132,33 @@ async fn role_check(ctx: &Context, msg: &Message, _args: &mut Args) -> CheckResu
|
|||||||
let pool = ctx.data.read().await
|
let pool = ctx.data.read().await
|
||||||
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
|
.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 {
|
match guild_opt {
|
||||||
Some(member) => {
|
Some(guild) => {
|
||||||
|
let member_res = guild.member(*ctx, msg.author.id).await;
|
||||||
|
|
||||||
|
match member_res {
|
||||||
|
Ok(member) => {
|
||||||
let user_roles: String = member.roles
|
let user_roles: String = member.roles
|
||||||
.iter()
|
.iter()
|
||||||
.map(|r| (*r.as_u64()).to_string())
|
.map(|r| (*r.as_u64()).to_string())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
|
|
||||||
|
println!("{}", user_roles);
|
||||||
|
|
||||||
let guild_id = *msg.guild_id.unwrap().as_u64();
|
let guild_id = *msg.guild_id.unwrap().as_u64();
|
||||||
|
|
||||||
let role_res = sqlx::query!(
|
let role_res = sqlx::query!(
|
||||||
"
|
"
|
||||||
SELECT COUNT(1) as count
|
SELECT COUNT(1) as count
|
||||||
FROM roles
|
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;
|
.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 => {
|
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")]
|
#[command("roles")]
|
||||||
async fn set_allowed_roles(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
async fn set_allowed_roles(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||||
if args.len() == 0 {
|
let guild_id = *msg.guild_id.unwrap().as_u64();
|
||||||
msg.channel_id.say(&ctx, "Usage: `?roles <role mentions or anything else to disable>`. Current roles: ").await?;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
let pool = ctx.data.read().await
|
let pool = ctx.data.read().await
|
||||||
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
|
.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!(
|
sqlx::query!(
|
||||||
"
|
"
|
||||||
@ -717,12 +745,25 @@ INSERT INTO roles (guild_id, role)
|
|||||||
(?, ?)
|
(?, ?)
|
||||||
",
|
",
|
||||||
guild_id, role
|
guild_id, role
|
||||||
).execute(&pool).await?;
|
)
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.channel_id.say(&ctx, "Specified roles whitelisted").await?;
|
msg.channel_id.say(&ctx, "Specified roles whitelisted").await?;
|
||||||
}
|
}
|
||||||
else {
|
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?;
|
msg.channel_id.say(&ctx, "Role whitelisting disabled").await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user