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
msg_refactor
Joey Hines 2022-03-19 21:37:05 -06:00
parent 0796f0be21
commit 0baf273237
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
3 changed files with 62 additions and 18 deletions

25
Cargo.lock generated
View File

@ -321,6 +321,7 @@ checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-executor",
"futures-io", "futures-io",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
@ -343,12 +344,34 @@ version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
[[package]]
name = "futures-executor"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]] [[package]]
name = "futures-io" name = "futures-io"
version = "0.3.21" version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b"
[[package]]
name = "futures-macro"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.21" version = "0.3.21"
@ -370,6 +393,7 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-io", "futures-io",
"futures-macro",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
"memchr", "memchr",
@ -1750,6 +1774,7 @@ version = "0.2.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"config", "config",
"futures",
"rand 0.8.5", "rand 0.8.5",
"regex", "regex",
"serde", "serde",

View File

@ -13,6 +13,7 @@ serde = "1.0.136"
rand = "0.8.5" rand = "0.8.5"
toml = "0.5.8" toml = "0.5.8"
regex = "1.5.5" regex = "1.5.5"
futures = "0.3.21"
[dependencies.serenity] [dependencies.serenity]
version = "0.10.10" version = "0.10.10"

View File

@ -1,7 +1,7 @@
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::standard::Args; use serenity::framework::standard::Args;
use serenity::http::AttachmentType; 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::guild::{Guild, Member};
use serenity::model::id::{ChannelId, UserId}; use serenity::model::id::{ChannelId, UserId};
use serenity::model::Permissions; use serenity::model::Permissions;
@ -13,6 +13,7 @@ use crate::game::global_data::GlobalData;
use crate::game::player_data::PlayerData; use crate::game::player_data::PlayerData;
use crate::game::MessageSource; use crate::game::MessageSource;
use crate::{error, game}; use crate::{error, game};
use serenity::prelude::SerenityError;
pub async fn send_msg_to_player_channels( pub async fn send_msg_to_player_channels(
ctx: &Context, ctx: &Context,
@ -23,20 +24,25 @@ pub async fn send_msg_to_player_channels(
attachment: Option<Vec<AttachmentType<'_>>>, attachment: Option<Vec<AttachmentType<'_>>>,
pin: bool, pin: bool,
) -> error::Result<()> { ) -> error::Result<()> {
for player_data in &global_data.game_state_mut()?.player_data { let msg_tasks = global_data
if let MessageSource::Player(channel_id) = msg_source { .game_state_mut()?
if channel_id == player_data.channel { .player_data
continue; .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 channel.send_message(&ctx.http, |m| {
.channels
.get(&ChannelId::from(player_data.channel))
.unwrap();
let msg = channel
.send_message(&ctx.http, |m| {
m.content(&msg); m.content(&msg);
if let Some(attachment) = attachment.clone() { if let Some(attachment) = attachment.clone() {
@ -45,12 +51,24 @@ pub async fn send_msg_to_player_channels(
m m
}) })
.await?; });
if pin { let msgs: Result<Vec<Message>, SerenityError> = futures::future::join_all(msg_tasks)
// pin system messages .await
msg.pin(&ctx.http).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 let host_channel = guild