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

@ -49,6 +49,8 @@ Rules have five fields. Only `matches` and `template` are required.
* `rooms` - The list of rooms where the rule should apply. * `rooms` - The list of rooms where the rule should apply.
If empty, the rule will apply to all rooms the bot is in. If empty, the rule will apply to all rooms the bot is in.
* `matches` - The regex or list of regexes to match. * `matches` - The regex or list of regexes to match.
* `probability` - The probability the rule will be executed expressed as a float
between 0 and 1.0. Defaults to 1.0.
* `template` - The name of the template to use. * `template` - The name of the template to use.
* `variables` - A key-value map of variables to extend or override template variables. * `variables` - A key-value map of variables to extend or override template variables.
Like with template variables, the values are parsed as Jinja2 templates. Like with template variables, the values are parsed as Jinja2 templates.

View file

@ -59,6 +59,7 @@ class Config(BaseProxyConfig):
not_rooms=set(rule.get("not_rooms", [])), not_rooms=set(rule.get("not_rooms", [])),
matches=self._compile_all(rule["matches"]), matches=self._compile_all(rule["matches"]),
not_matches=self._compile_all(rule.get("not_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, type=EventType.find(rule["type"]) if "type" in rule else None,
template=self.templates[rule["template"]], template=self.templates[rule["template"]],
variables=self._parse_variables(rule)) 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 attr import dataclass
from jinja2 import Template as JinjaTemplate from jinja2 import Template as JinjaTemplate
from random import random
from mautrix.types import RoomID, EventType from mautrix.types import RoomID, EventType
from maubot import MessageEvent from maubot import MessageEvent
@ -34,6 +36,7 @@ class Rule:
not_rooms: Set[RoomID] not_rooms: Set[RoomID]
matches: List[RPattern] matches: List[RPattern]
not_matches: List[RPattern] not_matches: List[RPattern]
probability: float
template: Template template: Template
type: Optional[EventType] type: Optional[EventType]
variables: Dict[str, Any] variables: Dict[str, Any]
@ -49,6 +52,8 @@ class Rule:
return None return None
elif evt.room_id in self.not_rooms: elif evt.room_id in self.not_rooms:
return None return None
elif self.probability < random():
return None
for pattern in self.matches: for pattern in self.matches:
match = pattern.search(evt.content.body) match = pattern.search(evt.content.body)
if match: if match: