Skip to content

Commit

Permalink
Notify guild owners if album contains image that is too large.
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
NotNorom committed Sep 17, 2023
1 parent f7bbb50 commit 4cb8fa3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 49 deletions.
101 changes: 56 additions & 45 deletions src/banner_changer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,57 +172,68 @@ impl ChangerError {
imgurs_err => error!("unhandled imgurs error: {imgurs_err}"),
},
},
Error::SetBanner(error) => match error {
SetBannerError::Transport(err) => {
warn!("guild_id={guild_id}: {err}")
}
SetBannerError::DiscordApi(discord_err) => match discord_err {
SerenityError::Http(http_err) => match http_err.as_ref() {
SerenityHttpError::UnsuccessfulRequest(error_response) => match error_response
.status_code
{
StatusCode::FORBIDDEN => {
// the bot does not have permissions to change the banner.
// remove guild from queue
let _ = repeater_handle.remove(guild_id).await;
db.delete::<GuildSchedule>(self.schedule.guild_id().0).await?;
warn!("Missing permissions to change banner for {guild_id}. Unscheduling.");
return Ok(ScheduleAction::Abort);
}
StatusCode::NOT_FOUND => {
let _ = repeater_handle.remove(guild_id).await;
db.delete::<GuildSchedule>(self.schedule.guild_id().0).await?;
warn!("Guild does not exist: {guild_id}. Unscheduling.");
return Ok(ScheduleAction::Abort);
}
StatusCode::GATEWAY_TIMEOUT => {
warn!("Gateway timed out. Retrying once.");
return Ok(ScheduleAction::Retry);
Error::SetBanner(error) => {
match error {
SetBannerError::Transport(err) => {
warn!("guild_id={guild_id}: {err}")
}
SetBannerError::DiscordApi(discord_err) => match discord_err {
SerenityError::Http(http_err) => match http_err.as_ref() {
SerenityHttpError::UnsuccessfulRequest(error_response) => {
match error_response.status_code {
StatusCode::FORBIDDEN => {
// the bot does not have permissions to change the banner.
// remove guild from queue
let _ = repeater_handle.remove(guild_id).await;
db.delete::<GuildSchedule>(self.schedule.guild_id().0).await?;
warn!("Missing permissions to change banner for {guild_id}. Unscheduling.");
return Ok(ScheduleAction::Abort);
}
StatusCode::NOT_FOUND => {
let _ = repeater_handle.remove(guild_id).await;
db.delete::<GuildSchedule>(self.schedule.guild_id().0).await?;
warn!("Guild does not exist: {guild_id}. Unscheduling.");
return Ok(ScheduleAction::Abort);
}
StatusCode::GATEWAY_TIMEOUT => {
warn!("Gateway timed out. Retrying once.");
return Ok(ScheduleAction::Retry);
}
_ => error!("unsuccessful http request: {error_response:?}"),
}
}
_ => error!("unsuccessful http request: {error_response:?}"),
http_err => error!("unhandled http error in set_banner: {http_err:?}"),
},
http_err => error!("unhandled http error in set_banner: {http_err:?}"),
serenity_err => error!("unhandled serenity error in set_banner: {serenity_err:?}"),
},
serenity_err => error!("unhandled serenity error in set_banner: {serenity_err:?}"),
},
SetBannerError::CouldNotPickAUrl => warn!("guild_id={guild_id}: 'Could not pick a url'"),
SetBannerError::CouldNotDeterminFileExtension => {
warn!("guild_id={guild_id}: 'Could not determine file extenstion'")
}
SetBannerError::MissingBannerFeature => {
let _ = repeater_handle.remove(guild_id).await;
db.delete::<GuildSchedule>(self.schedule.guild_id().0).await?;
SetBannerError::CouldNotPickAUrl => warn!("guild_id={guild_id}: 'Could not pick a url'"),
SetBannerError::CouldNotDeterminFileExtension => {
warn!("guild_id={guild_id}: 'Could not determine file extenstion'")
}
SetBannerError::MissingBannerFeature => {
let _ = repeater_handle.remove(guild_id).await;
db.delete::<GuildSchedule>(self.schedule.guild_id().0).await?;

let partial_guild = guild_id.to_partial_guild(&ctx.http).await?;
let guild_owner = partial_guild.owner_id;
info!("Letting owner={guild_owner} of guild={guild_id} know about the missing banner feature");
let partial_guild = guild_id.to_partial_guild(&ctx.http).await?;
let guild_owner = partial_guild.owner_id;
info!("Letting owner={guild_owner} of guild={guild_id} know about the missing banner feature");

dm_user(&ctx, guild_owner, &"Server has lost the required boost level. Stopping schedule. You can restart the bot after gaining the required boost level.").await?;
},
SetBannerError::ImageIsEmpty => {
warn!("guild_id={guild_id} with album={} has downloaded an image with 0 bytes", self.schedule.album());
dm_user(&ctx, guild_owner, &"Server has lost the required boost level. Stopping schedule. You can restart the bot after gaining the required boost level.").await?;
}
SetBannerError::ImageIsEmpty(url) => {
warn!("guild_id={guild_id} with album={} has downloaded an image with 0 bytes. url={url}", self.schedule.album());
}
SetBannerError::ImageIsTooBig(url) => {
warn!("guild_id={guild_id} with album={} has downloaded an image that is too big. url={url}", self.schedule.album());

let partial_guild = guild_id.to_partial_guild(&ctx.http).await?;
let guild_owner = partial_guild.owner_id;
info!("Letting owner={guild_owner} of guild={guild_id} know about an image that is too big");

dm_user(&ctx, guild_owner, &format!("The album you've set contains an image that is too big for discord. Maximum size is 10mb. The image is: {url}")).await?;
}
}
},
}
err => {
error!("unhandled bot error: {err:?}");
}
Expand Down
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ pub const DEFAULT_INTERVAL: u64 = 30;

/// Maximum amount of minutes between banner changes
pub const MAXIMUM_INTERVAL: u64 = 60 * 48; // 48h

/// Maximum image size in bytes for uploads to discord
pub const MAXIMUM_IMAGE_SIZE: usize = 1024 * 1024 * 10; // 10mb
14 changes: 10 additions & 4 deletions src/guild_id_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use reqwest::Client;
use tracing::{debug, info, instrument};
use url::Url;

use crate::constants::MAXIMUM_IMAGE_SIZE;

#[derive(Debug, thiserror::Error)]
pub enum SetBannerError {
#[error(transparent)]
Expand All @@ -24,7 +26,9 @@ pub enum SetBannerError {
#[error("Missing 'banner' feature")]
MissingBannerFeature,
#[error("Image is empty")]
ImageIsEmpty
ImageIsEmpty(Url),
#[error("Image is to big")]
ImageIsTooBig(Url),
}

#[async_trait]
Expand Down Expand Up @@ -90,9 +94,11 @@ impl RandomBanner for GuildId {
let amount_of_bytes = image_bytes.len();
debug!("Amount of image bytes downloaded: {}", amount_of_bytes);

if amount_of_bytes == 0 {
return Err(SetBannerError::ImageIsEmpty);
}
match amount_of_bytes {
0 => return Err(SetBannerError::ImageIsEmpty(url.clone())),
MAXIMUM_IMAGE_SIZE.. => return Err(SetBannerError::ImageIsTooBig(url.to_owned())), // 10mb
_ => {}
};

let b64 = base64::engine::general_purpose::STANDARD.encode(&image_bytes);

Expand Down

0 comments on commit 4cb8fa3

Please sign in to comment.