Added profile pic support + fixed an issue with webhook serailization

+ Added imgur module to handle interacting with the imgur api
+ Users now get a random profile pic from an imgur album
+ Switched to using webhook ids instead of storing the full webhook in toml, serenity does not seem to like the toml deserialization
This commit is contained in:
2022-03-20 17:42:50 -06:00
parent 71b8bc6e20
commit ea0be5c708
11 changed files with 363 additions and 59 deletions
+1 -15
View File
@@ -61,21 +61,7 @@ impl EventHandler for Handler {
}
}
async fn ready(&self, ctx: Context, ready: Ready) {
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();
let mut global_data = global_data.lock().await;
let host_webhook = ctx
.http
.get_webhook_from_url(&global_data.cfg.host_webhook)
.await
.expect("Unable to open host webhook");
global_data.host_webhook = Some(host_webhook);
async fn ready(&self, _ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
+31 -21
View File
@@ -13,9 +13,17 @@ use crate::game::global_data::GlobalData;
use crate::game::player_data::PlayerData;
use crate::game::MessageSource;
use crate::{error, game};
use serenity::model::prelude::Webhook;
use serenity::prelude::SerenityError;
fn filter_source_channel(player_data: &&PlayerData, msg_source: &MessageSource) -> bool {
if let MessageSource::Player(source_player) = &msg_source {
if source_player.channel == player_data.channel {
return false;
}
}
true
}
pub async fn send_msg_to_player_channels(
ctx: &Context,
guild: &Guild,
@@ -29,14 +37,7 @@ pub async fn send_msg_to_player_channels(
.game_state_mut()?
.player_data
.iter()
.filter(|player_data| {
if let MessageSource::Player(source_player) = &msg_source {
if source_player.channel == player_data.channel {
return false;
}
}
true
})
.filter(|player| filter_source_channel(player, &msg_source))
.map(|player_data| {
let channel = guild
.channels
@@ -107,15 +108,23 @@ pub async fn send_msg_to_player_channels(
pub async fn send_webhook_msg(
http: &Http,
webhook: &Webhook,
webhook_id: u64,
username: &str,
profile_pic_url: Option<String>,
msg: &str,
attachment: Option<Vec<AttachmentType<'_>>>,
) -> error::Result<()> {
let webhook = http.get_webhook(webhook_id).await?;
webhook
.execute(http, false, |w| {
w.content(&msg).username(username);
if let Some(profile_pic_url) = profile_pic_url {
w.avatar_url(profile_pic_url);
}
if let Some(attachment) = attachment.clone() {
w.add_files(attachment);
}
@@ -141,23 +150,22 @@ pub async fn send_webhook_msg_to_player_channels(
MessageSource::Automated => "Woxlf System Message".to_string(),
};
let profile_pic = match &msg_source {
MessageSource::Player(p) => Some(p.profile_pic_url.clone()),
MessageSource::Host | MessageSource::Automated => None,
};
let msg_tasks = global_data
.game_state_mut()?
.player_data
.iter()
.filter(|player_data| {
if let MessageSource::Player(source_player) = &msg_source {
if source_player.channel == player_data.channel {
return false;
}
}
true
})
.filter(|player| filter_source_channel(player, &msg_source))
.map(|player_data| {
send_webhook_msg(
&ctx.http,
&player_data.channel_webhook,
player_data.channel_webhook_id,
&msg_username,
profile_pic.clone(),
msg,
attachment.clone(),
)
@@ -188,8 +196,9 @@ pub async fn send_webhook_msg_to_player_channels(
send_webhook_msg(
&ctx.http,
global_data.host_webhook()?,
global_data.cfg.host_webhook_id,
&host_channel_username,
profile_pic,
msg,
attachment,
)
@@ -257,7 +266,8 @@ pub async fn add_user_to_game(
discord_id: discord_user.user.id.0,
vote_target: None,
codename,
channel_webhook: webhook,
channel_webhook_id: webhook.id.0,
profile_pic_url: global_data.get_profile_pic_url().await?,
};
global_data.game_state_mut()?.player_data.push(player_data);