-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
271 lines (217 loc) · 8.38 KB
/
utils.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import re
import time
import logging
from datetime import datetime, timezone, timedelta
from twitchio.ext import commands
from twitchio import PartialUser, Channel
from typing import List, Optional, Tuple
import sys
import os
import sqlite3
import validators
from logger import log_info, log_error, log_warning, log_debug, get_logger
logger = get_logger("twitch_bot.utils")
# Add the virtual environment's site-packages to sys.path
venv_path = os.getenv("PYTHONPATH")
if venv_path:
sys.path.append(venv_path)
def normalize_username(username: str) -> str:
return username.lstrip("@")
def split_message(message: str, max_length: int = 500) -> List[str]:
if len(message) <= max_length:
return [message]
sentences = re.findall(r"[^.!?]+[.!?]?", message)
return _chunk_sentences(sentences, max_length)
def _chunk_sentences(sentences: List[str], max_length: int) -> List[str]:
messages, current_chunk = [], ""
for sentence in sentences:
sentence = sentence.strip()
if len(current_chunk) + len(sentence) + 1 > max_length:
if current_chunk:
messages.append(current_chunk)
current_chunk = sentence if len(sentence) <= max_length else ""
if len(sentence) > max_length:
messages.extend(_split_long_sentence(sentence, max_length))
else:
current_chunk += (" " + sentence) if current_chunk else sentence
if current_chunk:
messages.append(current_chunk)
return messages
def _split_long_sentence(sentence: str, max_length: int) -> List[str]:
words = sentence.split()
sub_chunks, current_chunk = [], ""
for word in words:
if len(current_chunk) + len(word) + 1 > max_length:
sub_chunks.append(current_chunk)
current_chunk = word
else:
current_chunk += (" " + word) if current_chunk else word
if current_chunk:
sub_chunks.append(current_chunk)
return sub_chunks
def remove_duplicate_sentences(text: str) -> str:
sentences = re.split(r"(?<=[.!?]) +", text)
seen, unique_sentences = set(), []
for sentence in sentences:
normalized = sentence.strip().lower()
if normalized not in seen:
unique_sentences.append(sentence.strip())
seen.add(normalized)
return " ".join(unique_sentences)
async def fetch_user(bot: commands.Bot, user_identifier: str) -> Optional[PartialUser]:
try:
user_identifier = user_identifier.lstrip("@")
users = (
await bot.fetch_users(ids=[user_identifier])
if user_identifier.isdigit()
else await bot.fetch_users(names=[user_identifier])
)
return users[0] if users else None
except Exception as e:
log_error(f"Error fetching user '{user_identifier}': {e}")
return None
def get_channel(bot: commands.Bot, channel_name: str) -> Optional[Channel]:
channel = bot.get_channel(channel_name)
if not channel:
log_warning(f"Channel '{channel_name}' not found in bot's connected channels.")
return channel
def expand_time_units(time_str: str) -> str:
return re.sub(r"(\d)([a-zA-Z])", r"\1 \2", time_str)
def parse_time_string(time_str: str) -> Optional[timedelta]:
time_str = time_str.lower().replace(",", " ").replace("and", " ").replace("-", " ")
pattern = r"(?P<value>\d+(\.\d+)?)\s*(?P<unit>[a-zA-Z]+)"
matches = re.finditer(pattern, time_str)
kwargs = {}
unit_map = {
"second": "seconds",
"sec": "seconds",
"s": "seconds",
"minute": "minutes",
"min": "minutes",
"m": "minutes",
"hour": "hours",
"h": "hours",
"day": "days",
"d": "days",
"week": "weeks",
"w": "weeks",
"month": "days",
"year": "days",
"y": "days",
}
for match in matches:
value, unit = float(match.group("value")), match.group("unit")
key = next((v for k, v in unit_map.items() if unit.startswith(k)), None)
if key:
kwargs.setdefault(key, 0)
kwargs[key] += value * (30 if unit == "month" else 365 if unit == "year" else 1)
return timedelta(**kwargs) if kwargs else None
def parse_time(args: List[str], expect_time_keyword_at_start: bool = True) -> Tuple[Optional[datetime], str]:
from dateparser import parse
time_keywords = ["in", "on", "after"]
time_str = " ".join(args)
message = ""
if expect_time_keyword_at_start and args and args[0].lower() in time_keywords:
time_str = " ".join(args[1:])
else:
return None, time_str
for i in range(len(args), 0, -1):
time_part = " ".join(args[:i])
message = " ".join(args[i:])
delta = parse_time_string(time_part)
if delta:
return datetime.now(timezone.utc) + delta, message
parsed_time = parse(
time_part,
settings={
"TIMEZONE": "UTC",
"RETURN_AS_TIMEZONE_AWARE": True,
"PREFER_DATES_FROM": "future",
"RELATIVE_BASE": datetime.now(timezone.utc),
},
)
if parsed_time:
return parsed_time, message
return False, "Could not parse the time specified."
def format_time_delta(delta: timedelta) -> str:
total_seconds = int(delta.total_seconds())
periods = [("d", 86400), ("h", 3600), ("m", 60), ("s", 1)]
return " ".join(f"{value}{name}" for name, seconds in periods if (value := total_seconds // seconds)) or "0s"
def format_duration(seconds):
seconds = int(seconds)
weeks, remainder = divmod(seconds, 604800)
days, remainder = divmod(remainder, 86400)
hours, remainder = divmod(remainder, 3600)
minutes, seconds = divmod(remainder, 60)
parts = []
for value, unit in [(weeks, "w"), (days, "d"), (hours, "h"), (minutes, "m"), (seconds, "s")]:
if value:
parts.append(f"{value}{unit}")
return " ".join(parts) or "0s"
def get_afk_duration(start_time):
return time.time() - start_time
def get_database_connection(db_path="bot.db") -> sqlite3.Connection:
return sqlite3.connect(db_path)
def setup_database(db_path="bot.db"):
with get_database_connection(db_path) as conn:
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS reminders (
id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL,
username TEXT NOT NULL,
target_id INTEGER NOT NULL,
target_name TEXT NOT NULL,
channel_id INTEGER NOT NULL,
channel_name TEXT NOT NULL,
message TEXT NOT NULL,
remind_time TEXT,
private INTEGER NOT NULL,
trigger_on_message INTEGER NOT NULL,
active INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS user_stats (
user_id TEXT PRIMARY KEY,
username TEXT,
message_count INTEGER DEFAULT 0,
first_seen TIMESTAMP,
last_seen TIMESTAMP
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS afk (
user_id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
afk_time REAL NOT NULL,
reason TEXT,
return_time REAL,
active INTEGER NOT NULL DEFAULT 1
)
"""
)
conn.commit()
def is_valid_url(url: str) -> bool:
return validators.url(url)
def format_time_ago(timestamp: datetime) -> str:
now = datetime.now(timezone.utc)
delta = now - timestamp
if delta.days > 365:
return f"{delta.days // 365} year{'s' if delta.days // 365 != 1 else ''} ago"
elif delta.days > 30:
return f"{delta.days // 30} month{'s' if delta.days // 30 != 1 else ''} ago"
elif delta.days > 0:
return f"{delta.days} day{'s' if delta.days != 1 else ''} ago"
elif delta.seconds > 3600:
return f"{delta.seconds // 3600} hour{'s' if delta.seconds // 3600 != 1 else ''} ago"
elif delta.seconds > 60:
return f"{delta.seconds // 60} minute{'s' if delta.seconds // 60 != 1 else ''} ago"
else:
return f"{delta.seconds} second{'s' if delta.seconds != 1 else ''} ago"