optimized packing slightly. restrict interactions

This commit is contained in:
jellywx 2021-09-16 15:42:50 +01:00
parent 3c1eeed92f
commit b2207e308a
7 changed files with 76 additions and 76 deletions

12
Cargo.lock generated
View File

@ -1237,6 +1237,7 @@ dependencies = [
"rmp-serde", "rmp-serde",
"serde", "serde",
"serde_json", "serde_json",
"serde_repr",
"serenity", "serenity",
"sqlx", "sqlx",
"tokio", "tokio",
@ -1446,6 +1447,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_repr"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98d0516900518c29efa217c298fa1f4e6c6ffc85ae29fd7f4ee48f176e1a9ed5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.0" version = "0.7.0"

View File

@ -19,6 +19,7 @@ lazy_static = "1.4"
num-integer = "0.1" num-integer = "0.1"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_repr = "0.1"
rmp-serde = "0.15" rmp-serde = "0.15"
rand = "0.7" rand = "0.7"
levenshtein = "1.0" levenshtein = "1.0"

View File

@ -239,7 +239,7 @@ async fn restrict(ctx: &Context, invoke: &(dyn CommandInvoke + Send + Sync), arg
if let Some(OptionValue::Role(role)) = args.get("role") { if let Some(OptionValue::Role(role)) = args.get("role") {
let restricted_commands = let restricted_commands =
sqlx::query!("SELECT command FROM command_restrictions WHERE role_id = ?", role.0) sqlx::query!("SELECT command FROM command_restrictions WHERE role_id = (SELECT id FROM roles WHERE role = ?)", role.0)
.fetch_all(&pool) .fetch_all(&pool)
.await .await
.unwrap() .unwrap()
@ -256,7 +256,11 @@ async fn restrict(ctx: &Context, invoke: &(dyn CommandInvoke + Send + Sync), arg
let len = restrictable_commands.len(); let len = restrictable_commands.len();
let restrict_pl = ComponentDataModel::Restrict(Restrict { role_id: *role }); let restrict_pl = ComponentDataModel::Restrict(Restrict {
role_id: *role,
author_id: invoke.author_id(),
guild_id: invoke.guild_id().unwrap(),
});
invoke invoke
.respond( .respond(

View File

@ -405,22 +405,13 @@ async fn look(ctx: &Context, invoke: &(dyn CommandInvoke + Send + Sync), args: C
} }
} }
/*
#[command("del")] #[command("del")]
#[description("Delete reminders")] #[description("Delete reminders")]
#[permission_level(Managed)] #[required_permissions(Managed)]
async fn delete(ctx: &Context, invoke: &(dyn CommandInvoke + Send + Sync)) { async fn delete(ctx: &Context, invoke: &(dyn CommandInvoke + Send + Sync)) {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap(); let reminders = Reminder::from_guild(ctx, invoke.guild_id(), invoke.author_id()).await;
let _ = msg.channel_id.say(&ctx, lm.get(&user_data.language, "del/listing")).await;
let mut reminder_ids: Vec<u32> = vec![];
let reminders = Reminder::from_guild(ctx, msg.guild_id, msg.author.id).await;
let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| { let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| {
reminder_ids.push(reminder.id);
format!( format!(
"**{}**: '{}' *<#{}>* at <t:{}>", "**{}**: '{}' *<#{}>* at <t:{}>",
count + 1, count + 1,
@ -430,65 +421,8 @@ async fn delete(ctx: &Context, invoke: &(dyn CommandInvoke + Send + Sync)) {
) )
}); });
let _ = msg.channel_id.say_lines(&ctx, enumerated_reminders).await; let _ = invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("test")).await;
let _ = msg.channel_id.say(&ctx, lm.get(&user_data.language, "del/listed")).await;
let reply =
msg.channel_id.await_reply(&ctx).author_id(msg.author.id).channel_id(msg.channel_id).await;
if let Some(content) = reply.map(|m| m.content.replace(",", " ")) {
let parts = content.split(' ').filter(|i| !i.is_empty()).collect::<Vec<&str>>();
let valid_parts = parts
.iter()
.filter_map(|i| {
i.parse::<usize>()
.ok()
.filter(|val| val > &0)
.map(|val| reminder_ids.get(val - 1))
.flatten()
})
.map(|item| item.to_string())
.collect::<Vec<String>>();
if parts.len() == valid_parts.len() {
let joined = valid_parts.join(",");
let count_row = sqlx::query!(
"
SELECT COUNT(1) AS count FROM reminders WHERE FIND_IN_SET(id, ?)
",
joined
)
.fetch_one(&pool)
.await
.unwrap();
sqlx::query!(
"
DELETE FROM reminders WHERE FIND_IN_SET(id, ?)
",
joined
)
.execute(&pool)
.await
.unwrap();
let content = lm.get(&user_data.language, "del/count").replacen(
"{}",
&count_row.count.to_string(),
1,
);
let _ = msg.channel_id.say(&ctx, content).await;
} else {
let content = lm.get(&user_data.language, "del/count").replacen("{}", "0", 1);
let _ = msg.channel_id.say(&ctx, content).await;
}
}
} }
*/
#[command("timer")] #[command("timer")]
#[description("Manage timers")] #[description("Manage timers")]

View File

@ -3,16 +3,18 @@ use std::io::Cursor;
use chrono_tz::Tz; use chrono_tz::Tz;
use rmp_serde::Serializer; use rmp_serde::Serializer;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_repr::*;
use serenity::{ use serenity::{
builder::CreateEmbed, builder::CreateEmbed,
client::Context, client::Context,
model::{ model::{
channel::Channel, channel::Channel,
id::{ChannelId, RoleId}, id::{ChannelId, GuildId, RoleId, UserId},
interactions::{ interactions::{
message_component::{ButtonStyle, MessageComponentInteraction}, message_component::{ButtonStyle, MessageComponentInteraction},
InteractionResponseType, InteractionResponseType,
}, },
prelude::InteractionApplicationCommandCallbackDataFlags,
}, },
}; };
@ -22,10 +24,12 @@ use crate::{
reminder::{look_flags::LookFlags, Reminder}, reminder::{look_flags::LookFlags, Reminder},
user_data::UserData, user_data::UserData,
}, },
SQLPool,
}; };
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
#[repr(u8)]
pub enum ComponentDataModel { pub enum ComponentDataModel {
Restrict(Restrict), Restrict(Restrict),
LookPager(LookPager), LookPager(LookPager),
@ -47,7 +51,42 @@ impl ComponentDataModel {
pub async fn act(&self, ctx: &Context, component: MessageComponentInteraction) { pub async fn act(&self, ctx: &Context, component: MessageComponentInteraction) {
match self { match self {
ComponentDataModel::Restrict(restrict) => { ComponentDataModel::Restrict(restrict) => {
println!("{:?}", component.data.values); if restrict.author_id == component.user.id {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
let _ = sqlx::query!(
"
INSERT IGNORE INTO roles (role, name, guild_id) VALUES (?, \"Role\", (SELECT id FROM guilds WHERE guild = ?))
",
restrict.role_id.0,
restrict.guild_id.0
)
.execute(&pool)
.await;
for command in &component.data.values {
let _ = sqlx::query!(
"INSERT INTO command_restrictions (role_id, command) VALUES ((SELECT id FROM roles WHERE role = ?), ?)",
restrict.role_id.0,
command
)
.execute(&pool)
.await;
}
component
.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|response| response
.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL)
.content("Role permissions updated")
)
})
.await
.unwrap();
} else {
// tell them they cant do this
}
} }
ComponentDataModel::LookPager(pager) => { ComponentDataModel::LookPager(pager) => {
let flags = pager.flags; let flags = pager.flags;
@ -184,9 +223,12 @@ impl ComponentDataModel {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Restrict { pub struct Restrict {
pub role_id: RoleId, pub role_id: RoleId,
pub author_id: UserId,
pub guild_id: GuildId,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize_repr, Deserialize_repr, Debug)]
#[repr(u8)]
pub enum PageAction { pub enum PageAction {
First = 0, First = 0,
Previous = 1, Previous = 1,

View File

@ -404,7 +404,12 @@ impl From<ApplicationCommandInteraction> for CommandOptions {
cmd_opts.options.insert( cmd_opts.options.insert(
option.name, option.name,
OptionValue::Role(RoleId( OptionValue::Role(RoleId(
option.value.map(|m| m.as_u64()).flatten().unwrap(), option
.value
.map(|m| m.as_str().map(|s| s.parse::<u64>().ok()))
.flatten()
.flatten()
.unwrap(),
)), )),
); );
} }

View File

@ -1,9 +1,11 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_repr::*;
use serenity::model::id::ChannelId; use serenity::model::id::ChannelId;
use crate::consts::REGEX_CHANNEL; use crate::consts::REGEX_CHANNEL;
#[derive(Serialize, Deserialize, Copy, Clone, Debug)] #[derive(Serialize_repr, Deserialize_repr, Copy, Clone, Debug)]
#[repr(u8)]
pub enum TimeDisplayType { pub enum TimeDisplayType {
Absolute = 0, Absolute = 0,
Relative = 1, Relative = 1,