Added update types filtering.
This commit is contained in:
@ -18,6 +18,14 @@ pub enum TextMatchMethod {
|
|||||||
IMatches,
|
IMatches,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
pub enum UpdateType {
|
||||||
|
New,
|
||||||
|
Edited,
|
||||||
|
Deleted,
|
||||||
|
}
|
||||||
|
|
||||||
/// This filter is used to filter out
|
/// This filter is used to filter out
|
||||||
/// that marked as silent.
|
/// that marked as silent.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -40,6 +48,10 @@ pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RegexFilter(pub Regex);
|
pub struct RegexFilter(pub Regex);
|
||||||
|
|
||||||
|
/// Filters using provided regex.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct UpdateTypeFilter<'a>(pub &'a [UpdateType]);
|
||||||
|
|
||||||
impl Filter for ExcludedChatsFilter {
|
impl Filter for ExcludedChatsFilter {
|
||||||
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
|
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
|
||||||
let a = match update {
|
let a = match update {
|
||||||
@ -102,3 +114,19 @@ impl Filter for RegexFilter {
|
|||||||
Ok(self.0.is_match(message.text()))
|
Ok(self.0.is_match(message.text()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> Filter for UpdateTypeFilter<'a> {
|
||||||
|
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
|
||||||
|
for update_type in self.0 {
|
||||||
|
match (update_type, update) {
|
||||||
|
(UpdateType::New, Update::NewMessage(_))
|
||||||
|
| (UpdateType::Edited, Update::MessageEdited(_))
|
||||||
|
| (UpdateType::Deleted, Update::MessageDeleted(_)) => {
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
_ => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@ use super::{
|
|||||||
filtered_handler::FilteredHandler,
|
filtered_handler::FilteredHandler,
|
||||||
message_fitlers::{
|
message_fitlers::{
|
||||||
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, RegexFilter,
|
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, RegexFilter,
|
||||||
SilentFilter, TextFilter, TextMatchMethod,
|
SilentFilter, TextFilter, TextMatchMethod, UpdateType, UpdateTypeFilter,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handlers::{
|
handlers::{
|
||||||
@ -98,9 +98,10 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::IMatches)),
|
FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::IMatches)),
|
||||||
// Greeting my fellow humans.
|
// Greeting my fellow humans.
|
||||||
FilteredHandler::new(Greeter)
|
FilteredHandler::new(Greeter)
|
||||||
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
|
||||||
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
||||||
|
.add_filter(SilentFilter)
|
||||||
|
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
||||||
.add_filter(TextFilter(&["привет"], TextMatchMethod::IStartsWith))
|
.add_filter(TextFilter(&["привет"], TextMatchMethod::IStartsWith))
|
||||||
.add_filter(ExcludedChatsFilter(args.excluded_chats)),
|
.add_filter(ExcludedChatsFilter(args.excluded_chats)),
|
||||||
// Getting chat id.
|
// Getting chat id.
|
||||||
@ -108,26 +109,31 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
.add_filter(TextFilter(&[".cid"], TextMatchMethod::IMatches)),
|
.add_filter(TextFilter(&[".cid"], TextMatchMethod::IMatches)),
|
||||||
// Make бля fun again.
|
// Make бля fun again.
|
||||||
FilteredHandler::new(Blyaficator)
|
FilteredHandler::new(Blyaficator)
|
||||||
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".bl"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".bl"], TextMatchMethod::IStartsWith)),
|
||||||
// Handler for converting currecies.
|
// Handler for converting currecies.
|
||||||
FilteredHandler::new(CurrencyConverter::new()?)
|
FilteredHandler::new(CurrencyConverter::new()?)
|
||||||
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
|
.add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
|
||||||
.add_filter(CurrencyTextFilter),
|
.add_filter(CurrencyTextFilter),
|
||||||
// Simlpe rotator.
|
// Simlpe rotator.
|
||||||
FilteredHandler::new(Rotator)
|
FilteredHandler::new(Rotator)
|
||||||
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".rl"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".rl"], TextMatchMethod::IStartsWith)),
|
||||||
// Weather forecast.
|
// Weather forecast.
|
||||||
FilteredHandler::new(WeatherForecaster::new()?)
|
FilteredHandler::new(WeatherForecaster::new()?)
|
||||||
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".w"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".w"], TextMatchMethod::IStartsWith)),
|
||||||
// Smiley repeator.
|
// Smiley repeator.
|
||||||
FilteredHandler::new(Repeator)
|
FilteredHandler::new(Repeator)
|
||||||
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
|
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
||||||
.add_filter(RegexFilter(Regex::new("^[)0]+$")?)),
|
.add_filter(RegexFilter(Regex::new("^[)0]+$")?)),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user