-
Notifications
You must be signed in to change notification settings - Fork 3
/
messages.py
175 lines (138 loc) · 4.27 KB
/
messages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import json
import random
from datetime import datetime as dt
from enum import Enum
from typing import List, Dict, Callable
from config import Config
from trigger import Trigger
from utils import (
get_fact,
translate,
)
config = Config()
def timer_minutes(minutes: int = 1440):
recent_time = int(dt.now().timestamp() - minutes * 60)
def wrapper(msg):
nonlocal recent_time
is_spend_time = msg.date - recent_time
print("closure:", recent_time, "msg",
msg.date, is_spend_time > minutes * 60)
if is_spend_time > minutes * 60:
recent_time = msg.date
return True
return False
return wrapper
def only_user(users: List[str]) -> Callable:
def wrapper(msg):
return msg.from_user.username in users
return wrapper
def text_contains(words: List[str]) -> Callable:
def wrapper(msg):
return any([word in msg.text.lower() for word in words])
return wrapper
def text_word_include(words: List[str]) -> Callable:
def wrapper(msg):
return any([word in msg.text.lower().split() for word in words])
return wrapper
def message_more(length: int) -> Callable:
def wrapper(msg):
return all([len(msg.text.lower()) > length, not msg.text.startswith("http")])
return wrapper
def only_manually() -> Callable:
def wrapper(*args):
return False
return wrapper
def debug():
debug_dict = {}
for m in messages:
debug_dict[str(m)] = messages[m].debug()
return ("{}".format(json.dumps(debug_dict)),)
class MessagesType(Enum):
PUNCH = "punch"
TRANSLATE = "translate"
SKIP = "skip"
FACT = "fact"
FIGHT = "fight"
PING = "ping"
SAY_HI = "say_hi_albert"
COMPANY = "company"
DEBUG = "debug"
messages: Dict[MessagesType, Trigger] = {
MessagesType.PUNCH: Trigger(
chance=10,
command="punch",
condition=[only_user(["eromanovskyj"]), timer_minutes(2880)],
text=lambda: random.choice(config.phrases),
bot_type="reply",
),
MessagesType.FACT: Trigger(
chance=10,
command="fact",
condition=[
only_user(["eromanovskyj"]),
text_contains(
[
"беларус",
"минск",
"лукашенк",
"картош",
"картоха",
"мiнск",
"минcк",
"минсk",
]
),
timer_minutes(1440),
],
text=get_fact,
bot_type="reply",
),
MessagesType.FIGHT: Trigger(
chance=30,
condition=[text_word_include(["бой"]), timer_minutes(1440), ],
text=lambda: "Мой хуй с твоей губой",
bot_type="reply",
),
MessagesType.TRANSLATE: Trigger(
chance=30,
condition=[only_user(["eromanovskyj"]), message_more(
100), timer_minutes(2880), ],
text=translate,
bot_type="reply",
),
MessagesType.SKIP: Trigger(
chance=30,
condition=[text_word_include(["пас"]), timer_minutes(2880)],
text="Парни сори, я пас, сегодня я каблук",
bot_type="reply",
),
MessagesType.SAY_HI: Trigger(
chance=30,
command="sayhialbert",
condition=[only_user(["eromanovskyj", "scheglov"]), only_manually()],
text=lambda: random.choice(config.stickers),
bot_type="sticker",
),
MessagesType.DEBUG: Trigger(
chance=30,
command="debug",
condition=[only_user(["scheglov"]), only_manually()],
text=debug,
bot_type="reply",
),
MessagesType.FIGHT: Trigger(
chance=100,
condition=[text_contains(["лебедев"]), timer_minutes(1440), ],
text=lambda: "Хочу напомнить если забыли: Лебедев - петушара",
bot_type="reply",
),
}
def message_turbine(msg):
sorted_by_chance = sorted(
messages.items(), key=lambda item: item[1].current_chance, reverse=True
)
for (_, trigger) in sorted_by_chance:
trigger_msg = trigger.on(msg)
if trigger_msg:
return trigger_msg
return None