roles are now stored individually on the guild.

This commit is contained in:
2021-09-03 11:33:24 +01:00
parent 7354646c89
commit b34ac64172
9 changed files with 222 additions and 344 deletions

View File

@ -147,7 +147,7 @@ pub async fn help(
CreateGenericResponse::new().embed(|e| {
e.title("Invalid Command")
.color(THEME_COLOR)
.description("Type `/help command` to view help about a command below:")
.description("Type `/help command` to view more about a command below:")
.fields(groups_iter)
}),
)
@ -173,7 +173,10 @@ pub async fn help(
CreateGenericResponse::new().embed(|e| {
e.title("Help")
.color(THEME_COLOR)
.description("Type `/help command` to view help about a command below:")
.description("**Welcome to SoundFX!**
To get started, upload a sound with `/upload`, or use `/search` and `/play` to look at some of the public sounds
Type `/help command` to view help about a command below:")
.fields(groups_iter)
}),
)

View File

@ -145,23 +145,21 @@ pub async fn change_prefix(
#[command("roles")]
#[required_permissions(Restricted)]
#[kind(Text)]
#[group("Settings")]
#[description("Change the roles allowed to use the bot")]
#[description("Change the role allowed to use the bot")]
#[arg(
name = "roles",
kind = "String",
description = "The role mentions to enlist",
name = "role",
kind = "Role",
description = "A role to allow to use the bot. Use @everyone to allow all server members",
required = true
)]
#[example("`/roles @everyone` - allow all server members to use the bot")]
#[example("`/roles @DJ` - allow only server members with the 'DJ' role to use the bot")]
pub async fn set_allowed_roles(
ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send),
args: Args,
) -> CommandResult {
let msg = invoke.msg().unwrap();
let guild_id = *msg.guild_id.unwrap().as_u64();
let pool = ctx
.data
.read()
@ -170,73 +168,19 @@ pub async fn set_allowed_roles(
.cloned()
.expect("Could not get SQLPool from data");
if args.is_empty() {
let roles = sqlx::query!(
"
SELECT role
FROM roles
WHERE guild_id = ?
",
guild_id
let role_id = args.named("role").unwrap().parse::<u64>().unwrap();
let guild_data = ctx.guild_data(invoke.guild_id().unwrap()).await.unwrap();
guild_data.write().await.allowed_role = Some(role_id);
guild_data.read().await.commit(pool).await?;
invoke
.respond(
ctx.http.clone(),
CreateGenericResponse::new().content(format!("Allowed role set to <@&{}>", role_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!(
"
DELETE FROM roles
WHERE guild_id = ?
",
guild_id
)
.execute(&pool)
.await?;
if msg.mention_roles.len() > 0 {
for role in msg.mention_roles.iter().map(|r| *r.as_u64()) {
sqlx::query!(
"
INSERT INTO roles (guild_id, role)
VALUES
(?, ?)
",
guild_id,
role
)
.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?;
}
}
Ok(())
}

View File

@ -32,9 +32,10 @@ use std::{
sync::Arc,
};
use crate::{guild_data::CtxGuildData, MySQL};
use crate::guild_data::CtxGuildData;
use serde_json::Value;
use serenity::builder::CreateComponents;
use serenity::model::id::RoleId;
type CommandFn = for<'fut> fn(
&'fut Context,
@ -74,10 +75,6 @@ impl Args {
Self { args }
}
pub fn is_empty(&self) -> bool {
self.args.is_empty()
}
pub fn named<D: ToString>(&self, name: D) -> Option<&String> {
let name = name.to_string();
@ -397,42 +394,14 @@ impl Command {
}
if self.required_permissions == PermissionLevel::Managed {
let pool = ctx
.data
.read()
.await
.get::<MySQL>()
.cloned()
.expect("Could not get SQLPool from data");
match ctx.guild_data(guild.id).await {
Ok(guild_data) => guild_data.read().await.allowed_role.map_or(true, |role| {
role == guild.id.0 || {
let role_id = RoleId(role);
match sqlx::query!(
"
SELECT role
FROM roles
WHERE guild_id = ?
",
guild.id.as_u64()
)
.fetch_all(&pool)
.await
{
Ok(rows) => {
let role_ids = member
.roles
.iter()
.map(|r| *r.as_u64())
.collect::<Vec<u64>>();
for row in rows {
if role_ids.contains(&row.role) || &row.role == guild.id.as_u64() {
return true;
}
member.roles.contains(&role_id)
}
false
}
Err(sqlx::Error::RowNotFound) => false,
}),
Err(e) => {
warn!("Unexpected error occurred querying roles: {:?}", e);
@ -716,7 +685,7 @@ impl RegexFramework {
let _ = interaction
.respond(
ctx.http.clone(),
CreateGenericResponse::new().content("You must either be an Admin or have a role specified in `?roles` to do this command")
CreateGenericResponse::new().content("You must either be an Admin or have a role specified by `/roles` to do this command")
)
.await;
} else if command.required_permissions == PermissionLevel::Restricted {

View File

@ -10,6 +10,7 @@ pub struct GuildData {
pub prefix: String,
pub volume: u8,
pub allow_greets: bool,
pub allowed_role: Option<u64>,
}
#[async_trait]
@ -68,7 +69,7 @@ impl GuildData {
let guild_data = sqlx::query_as_unchecked!(
GuildData,
"
SELECT id, prefix, volume, allow_greets
SELECT id, prefix, volume, allow_greets, allowed_role
FROM servers
WHERE id = ?
",
@ -102,22 +103,12 @@ INSERT INTO servers (id)
.execute(&db_pool)
.await?;
sqlx::query!(
"
INSERT IGNORE INTO roles (guild_id, role)
VALUES (?, ?)
",
guild_id.as_u64(),
guild_id.as_u64()
)
.execute(&db_pool)
.await?;
Ok(GuildData {
id: guild_id.as_u64().to_owned(),
prefix: String::from("?"),
volume: 100,
allow_greets: true,
allowed_role: None,
})
}
@ -131,13 +122,15 @@ UPDATE servers
SET
prefix = ?,
volume = ?,
allow_greets = ?
allow_greets = ?,
allowed_role = ?
WHERE
id = ?
",
self.prefix,
self.volume,
self.allow_greets,
self.allowed_role,
self.id
)
.execute(&db_pool)