diff --git a/README.md b/README.md index 4ed717b..e1de61a 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,4 @@ __Other Variables__ * `PYTHON_LOCATION` - default `venv/bin/python3`. Can be changed if your Python executable is located somewhere else * `LOCAL_LANGUAGE` - default `EN`. Specifies the string set to fall back to if a string cannot be found (and to be used with new users) * `THEME_COLOR` - default `8fb677`. Specifies the hex value of the color to use on info message embeds +* `WEBHOOK_AVATAR` - default `None`, accepts the path to an image file to be used as the avatar when creating webhooks. **IMPORTANT: image file must be 128x128 or smaller in size** diff --git a/assets/webhook.jpg b/assets/webhook.jpg new file mode 100644 index 0000000..ee26098 Binary files /dev/null and b/assets/webhook.jpg differ diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 221cad1..f673bb0 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -6,12 +6,15 @@ use serenity::{ cache::Cache, client::Context, framework::standard::CommandResult, - http::CacheHttp, + http::{AttachmentType, CacheHttp}, model::{ + channel::GuildChannel, channel::Message, id::{ChannelId, GuildId, UserId}, misc::Mentionable, + webhook::Webhook, }, + Result as SerenityResult, }; use tokio::process::Command; @@ -20,7 +23,7 @@ use crate::{ check_subscription_on_message, consts::{ CHARACTERS, DAY, HOUR, LOCAL_TIMEZONE, MAX_TIME, MINUTE, MIN_INTERVAL, PYTHON_LOCATION, - REGEX_CHANNEL, REGEX_CHANNEL_USER, + REGEX_CHANNEL, REGEX_CHANNEL_USER, WEBHOOK_AVATAR, }, framework::SendIterator, models::{ChannelData, GuildData, Timer, UserData}, @@ -43,14 +46,14 @@ use num_integer::Integer; use std::{ convert::TryInto, default::Default, + fmt::Display, + path::Path, string::ToString, time::{SystemTime, UNIX_EPOCH}, }; use regex::Regex; -use serde_json::json; - fn shorthand_displacement(seconds: u64) -> String { let (hours, seconds) = seconds.div_rem(&HOUR); let (minutes, seconds) = seconds.div_rem(&MINUTE); @@ -77,6 +80,21 @@ fn longhand_displacement(seconds: u64) -> String { sections.join(", ") } +async fn create_webhook( + ctx: impl CacheHttp, + channel: GuildChannel, + name: impl Display, + avatar: Option, +) -> SerenityResult { + if let Some(path) = avatar { + channel + .create_webhook_with_avatar(ctx.http(), name, AttachmentType::from(Path::new(&path))) + .await + } else { + channel.create_webhook(ctx.http(), name).await + } +} + #[command] #[supports_dm(false)] #[permission_level(Restricted)] @@ -1205,13 +1223,9 @@ async fn create_reminder, S: ToString + Type + Encode = env::var("WEBHOOK_AVATAR").ok(); }