diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e79d6f..cfbc6e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Procedure when bumping the version number: - `bot::botrulez::FullHelp` now implements `bot::command::Command` - `bot::botrulez::Ping` now implements `bot::command::Command` - `bot::botrulez::ShortHelp` now implements `bot::command::Command` +- `bot::instances::Instances::is_from_known_instance` ### Changed - Instances log to target `euphoxide::live::` diff --git a/src/bot/instances.rs b/src/bot/instances.rs index f54ecf6..6607a70 100644 --- a/src/bot/instances.rs +++ b/src/bot/instances.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use super::instance::{Instance, ServerConfig}; +use super::instance::{self, Instance, ServerConfig}; /// A convenient way to keep a [`ServerConfig`] and some [`Instance`]s. pub struct Instances { @@ -26,6 +26,18 @@ impl Instances { self.instances.values() } + /// Check if an event comes from an instance whose name is known. + /// + /// Assuming every instance has a unique name, events from unknown instances + /// should be discarded. This helps prevent "ghost instances" that were + /// stopped but haven't yet disconnected properly from influencing your + /// bot's state. + /// + /// The user is responsible for ensuring that instances' names are unique. + pub fn is_from_known_instance(&self, event: &instance::Event) -> bool { + self.instances.contains_key(&event.config().name) + } + pub fn is_empty(&self) -> bool { self.instances.is_empty() }