Skip to content

Commit 7b21982

Browse files
committed
feat: images rotate randomly sometimes. Updated blown up text.
1 parent 225971e commit 7b21982

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/commands/image.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::bot_types::{_Context as Context, Error};
22
use image;
33
use poise::serenity_prelude as serenity;
4+
use serenity::all::Message;
5+
use serenity::builder::CreateMessage;
46
use std::path::{Path, PathBuf};
57
use tokio::io::AsyncWriteExt;
68

@@ -11,6 +13,7 @@ const ROTATE_FOLDER: &str = "rotated";
1113
#[poise::command(prefix_command)]
1214
pub async fn rotate(ctx: Context<'_>) -> Result<(), Error> {
1315
let msg = ctx.channel_id().message(&ctx.http(), ctx.id()).await?;
16+
// rotate_image(msg.clone()).await;
1417

1518
if msg.referenced_message.is_none() {
1619
ctx.reply("Nothing to rotate...")
@@ -99,6 +102,10 @@ async fn get_and_save_picture(link: &str, file_path: &Path) -> Result<bool, Erro
99102
if !response.status().is_success() {
100103
return Err(Error::from(response.error_for_status().unwrap_err()));
101104
}
105+
const MAX_SIZE: u64 = 50_000_000;
106+
if response.content_length().is_none_or(|len| len > MAX_SIZE) {
107+
return Err(Error::from("File to large."));
108+
}
102109
let mut file = tokio::fs::File::create(file_path).await?;
103110
let content = response.bytes().await?;
104111
file.write_all(&content).await?;
@@ -114,3 +121,41 @@ async fn rotate_image_and_save(file_path: &Path, save_path: &Path) {
114121
let rot_img = img.rotate180();
115122
rot_img.save(save_path).expect("Error Saving Image");
116123
}
124+
125+
pub async fn rotate_image_directly(
126+
http: &serenity::Http,
127+
channel_id: serenity::ChannelId,
128+
msg: &Message,
129+
) -> Result<(), Error> {
130+
if !matches!(
131+
msg.attachments
132+
.first()
133+
.and_then(|a| a.content_type.as_deref()),
134+
Some("image/png" | "image/jpeg")
135+
) {
136+
return Err(Error::from("Not a valid image"));
137+
}
138+
check_folders();
139+
let attachments_link = msg.attachments.first().unwrap().proxy_url.clone();
140+
let content_type = msg
141+
.attachments
142+
.first()
143+
.unwrap()
144+
.content_type
145+
.clone()
146+
.unwrap();
147+
148+
let file_name = generate_file_name(msg.id.to_string(), &content_type);
149+
150+
let original_file_path = PathBuf::from(ROOT).join(ORIGINAL_FOLDER).join(&file_name);
151+
get_and_save_picture(&attachments_link, &original_file_path).await?;
152+
153+
let rotate_file_path = PathBuf::from(ROOT).join(ROTATE_FOLDER).join(&file_name);
154+
rotate_image_and_save(&original_file_path, &rotate_file_path).await;
155+
156+
let attachment = serenity::CreateAttachment::path(&rotate_file_path).await?;
157+
let builder = CreateMessage::new().add_file(attachment);
158+
channel_id.send_message(http, builder).await?;
159+
160+
Ok(())
161+
}

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl EventHandler for Handler {
6363
msg.reply(
6464
&_ctx.http,
6565
format!(
66-
"{} You're our lucky loser! See you in 10 minutes. :3 {}/{}",
66+
"{} oh nyo, >w< wooks wike somebwody got bwown up {}/{}",
6767
get_emoji("winner"),
6868
current_number_of_bombs,
6969
MAX_BOMB_RANGE,
@@ -72,6 +72,17 @@ impl EventHandler for Handler {
7272
.await
7373
.unwrap();
7474
}
75+
76+
// Rotate the image sometimes.
77+
if !msg.attachments.is_empty() {
78+
let mut _rng = rand::rng().random_range(0..=1000);
79+
let lucky_numbers = [5];
80+
if lucky_numbers.contains(&_rng) {
81+
rotate_image_directly(&_ctx.http, msg.channel_id, &msg)
82+
.await
83+
.expect("Error rotating image");
84+
}
85+
}
7586
}
7687

7788
async fn reaction_add(&self, _ctx: Context, _add_reaction: Reaction) {

0 commit comments

Comments
 (0)