Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
p0rtL6 committed Mar 17, 2024
1 parent 5b6f1ab commit 7727025
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blackcube-rs"
version = "0.2.3"
version = "0.2.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
13 changes: 13 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ impl HasAuth for Member {
Ok(self.roles.contains(&config.server.auth_role_id))
}
}
impl HasAuth for User {
async fn has_auth(&self, ctx: &Context) -> anyhow::Result<bool> {
let data = ctx.data.read().await;
let config = data.get::<Config>().context("Could not get config")?;
Ok(self
.has_role(
&ctx.http,
config.server.guild_id,
config.server.auth_role_id,
)
.await?)
}
}

pub trait IsBlacklisted {
async fn is_blacklisted(&self, ctx: &Context) -> anyhow::Result<bool>;
Expand Down
9 changes: 5 additions & 4 deletions src/handlers/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub async fn handle_component_interaction(
.message
.embeds
.first()
.context("Could not get first embed")?;
.context("Could not get first embed")?
.clone();

let embed_link = embed.url.clone();

Expand Down Expand Up @@ -104,7 +105,7 @@ pub async fn handle_component_interaction(

drop(data);

delete_user_request(&ctx, uid, &embed_link.context("could not get embed link")?)
delete_user_request(&ctx, &embed)
.await
.context("Could not delete original request")?;
} else {
Expand Down Expand Up @@ -134,7 +135,7 @@ pub async fn handle_component_interaction(
)
.await
.context("Could not edit request message")?;
delete_user_request(&ctx, uid, &embed_link.context("could not get embed link")?)
delete_user_request(&ctx, &embed)
.await
.context("Could not delete original request")?;
} else {
Expand Down Expand Up @@ -165,7 +166,7 @@ pub async fn handle_component_interaction(
.await
.context("Could not edit request message")?;

delete_user_request(&ctx, uid, &embed_link.context("could not get embed link")?)
delete_user_request(&ctx, &embed)
.await
.context("Could not delete original request")?;
} else {
Expand Down
77 changes: 54 additions & 23 deletions src/handlers/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,57 @@ use anyhow::{bail, Context as AnyhowContext};
use serenity::{client::Context, model::channel::Message};

use crate::{
auth::IsBlacklisted,
auth::{HasAuth, IsBlacklisted},
responses::{create_request_log_message, edit_request},
structs::{Config, PendingRequestMidStore, PendingRequestUidStore},
};

pub async fn handle_user_request(ctx: Context, msg: Message) -> anyhow::Result<()> {
if msg.author.is_blacklisted(&ctx).await? {
msg.delete(&ctx.http).await?;
bail!("User is blacklisted");
};

let message_attachment = msg.attachments.first();

// Check to see if attachment exists

if message_attachment.is_none() && !msg.author.has_auth(&ctx).await? {
msg.delete(&ctx.http).await?;
bail!("No message attachment")
}

let message_attachment = message_attachment.context("No message attachment")?; // Better way to do this?

// Check to make sure image is under size limit

if message_attachment.size > 10000000 {
msg.delete(&ctx.http).await?;
bail!("File size too large");
}

// Check for valid image type

let data = ctx.data.read().await;
let config = data.get::<Config>().context("Could not get config")?;

let attachment_content_type = &message_attachment
.content_type
.as_ref()
.context("Could not get content-type")?[6..];

if !config
.settings
.image_types
.contains(&attachment_content_type.to_string())
&& !msg.author.has_auth(&ctx).await?
{
msg.delete(&ctx.http).await?;
bail!("Invalid file type");
}

drop(data);

// check if user has an existing request, if so, cancel it first

let mut data = ctx.data.write().await;
Expand Down Expand Up @@ -59,32 +100,22 @@ pub async fn handle_user_request(ctx: Context, msg: Message) -> anyhow::Result<(
None => {}
}

// Get message attachment and create request
let message_attachment = msg.attachments.first().context("No message attachment")?;
let attachment_content_type = &message_attachment
.content_type
.as_ref()
.context("Could not get content-type")?[6..];

if config.settings.image_types.contains(&attachment_content_type.to_string())
{
drop(data);
drop(data);

let created_message_id = create_request_log_message(&ctx, &msg).await?; // Add error handling here (log to channel?)
let created_message_id = create_request_log_message(&ctx, &msg).await?; // Add error handling here (log to channel?)

// Add new request to local store
// Add new request to local store

let mut data = ctx.data.write().await;
let pending_request_store = data
.get_mut::<PendingRequestUidStore>()
.context("Could not get pending request store")?;
pending_request_store.insert(msg.author.id, created_message_id);
let mut data = ctx.data.write().await;
let pending_request_store = data
.get_mut::<PendingRequestUidStore>()
.context("Could not get pending request store")?;
pending_request_store.insert(msg.author.id, created_message_id);

let pending_request_mid_store = data
.get_mut::<PendingRequestMidStore>()
.context("Could not get pending request store")?;
pending_request_mid_store.insert(msg.id, created_message_id);
}
let pending_request_mid_store = data
.get_mut::<PendingRequestMidStore>()
.context("Could not get pending request store")?;
pending_request_mid_store.insert(msg.id, created_message_id);

Ok(())
}
16 changes: 13 additions & 3 deletions src/imgur.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::bail;
use anyhow::Context as AnyhowContext;
use serenity::prelude::Context;

Expand All @@ -20,8 +21,17 @@ pub async fn upload_image_to_imgur(
.form(&[("image", image_url)]);

let response = request.send().await?;
let raw_json_response = response.text().await?;
let json = serde_json::from_str::<ImgurResponse>(&raw_json_response)?;

Ok(json)
if response.status().is_success() {
let raw_json_response = response.text().await?;
let json = serde_json::from_str::<ImgurResponse>(&raw_json_response)?;

Ok(json)
} else {
bail!(
"Error Uploading to Imgur: {:?} | {}",
response.headers().clone(),
response.text().await?
);
}
}
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use serenity::{
prelude::GatewayIntents,
};

use crate::structs::HttpClient;
use crate::{responses::delete_user_request, structs::HttpClient};
struct Handler;

#[async_trait]
Expand Down Expand Up @@ -113,6 +113,22 @@ impl EventHandler for Handler {
if result.is_err() {
println!("{:?}", result);

let embed = component_interaction.message.embeds.first();

match embed {
Some(embed) => {
let delete_result = delete_user_request(&ctx, embed).await;

match delete_result {
Ok(()) => {}
Err(err) => {
println!("error deleting user request: {}", err);
}
}
}
None => {}
}

let result = edit_request(
&ctx,
&mut component_interaction.message,
Expand Down
24 changes: 18 additions & 6 deletions src/responses.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Context as AnyhowContext;
use serenity::{
all::{ButtonStyle, InteractionResponseFlags, MessageId, UserId},
all::{ButtonStyle, Embed, InteractionResponseFlags, MessageId, UserId},
builder::{
CreateActionRow, CreateButton, CreateEmbed, CreateInteractionResponse,
CreateInteractionResponseMessage, CreateMessage, EditMessage,
Expand Down Expand Up @@ -140,11 +140,23 @@ pub async fn create_request_log_message(ctx: &Context, msg: &Message) -> anyhow:
Ok(created_message.id)
}

pub async fn delete_user_request(
ctx: &Context,
uid: String,
embed_link: &String,
) -> anyhow::Result<()> {
pub async fn delete_user_request(ctx: &Context, embed: &Embed) -> anyhow::Result<()> {
let embed_link = embed.url.clone().context("could not get embed link")?;

let mut uid: Option<String> = None;

for field in &embed.fields {
match field.name.as_str() {
"UID" => {
uid = Some(field.value.clone());
break;
}
_ => {}
}
}

let uid: String = uid.context("Could not parse uid from embed")?;

let mut data = ctx.data.write().await;
let pending_request_store = data
.get_mut::<PendingRequestUidStore>()
Expand Down
3 changes: 2 additions & 1 deletion src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use serde::{Deserialize, Serialize};

pub use serenity::model::id::{ChannelId, RoleId};
use serenity::{
all::{MessageId, UserId},
all::{GuildId, MessageId, UserId},
prelude::TypeMapKey,
};

Expand Down Expand Up @@ -101,6 +101,7 @@ pub struct Database {

#[derive(Debug, Serialize, Deserialize)]
pub struct Server {
pub guild_id: GuildId,
pub request_channel_id: ChannelId,
pub log_channel_id: ChannelId,
pub command_channel_id: ChannelId,
Expand Down

0 comments on commit 7727025

Please sign in to comment.