Improved message handling

+ Messages broadcast to all channels are now all started in "parallel"
+ This should improve message times to all channels, especially when there are many players
+ Start of the logic needed for having multiple deputy bots and one chief bot
+ clippy + fmt
This commit is contained in:
2022-03-19 21:37:05 -06:00
parent 0796f0be21
commit 0baf273237
3 changed files with 62 additions and 18 deletions
+36 -18
View File
@@ -1,7 +1,7 @@
use serenity::client::Context;
use serenity::framework::standard::Args;
use serenity::http::AttachmentType;
use serenity::model::channel::{PermissionOverwrite, PermissionOverwriteType};
use serenity::model::channel::{Message, PermissionOverwrite, PermissionOverwriteType};
use serenity::model::guild::{Guild, Member};
use serenity::model::id::{ChannelId, UserId};
use serenity::model::Permissions;
@@ -13,6 +13,7 @@ use crate::game::global_data::GlobalData;
use crate::game::player_data::PlayerData;
use crate::game::MessageSource;
use crate::{error, game};
use serenity::prelude::SerenityError;
pub async fn send_msg_to_player_channels(
ctx: &Context,
@@ -23,20 +24,25 @@ pub async fn send_msg_to_player_channels(
attachment: Option<Vec<AttachmentType<'_>>>,
pin: bool,
) -> error::Result<()> {
for player_data in &global_data.game_state_mut()?.player_data {
if let MessageSource::Player(channel_id) = msg_source {
if channel_id == player_data.channel {
continue;
let msg_tasks = global_data
.game_state_mut()?
.player_data
.iter()
.filter(|player_data| {
if let MessageSource::Player(channel_id) = msg_source {
if channel_id == player_data.channel {
return false;
}
}
}
true
})
.map(|player_data| {
let channel = guild
.channels
.get(&ChannelId::from(player_data.channel))
.unwrap();
let channel = guild
.channels
.get(&ChannelId::from(player_data.channel))
.unwrap();
let msg = channel
.send_message(&ctx.http, |m| {
channel.send_message(&ctx.http, |m| {
m.content(&msg);
if let Some(attachment) = attachment.clone() {
@@ -45,12 +51,24 @@ pub async fn send_msg_to_player_channels(
m
})
.await?;
});
if pin {
// pin system messages
msg.pin(&ctx.http).await?;
}
let msgs: Result<Vec<Message>, SerenityError> = futures::future::join_all(msg_tasks)
.await
.into_iter()
.collect();
let msgs = msgs?;
if pin {
let pin_tasks = msgs.iter().map(|msg| msg.pin(&ctx.http));
let pins: Result<(), SerenityError> = futures::future::join_all(pin_tasks)
.await
.into_iter()
.collect();
pins?;
}
let host_channel = guild