add probability field to rules

This commit is contained in:
Jared Baldridge 2022-04-02 23:35:31 -07:00
parent a1e5d2c87d
commit 0c0c24778e
3 changed files with 8 additions and 0 deletions

View file

@ -59,6 +59,7 @@ class Config(BaseProxyConfig):
not_rooms=set(rule.get("not_rooms", [])),
matches=self._compile_all(rule["matches"]),
not_matches=self._compile_all(rule.get("not_matches", [])),
probability=rule.get("probability", 1.0),
type=EventType.find(rule["type"]) if "type" in rule else None,
template=self.templates[rule["template"]],
variables=self._parse_variables(rule))

View file

@ -18,6 +18,8 @@ from typing import Optional, Match, Dict, List, Set, Union, Pattern, Any
from attr import dataclass
from jinja2 import Template as JinjaTemplate
from random import random
from mautrix.types import RoomID, EventType
from maubot import MessageEvent
@ -34,6 +36,7 @@ class Rule:
not_rooms: Set[RoomID]
matches: List[RPattern]
not_matches: List[RPattern]
probability: float
template: Template
type: Optional[EventType]
variables: Dict[str, Any]
@ -49,6 +52,8 @@ class Rule:
return None
elif evt.room_id in self.not_rooms:
return None
elif self.probability < random():
return None
for pattern in self.matches:
match = pattern.search(evt.content.body)
if match: