made webhook avatar configurable

This commit is contained in:
jude 2020-10-14 11:31:18 +01:00
parent 254c9b04e9
commit dca2b37fb0
4 changed files with 27 additions and 11 deletions

View File

@ -33,3 +33,4 @@ __Other Variables__
* `PYTHON_LOCATION` - default `venv/bin/python3`. Can be changed if your Python executable is located somewhere else * `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) * `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 * `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**

BIN
assets/webhook.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -6,12 +6,15 @@ use serenity::{
cache::Cache, cache::Cache,
client::Context, client::Context,
framework::standard::CommandResult, framework::standard::CommandResult,
http::CacheHttp, http::{AttachmentType, CacheHttp},
model::{ model::{
channel::GuildChannel,
channel::Message, channel::Message,
id::{ChannelId, GuildId, UserId}, id::{ChannelId, GuildId, UserId},
misc::Mentionable, misc::Mentionable,
webhook::Webhook,
}, },
Result as SerenityResult,
}; };
use tokio::process::Command; use tokio::process::Command;
@ -20,7 +23,7 @@ use crate::{
check_subscription_on_message, check_subscription_on_message,
consts::{ consts::{
CHARACTERS, DAY, HOUR, LOCAL_TIMEZONE, MAX_TIME, MINUTE, MIN_INTERVAL, PYTHON_LOCATION, 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, framework::SendIterator,
models::{ChannelData, GuildData, Timer, UserData}, models::{ChannelData, GuildData, Timer, UserData},
@ -43,14 +46,14 @@ use num_integer::Integer;
use std::{ use std::{
convert::TryInto, convert::TryInto,
default::Default, default::Default,
fmt::Display,
path::Path,
string::ToString, string::ToString,
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
use regex::Regex; use regex::Regex;
use serde_json::json;
fn shorthand_displacement(seconds: u64) -> String { fn shorthand_displacement(seconds: u64) -> String {
let (hours, seconds) = seconds.div_rem(&HOUR); let (hours, seconds) = seconds.div_rem(&HOUR);
let (minutes, seconds) = seconds.div_rem(&MINUTE); let (minutes, seconds) = seconds.div_rem(&MINUTE);
@ -77,6 +80,21 @@ fn longhand_displacement(seconds: u64) -> String {
sections.join(", ") sections.join(", ")
} }
async fn create_webhook(
ctx: impl CacheHttp,
channel: GuildChannel,
name: impl Display,
avatar: Option<String>,
) -> SerenityResult<Webhook> {
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] #[command]
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
@ -1205,12 +1223,8 @@ async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MyS
if let Some(guild_channel) = channel.guild() { if let Some(guild_channel) = channel.guild() {
if channel_data.webhook_token.is_none() || channel_data.webhook_id.is_none() { if channel_data.webhook_token.is_none() || channel_data.webhook_id.is_none() {
if let Ok(webhook) = ctx if let Ok(webhook) =
.http() create_webhook(&ctx, guild_channel, "Reminder", WEBHOOK_AVATAR.clone())
.create_webhook(
guild_channel.id.as_u64().to_owned(),
&json!({"name": "Reminder"}),
)
.await .await
{ {
channel_data.webhook_id = Some(webhook.id.as_u64().to_owned()); channel_data.webhook_id = Some(webhook.id.as_u64().to_owned());

View File

@ -53,4 +53,5 @@ lazy_static! {
THEME_COLOR_FALLBACK, THEME_COLOR_FALLBACK,
|inner| u32::from_str_radix(&inner, 16).unwrap_or(THEME_COLOR_FALLBACK) |inner| u32::from_str_radix(&inner, 16).unwrap_or(THEME_COLOR_FALLBACK)
); );
pub static ref WEBHOOK_AVATAR: Option<String> = env::var("WEBHOOK_AVATAR").ok();
} }