|
@@ -1,4 +1,5 @@
|
1
|
1
|
from math import floor
|
|
2
|
+import re
|
2
|
3
|
from typing import Dict
|
3
|
4
|
from million.model.message import Message
|
4
|
5
|
|
|
@@ -7,19 +8,29 @@ memoization: Dict[Message, int] = {}
|
7
|
8
|
# TODO WIP
|
8
|
9
|
# - DNS to resolve audio, gif, pictures with counts
|
9
|
10
|
def __compute__(msg: Message) -> int:
|
10
|
|
- value = None
|
11
|
|
- # Remove any number that is not a digit
|
|
11
|
+ value = __computeContent(msg)
|
|
12
|
+
|
|
13
|
+ memoization[msg] = value
|
|
14
|
+ return value
|
|
15
|
+
|
|
16
|
+def __computeContent(msg: Message) -> int:
|
12
|
17
|
# TODO parse potential math expressions in content
|
13
|
|
- cleaned_content = ''.join([c for c in msg.content if c.isdigit()])
|
14
|
|
- try:
|
15
|
|
- value = floor(float(cleaned_content))
|
16
|
|
- except Exception as e:
|
17
|
|
- raise ValueError(
|
18
|
|
- f"Message {cleaned_content} does not contain a number ({e})")
|
|
18
|
+ match = re.search(r"\d+", msg.content)
|
|
19
|
+
|
|
20
|
+ if match:
|
|
21
|
+ value = int(match[0])
|
|
22
|
+ else:
|
|
23
|
+ value = None
|
19
|
24
|
|
20
|
|
- memoization[msg] = value
|
21
|
25
|
return value
|
22
|
26
|
|
|
27
|
+def reset(msg: Message) -> None:
|
|
28
|
+ if msg in memoization:
|
|
29
|
+ memoization.pop(msg)
|
|
30
|
+
|
|
31
|
+def reset() -> None:
|
|
32
|
+ memoization.clear()
|
|
33
|
+
|
23
|
34
|
def get(msg: Message) -> int:
|
24
|
35
|
"""
|
25
|
36
|
Returns the estimated value counted in this message
|