2023-10-27 15:31:53 +02:00
|
|
|
|
# SPDX-FileCopyrightText: 2023 Egor Guslyancev <electromagneticcyclone@disroot.org>
|
|
|
|
|
#
|
2023-12-04 10:13:56 +01:00
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-13 20:52:30 +01:00
|
|
|
|
"Main bot module."
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
import time
|
2023-12-13 19:41:19 +01:00
|
|
|
|
import ast
|
2023-10-27 15:31:53 +02:00
|
|
|
|
import datetime as dt
|
2023-12-13 19:41:19 +01:00
|
|
|
|
import typing
|
2023-10-27 15:31:53 +02:00
|
|
|
|
from sys import stderr, stdout, stdin
|
|
|
|
|
from threading import Thread
|
2023-12-12 14:01:52 +01:00
|
|
|
|
import telebot
|
2023-12-18 12:01:00 +01:00
|
|
|
|
import config_reader as cr
|
2023-12-13 19:41:19 +01:00
|
|
|
|
import db_classes
|
2023-12-18 19:24:25 +01:00
|
|
|
|
import timeout as tmo
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
|
|
|
|
|
2023-12-13 20:39:25 +01:00
|
|
|
|
# TODO more backends (redis at least)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
db = db_classes.PickleDB(".db")
|
|
|
|
|
db.load()
|
2024-01-01 00:26:57 +01:00
|
|
|
|
CURRENT_VERSION = "v1.0rc12"
|
2023-12-13 19:41:19 +01:00
|
|
|
|
VERSION = db.read("about.version", CURRENT_VERSION)
|
|
|
|
|
db.write("about.author", "electromagneticcyclone")
|
|
|
|
|
db.write("about.tester", "angelbeautifull")
|
|
|
|
|
db.write(
|
2023-12-12 14:01:52 +01:00
|
|
|
|
"about.source", "https://git.disroot.org/electromagneticcyclone/duty-board-dog"
|
|
|
|
|
)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
if (db.read("about.host") is None) and __debug__:
|
2023-10-27 15:31:53 +02:00
|
|
|
|
stdout.write("Введите username хоста: ")
|
|
|
|
|
stdout.flush()
|
2023-12-13 19:41:19 +01:00
|
|
|
|
db.write("about.host", stdin.readline()[:-1])
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
|
|
|
|
|
2023-12-18 13:31:28 +01:00
|
|
|
|
MODE = "devel" if __debug__ else "prod"
|
|
|
|
|
bot = telebot.TeleBot(cr.read(f"tokens.{MODE}"), parse_mode="MarkdownV2")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-10 17:37:24 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def get_time(forum: int) -> dt.datetime:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
"Get datetime.now in forum's timezone. Default timezone is UTC+3."
|
|
|
|
|
return dt.datetime.now(dt.UTC) + dt.timedelta(
|
2023-12-18 19:24:25 +01:00
|
|
|
|
hours=db.read(f"{forum}.settings.timezone", 3)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-10-31 10:23:14 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def change_phase(forum: int, date: dt.datetime = None) -> None:
|
|
|
|
|
"Changes forum's current phase."
|
2023-10-31 10:23:14 +01:00
|
|
|
|
if date is None:
|
|
|
|
|
date = get_time(forum).date()
|
2023-12-18 19:24:25 +01:00
|
|
|
|
phase = db.read(f"{forum}.schedule.phase")
|
|
|
|
|
phases = db.read(f"{forum}.schedule.phases")
|
|
|
|
|
last_changed = db.read(f"{forum}.schedule.last_phase_changing_date")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if None in (phase, last_changed, phases):
|
|
|
|
|
return
|
|
|
|
|
now = date
|
|
|
|
|
weekday = now.weekday()
|
|
|
|
|
if not (now > last_changed and weekday == 0):
|
|
|
|
|
return
|
|
|
|
|
phase = (phase + 1) % phases
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.schedule.phase", phase)
|
|
|
|
|
db.write(f"{forum}.schedule.last_phase_changing_date", now)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
def get_status(forum: int, now: dt.datetime = None) -> bool:
|
|
|
|
|
"Returns forum's duty status. Sundays are free."
|
|
|
|
|
if now is None:
|
|
|
|
|
now = get_time(forum).date()
|
2023-10-29 09:30:36 +01:00
|
|
|
|
weekday = now.weekday()
|
|
|
|
|
if weekday == 6:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
return False
|
2023-12-18 19:24:25 +01:00
|
|
|
|
phase = db.read(f"{forum}.schedule.phase", 0)
|
|
|
|
|
work_days = db.read(f"{forum}.schedule.work_days", [])
|
|
|
|
|
skip_days = db.read(f"{forum}.schedule.skip_days", [])
|
|
|
|
|
days = db.read(f"{forum}.schedule.days", ([0] * 6, [0] * 6))
|
|
|
|
|
is_active = db.read(f"{forum}.is_active", False)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if is_active:
|
|
|
|
|
if now in work_days:
|
|
|
|
|
return True
|
|
|
|
|
if now in skip_days:
|
2023-10-31 10:24:55 +01:00
|
|
|
|
return 0
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if days[phase][weekday]:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
def get_chat(
|
|
|
|
|
message: telebot.types.Message | int, start: bool = False
|
|
|
|
|
) -> telebot.types.Message | None:
|
|
|
|
|
"Returns forum's root message or user's message if chat is not a forum."
|
|
|
|
|
if isinstance(message, int):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
is_forum = True
|
|
|
|
|
forum = message
|
|
|
|
|
else:
|
|
|
|
|
is_forum = message.chat.is_forum
|
|
|
|
|
forum = message.chat.id
|
2023-12-13 19:41:19 +01:00
|
|
|
|
if (db.read(str(forum)) is None or not is_forum) and not start:
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return None
|
2023-12-18 19:24:25 +01:00
|
|
|
|
chat = db.read(f"{forum}.settings.chat")
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if isinstance(message, int):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return chat
|
|
|
|
|
if (chat is None) or (chat.id == message.reply_to_message.id):
|
|
|
|
|
return message.reply_to_message if is_forum else message
|
2023-12-12 14:01:52 +01:00
|
|
|
|
return None
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def check_if_admin(message: telebot.types.Message) -> bool | None:
|
|
|
|
|
"Checks if the message is sent by the forum's admin."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
2023-12-18 19:24:25 +01:00
|
|
|
|
admin = db.read(f"{forum}.settings.admin")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if admin is None:
|
|
|
|
|
return True
|
|
|
|
|
if admin["id"] is None:
|
|
|
|
|
return admin["username"] == message.from_user.username
|
|
|
|
|
return admin["id"] == message.from_user.id
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def mention(forum: int, uid: int) -> str | None:
|
|
|
|
|
"Returns markdown formatted string with user's mention."
|
|
|
|
|
uid = str(uid)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.people.{uid}") is None:
|
|
|
|
|
stderr.write(f"Пользователя с ID {uid} нет в базе.\n")
|
2023-12-18 12:01:00 +01:00
|
|
|
|
return None
|
2023-12-12 14:01:52 +01:00
|
|
|
|
return (
|
2023-12-19 06:25:42 +01:00
|
|
|
|
f"[{db.read(f'{forum}.people.{uid}.name')} "
|
|
|
|
|
+ f"{db.read(f'{forum}.people.{uid}.surname')}](tg://user?id={uid})"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def find_uids(forum: int, s: str) -> list | None:
|
|
|
|
|
"Find user's id by nickname, name or surname."
|
2023-12-18 19:24:25 +01:00
|
|
|
|
people = db.read(f"{forum}.people")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if people is None:
|
|
|
|
|
return None
|
2023-12-18 13:57:19 +01:00
|
|
|
|
if len(s) > 0:
|
|
|
|
|
if s[0] == "@":
|
|
|
|
|
s = s[1:]
|
2023-12-18 19:24:25 +01:00
|
|
|
|
f = list(filter(lambda x: s in people[x].values(), people.keys()))
|
2023-12-18 13:57:19 +01:00
|
|
|
|
else:
|
|
|
|
|
f = list(people.keys())
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if len(f) == 0:
|
|
|
|
|
return None
|
|
|
|
|
return f
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def format_user_info(forum: int, uid: int) -> str:
|
|
|
|
|
"Returns markdown formatted string with all user's info by their id."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
uid = str(uid)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
person = db.read(f"{forum}.people.{uid}")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if person is None:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
return ""
|
|
|
|
|
r = ""
|
2023-12-18 19:24:25 +01:00
|
|
|
|
r += f"\\#{uid}\n"
|
2023-10-27 15:31:53 +02:00
|
|
|
|
for k, i in person.items():
|
2023-12-18 19:24:25 +01:00
|
|
|
|
r += f"‖ {k} \\= {telebot.formatting.escape_markdown(i)}\n"
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return r
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def prepend_user(forum: int, ulist_s: str, uid: int) -> None:
|
|
|
|
|
"Inserts user id at the start of provided db list in forum's context."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
uid = str(uid)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
ulist = db.read(f"{forum}.{ulist_s}", [])
|
2023-12-04 10:04:07 +01:00
|
|
|
|
ulist = list(set([uid] + ulist))
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.{ulist_s}", ulist)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def append_user(forum: int, ulist_s: str, uid: int) -> None:
|
|
|
|
|
"Inserts user id at the end of provided db list in forum's context."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
uid = str(uid)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
ulist = db.read(f"{forum}.{ulist_s}", [])
|
2023-12-04 10:04:07 +01:00
|
|
|
|
ulist = list(set(ulist + [uid]))
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.{ulist_s}", ulist)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def pop_user(forum: int, ulist_s: str) -> dict | None:
|
|
|
|
|
"Removes user id from the start of provided db list in forum's context. Returns user id."
|
2023-12-18 19:24:25 +01:00
|
|
|
|
ulist = db.read(f"{forum}.{ulist_s}", [])
|
2023-12-04 10:04:07 +01:00
|
|
|
|
r = None
|
|
|
|
|
if len(ulist) > 0:
|
|
|
|
|
r = ulist.pop(0)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.{ulist_s}", ulist)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
return r
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def insert_user_in_current_order(forum: int, uid: int) -> bool:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Inserts user id into current order list."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
uid = str(uid)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
order = db.read(f"{forum}.rookies.order", [])
|
|
|
|
|
people = db.read(f"{forum}.people", {})
|
|
|
|
|
current = db.read(f"{forum}.rookies.current")
|
2023-12-04 10:04:07 +01:00
|
|
|
|
if uid not in people:
|
|
|
|
|
return False
|
2023-12-12 14:01:52 +01:00
|
|
|
|
order = dict(map(lambda x: (x, people[x]), order))
|
2023-12-04 10:04:07 +01:00
|
|
|
|
if current is not None:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
order = dict(
|
|
|
|
|
sorted(
|
|
|
|
|
list(order.items()) + [(current, people[current])],
|
|
|
|
|
key=lambda item: item[1]["surname"],
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
order = dict(
|
|
|
|
|
sorted(
|
|
|
|
|
list(order.items()) + [uid, people[uid]],
|
|
|
|
|
key=lambda item: item[1]["surname"],
|
|
|
|
|
)
|
|
|
|
|
)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
pos = list(order.keys()).index(current)
|
|
|
|
|
if pos == 0:
|
|
|
|
|
return False
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.order", list(order.keys())[1:])
|
2023-12-18 12:01:00 +01:00
|
|
|
|
return True
|
2023-12-04 10:04:07 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def parse_dates(forum: int, args: typing.Iterable) -> list | str:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"""
|
|
|
|
|
Translates strings into dates in forum's context.
|
|
|
|
|
Returns problematic string if it couldn't be parsed.
|
|
|
|
|
"""
|
2023-12-10 17:37:24 +01:00
|
|
|
|
dates = []
|
2023-12-12 14:01:52 +01:00
|
|
|
|
cur_date = get_time(forum).date() - dt.timedelta(days=1)
|
|
|
|
|
cur_year = cur_date.year
|
2023-12-10 17:37:24 +01:00
|
|
|
|
for a in args:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
human_relative = {
|
|
|
|
|
"сегодня": get_time(forum).date(),
|
|
|
|
|
"завтра": get_time(forum).date() + dt.timedelta(days=1),
|
|
|
|
|
"послезавтра": get_time(forum).date() + dt.timedelta(days=2),
|
|
|
|
|
"вчера": get_time(forum).date() - dt.timedelta(days=1),
|
|
|
|
|
"позавчера": get_time(forum).date() - dt.timedelta(days=2),
|
|
|
|
|
}.get(a.lower())
|
|
|
|
|
if human_relative is not None:
|
|
|
|
|
dates.append(human_relative)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
d = a.split(".")
|
2023-12-10 17:37:24 +01:00
|
|
|
|
a_dates = []
|
|
|
|
|
if len(d) in (2, 3):
|
|
|
|
|
try:
|
|
|
|
|
d = list(map(int, d))
|
2023-12-12 14:01:52 +01:00
|
|
|
|
except ValueError:
|
2023-12-13 19:41:19 +01:00
|
|
|
|
return a
|
|
|
|
|
if len(d) == 2:
|
|
|
|
|
years = [cur_year, cur_year + 1]
|
|
|
|
|
else:
|
|
|
|
|
years = [(cur_year // 100 * 100) + d[2] if (d[2] < 100) else d[2]]
|
|
|
|
|
for y in years:
|
|
|
|
|
try:
|
|
|
|
|
a_dates.append(dt.datetime(y, d[1], d[0]).date())
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
a_dates = sorted(
|
|
|
|
|
filter(
|
|
|
|
|
lambda x: cur_date + dt.timedelta(days=120) > x > cur_date,
|
|
|
|
|
a_dates,
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
)
|
|
|
|
|
if len(a_dates) == 0:
|
|
|
|
|
return a
|
2023-12-10 17:37:24 +01:00
|
|
|
|
else:
|
|
|
|
|
return a
|
2023-12-13 19:41:19 +01:00
|
|
|
|
dates.append(a_dates[0])
|
2023-12-10 17:37:24 +01:00
|
|
|
|
return dates
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def mod_days(message: telebot.types.Message, target: str, neighbour: str) -> None:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Helper function to add skip and work days."
|
2023-12-10 17:37:24 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) == 0:
|
|
|
|
|
dates = [get_time(forum).date()]
|
|
|
|
|
else:
|
|
|
|
|
dates = parse_dates(forum, args)
|
2023-12-18 12:01:00 +01:00
|
|
|
|
if isinstance(dates, str):
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
telebot.formatting.escape_markdown(dates)
|
|
|
|
|
+ " — это точно дата из ближайшего будущего?",
|
|
|
|
|
)
|
2023-12-10 17:37:24 +01:00
|
|
|
|
return
|
|
|
|
|
if dates is None:
|
|
|
|
|
bot.reply_to(chat, "Нечего добавлять")
|
|
|
|
|
return
|
2023-12-13 19:41:19 +01:00
|
|
|
|
t = db.read(target)
|
2023-12-10 17:37:24 +01:00
|
|
|
|
if t is None:
|
|
|
|
|
t = []
|
2023-12-13 19:41:19 +01:00
|
|
|
|
n = db.read(neighbour)
|
2023-12-10 17:37:24 +01:00
|
|
|
|
if n is None:
|
|
|
|
|
n = []
|
2023-12-13 19:41:19 +01:00
|
|
|
|
db.write(neighbour, list(filter(lambda x: x not in dates, n)))
|
|
|
|
|
db.write(target, list(sorted(set(t + dates))))
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-12-10 17:37:24 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Добавил "
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
", ".join(
|
|
|
|
|
[".".join(map(str, (d.day, d.month, d.year))) for d in dates]
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-10 17:37:24 +01:00
|
|
|
|
|
2023-12-18 19:24:25 +01:00
|
|
|
|
antispam_tmo = tmo.Timeout(10 * 60)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def antispam(
|
|
|
|
|
message: telebot.types.Message, chat: telebot.types.Message, forum: int
|
|
|
|
|
) -> bool:
|
|
|
|
|
"Removes frequent non admin's commands."
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
return False
|
2023-12-18 19:24:25 +01:00
|
|
|
|
antispam_tmo.period = db.read(f"{forum}.settings.antispam.period", 600)
|
|
|
|
|
return antispam_tmo.check(
|
2023-12-13 19:41:19 +01:00
|
|
|
|
lambda: bot.reply_to(chat, "*Хватит спамить\\!\\!\\!*"),
|
|
|
|
|
lambda: bot.delete_message(forum, message.id),
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["start"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def start_bot(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to print kickstart info."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
2023-12-12 14:01:52 +01:00
|
|
|
|
chat = get_chat(message, True)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if message.chat.is_forum:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Привет\\! Я бот для управления дежурствами и напоминания о них\\."
|
|
|
|
|
+ " Напиши /link, чтобы привязать комнату\\.",
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
else:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Я работаю только на форумах \\(супергруппах с комнатами\\)\\."
|
|
|
|
|
+ " Пригласи меня в один из них и напиши /start",
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["help"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def get_help(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to print info about all of the available commands."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
""
|
|
|
|
|
+ "/admin \\[@\\] — переопределить *админа*\n"
|
|
|
|
|
+ "/link — привязать комнату\n"
|
|
|
|
|
+ "/unlink — отвязать комнату\n"
|
|
|
|
|
+ "/cleaner 0/1 — удалять команды\n"
|
|
|
|
|
+ "/new @ Имя Фамилия — добавить ~салагу~ студента\n"
|
|
|
|
|
+ "/readd \\# @ — если студент поменял ник, это нужно отметить\n"
|
|
|
|
|
+ "/remind — напомнить дежурным подежурить, а новичкам отметится\n"
|
|
|
|
|
+ "/forget — забыть всех новичков\n"
|
|
|
|
|
+ "/del @/\\#/Имя — убрать студента\n"
|
|
|
|
|
+ "/list \\[@/\\#/Имя\\] — получить информацию по имени/фамиили\n"
|
|
|
|
|
+ "/purge — удалить форум из базы *\\!\\!\\!ОПАСНО\\!\\!\\!*\n"
|
|
|
|
|
+ "/timezone ±n — установить часовой пояс по UTC\n"
|
2023-12-18 19:24:25 +01:00
|
|
|
|
+ "/days \\*\\*\\*\\*\\*\\* \\*\\*\\*\\*\\*\\*"
|
|
|
|
|
+ " \\(\\* \\= \\[\\0/1\\]\\) — задать дни недели \\(ПН\\-СБ\\),"
|
|
|
|
|
+ " когда необходимо дежурство, отдельно для числителей и знаменателей\n"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ "/calendar \\[Числитель/знаменатель\\] — показать календарь дежурств\n"
|
2023-12-18 19:24:25 +01:00
|
|
|
|
+ "/phase \\[Числитель/знаменатель\\] — узнать или скорректировать"
|
|
|
|
|
+ " фазу текущей недели\n"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ "/skip \\[00\\.00\\] — пропустить сегодняшний или заданный день\n"
|
|
|
|
|
+ "/work \\[00\\.00\\] — поработать в сегодняшнем или заданном дне\n"
|
|
|
|
|
+ "/honor \\[\\-\\]@ — пропуск следующего дежурства, так как студент молодец\n"
|
|
|
|
|
+ "/sick @ \\[дата\\] — пропуск дежурства по причине болезни\n"
|
|
|
|
|
+ "/force \\[\\-\\]@ — провинившийся дежурит как только так сразу\n"
|
|
|
|
|
+ "/order — посмотреть очередь дежурств\n"
|
|
|
|
|
+ "/stop — остановить дежурства\n"
|
|
|
|
|
+ "/begin \\[@\\] — начать сначала с определённого студента",
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-10 17:37:24 +01:00
|
|
|
|
if __debug__:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-10 17:37:24 +01:00
|
|
|
|
def pretty(d, indent=0):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Print pretty dict."
|
2023-12-12 14:01:52 +01:00
|
|
|
|
for key, value in d.items():
|
2023-12-18 19:24:25 +01:00
|
|
|
|
stderr.write(" " * indent + f"{key}\n")
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
pretty(value, indent + 1)
|
|
|
|
|
else:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
stderr.write(" " * (indent + 1) + f"{value}\n")
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["info"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def info(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to print db."
|
2023-11-23 15:31:06 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message, True)
|
|
|
|
|
if chat is not None:
|
2023-12-10 17:37:24 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
if message.from_user.username == db.read("about.host"):
|
|
|
|
|
pretty(db.read(""))
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["exec"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def exec_bot(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to eval python code."
|
2023-11-23 15:31:06 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message, True)
|
|
|
|
|
if chat is not None:
|
2023-12-13 19:41:19 +01:00
|
|
|
|
if message.from_user.username == db.read("about.host"):
|
|
|
|
|
try:
|
|
|
|
|
result = ast.literal_eval(" ".join(message.text.split(" ")[1:]))
|
|
|
|
|
# Disabling W0718 because everything can be excepted with eval
|
|
|
|
|
except Exception as e: # pylint: disable=broad-exception-caught
|
2023-12-18 19:24:25 +01:00
|
|
|
|
bot.reply_to(chat, f"Ошибка выполнения: {e}")
|
2023-12-13 19:41:19 +01:00
|
|
|
|
return
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
2023-12-13 19:41:19 +01:00
|
|
|
|
telebot.formatting.escape_markdown(str(result)),
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-12-10 17:37:24 +01:00
|
|
|
|
else:
|
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["backup"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def backup_db(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to backup database."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message, True)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
if message.from_user.username == db.read("about.host"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) == 0:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
args.append("")
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.save(f"{args[0]}.backup.db")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["restore"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def restore_db(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to restore from backup"
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message, True)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
if message.from_user.username == db.read("about.host"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) == 0:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
args.append("")
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.load(f"{args[0]}.backup.db")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["link"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def link(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to link forum's room."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message, True)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.settings.chat", message.reply_to_message)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
message.reply_to_message,
|
|
|
|
|
'Комната "'
|
2023-12-18 19:24:25 +01:00
|
|
|
|
+ f"{message.reply_to_message.forum_topic_created.name}"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ '" привязана\\. '
|
|
|
|
|
+ "Чек-лист того, что нужно написать в первую очередь:\n"
|
|
|
|
|
+ "/admin \\[@\\], чтобы задать админа\n"
|
|
|
|
|
+ "/help, чтобы узнать, что я умею\n"
|
|
|
|
|
+ "/tz, чтобы задать часовой пояс\n"
|
|
|
|
|
+ "/phase \\[Числитель/знаменатель\\], чтобы задать недельную фазу\n"
|
2023-12-18 19:24:25 +01:00
|
|
|
|
+ "/days \\*\\*\\*\\*\\*\\* \\*\\*\\*\\*\\*\\* \\(\\* \\= \\[\\0/1\\]\\),"
|
|
|
|
|
+ " чтобы задать дни дежурств\n"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ "/new @ Имя Фамилия, чтобы добавить ~салаг~ студетов\n"
|
|
|
|
|
+ "/begin \\[@\\], чтобы начать дежурство",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["unlink"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def unlink(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to unlink room."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.settings.chat", None)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(chat, "Комната отвязана")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["purge"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def purge_db_people(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to clear info about people from database."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
if message.text == "/purge ДА УДАЛЯЙ ДАВАЙ":
|
2023-12-13 19:41:19 +01:00
|
|
|
|
db.write(str(forum), None)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(chat, "База студентов удалена")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
else:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Чтобы подтвердить свои благие намерения, напиши\n"
|
|
|
|
|
+ "`/purge ДА УДАЛЯЙ ДАВАЙ`",
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["forget"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def forget_db_pending(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to forget unregistered newbies."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
empty = db.read(f"{forum}.pending") is None
|
|
|
|
|
db.write(f"{forum}.pending", None)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"База новичков удалена"
|
|
|
|
|
+ (", хоть и была уже пустая…" if empty else ""),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["admin"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def set_admin(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to register admin."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
2023-12-12 14:01:52 +01:00
|
|
|
|
admin = [i for i in message.text.split() if i[0] == "@"]
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if len(admin) == 0:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
uid = db.read(f"{forum}.settings.admin.id")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if uid == message.from_user.id:
|
|
|
|
|
bot.reply_to(chat, "Ты уже тут главный")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.settings.admin.id", message.from_user.id)
|
|
|
|
|
db.write(f"{forum}.settings.admin.username", message.from_user.username)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
2023-12-18 19:24:25 +01:00
|
|
|
|
chat, f"Рад познакомиться, {message.from_user.first_name}\\!"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
else:
|
|
|
|
|
admin = admin[0][1:]
|
2023-12-18 19:24:25 +01:00
|
|
|
|
uadmin = db.read(f"{forum}.settings.admin.username")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if uadmin == admin:
|
|
|
|
|
bot.reply_to(chat, "Ты уже тут главный")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.settings.admin.id", None)
|
|
|
|
|
db.write(f"{forum}.settings.admin.username", admin)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Теперь @"
|
|
|
|
|
+ telebot.formatting.escape_markdown(admin)
|
|
|
|
|
+ " тут царь и бог\\!\n"
|
|
|
|
|
+ "Напиши /admin в чат, чтобы я знал тебя в лицо",
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["list"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def list_users(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to list users."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
s = message.text.split()[1:]
|
|
|
|
|
if len(s) == 0:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
s = [""]
|
2023-10-27 15:31:53 +02:00
|
|
|
|
r = []
|
|
|
|
|
for i in s:
|
|
|
|
|
f = find_uids(forum, i)
|
|
|
|
|
if f is not None:
|
|
|
|
|
r += f
|
|
|
|
|
if len(r) == 0:
|
|
|
|
|
bot.reply_to(chat, "Никого не нашёл")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
res = []
|
|
|
|
|
for i in r:
|
|
|
|
|
res.append(format_user_info(forum, i))
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(chat, "\n".join(res))
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["remind"])
|
2023-12-18 19:24:25 +01:00
|
|
|
|
def remind_users(message: telebot.types.Message | int):
|
|
|
|
|
"Command to remind about duty or about registration. Can be called automatically."
|
|
|
|
|
auto, forum = (
|
|
|
|
|
(True, message) if isinstance(message, int) else (False, message.chat.id)
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if not auto and antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if auto or check_if_admin(message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
pending = db.read(f"{forum}.pending", {})
|
2023-10-27 15:31:53 +02:00
|
|
|
|
r = ""
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if len(pending.keys()) != 0:
|
2023-10-27 15:31:53 +02:00
|
|
|
|
for i in pending.keys():
|
2023-12-18 19:24:25 +01:00
|
|
|
|
r += f"@{telebot.formatting.escape_markdown(i)}\n"
|
|
|
|
|
r += "нужно нажать /new\n\n"
|
2023-10-27 15:31:53 +02:00
|
|
|
|
status = get_status(forum)
|
|
|
|
|
if status:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
current = db.read(f"{forum}.rookies.current")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if current is not None:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
rookie = db.read(f"{forum}.people.{current}")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if rookie is not None:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
r += f"{mention(forum, current)} сегодня дежурит"
|
|
|
|
|
if not auto and db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if not auto and r == "":
|
|
|
|
|
bot.reply_to(chat, "Дежурств сегодня нет")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
else:
|
|
|
|
|
bot.reply_to(chat, r)
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["del"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def del_person(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to remove person from forum."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) < 1:
|
|
|
|
|
bot.reply_to(chat, "Давай хоть кого\\-нибудь удалим")
|
|
|
|
|
return
|
|
|
|
|
if len(args) > 1:
|
|
|
|
|
bot.reply_to(chat, "Давай удалять их по очереди")
|
|
|
|
|
return
|
|
|
|
|
user = args[0]
|
|
|
|
|
if user[1:] == bot.get_me().username:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Ты сейчас быканул\\?")
|
|
|
|
|
return
|
|
|
|
|
f = find_uids(forum, user)
|
|
|
|
|
if f is None:
|
|
|
|
|
bot.reply_to(chat, "Никого не нашёл")
|
|
|
|
|
return
|
|
|
|
|
if len(f) > 1:
|
|
|
|
|
bot.reply_to(chat, "Немогу определиться…")
|
|
|
|
|
list_users(message)
|
|
|
|
|
bot.reply_to(chat, "Конкретезируй плиз")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
bot.reply_to(chat, f"Скатертью дорога, {mention(forum, f[0])}\\!")
|
|
|
|
|
db.pop(f"{forum}.people.{f[0]}")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["cleaner"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def cleaner(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to set commands cleaner status."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) != 1:
|
|
|
|
|
bot.reply_to(chat, "Нужно указать `0` или `1`")
|
|
|
|
|
return
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if args[0] not in ("0", "1"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Нужно указать `0` или `1`")
|
|
|
|
|
return
|
2023-12-12 14:01:52 +01:00
|
|
|
|
state = args[0] == "1"
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.settings.delete_messages", state)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if state:
|
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Очистка команд "
|
|
|
|
|
+ ("включена" if state else "отключена")
|
|
|
|
|
+ (
|
|
|
|
|
"\nЧтобы чистка работала, мне нужно дать право удалять сообщения"
|
|
|
|
|
if state
|
|
|
|
|
else ""
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["new"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def add_new(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to add new person to forum."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) == 0:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
pending = db.read(f"{forum}.pending")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if pending is None:
|
|
|
|
|
antispam(message, chat, forum)
|
|
|
|
|
return
|
|
|
|
|
user = message.from_user.username
|
|
|
|
|
if user not in pending.keys():
|
|
|
|
|
antispam(message, chat, forum)
|
|
|
|
|
return
|
|
|
|
|
uid = str(message.from_user.id)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.people.{uid}.username", user)
|
|
|
|
|
db.write(f"{forum}.people.{uid}.name", pending[user]["name"])
|
|
|
|
|
db.write(f"{forum}.people.{uid}.surname", pending[user]["surname"])
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
bot.reply_to(chat, f"Рад познакомиться, {mention(forum, uid)}\\!")
|
|
|
|
|
db.pop(f"{forum}.pending.{user}")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
elif check_if_admin(message):
|
|
|
|
|
if len(args) != 3:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat, "Нужно указать @, имя и фамилию\nНе больше, не меньше"
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return
|
|
|
|
|
user = args[0]
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if user[0] != "@":
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Пользователей нужно помянуть через `@`")
|
|
|
|
|
return
|
|
|
|
|
user = user[1:]
|
|
|
|
|
if user == bot.get_me().username:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Я тебе не салага\\!")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
people = db.read(f"{forum}.people", [])
|
|
|
|
|
pending = db.read(f"{forum}.pending", {})
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if any(
|
|
|
|
|
[people[i]["username"] == user for i in people]
|
|
|
|
|
+ [user in pending.keys()]
|
|
|
|
|
):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Пользователь с таким ником уже в базе")
|
|
|
|
|
return
|
|
|
|
|
name = args[1]
|
|
|
|
|
surname = args[2]
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.pending.{user}.name", name)
|
|
|
|
|
db.write(f"{forum}.pending.{user}.surname", surname)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Добро пожаловать, @"
|
|
|
|
|
+ telebot.formatting.escape_markdown(user)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
+ f" \\({name} {surname}\\)\\!\n"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ "Первое твоё обязательство — написать /new",
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["readd"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def readd(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to change person's nickname."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) != 2:
|
|
|
|
|
bot.reply_to(chat, "Нужно указать id и новый ник\nНе больше, не меньше")
|
|
|
|
|
return
|
|
|
|
|
uid = args[0]
|
|
|
|
|
try:
|
|
|
|
|
int(uid)
|
2023-12-13 19:41:19 +01:00
|
|
|
|
except ValueError:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat, "ID — это число\nНа буквах пока считать не научились"
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.people.{uid}") is None:
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Такого пользователя нет в базе")
|
|
|
|
|
return
|
|
|
|
|
user = args[1]
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if user[0] != "@":
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Пользователей нужно помянуть через `@`")
|
|
|
|
|
return
|
|
|
|
|
user = user[1:]
|
|
|
|
|
if user == bot.get_me().username:
|
|
|
|
|
bot.reply_to(chat, "Я тебе не салага\\!")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.people.{uid}.username", user)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
2023-12-18 19:24:25 +01:00
|
|
|
|
f"@{telebot.formatting.escape_markdown(user)}"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ " от меня не скроется\\!",
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["days"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def set_days(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to define duty days."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) != 2:
|
|
|
|
|
bot.reply_to(chat, "Мне нужно два двоичных числа")
|
|
|
|
|
return
|
|
|
|
|
num1 = args[0]
|
|
|
|
|
num2 = args[1]
|
|
|
|
|
if len(num1) != 6:
|
|
|
|
|
bot.reply_to(chat, "В первом числе не 6 цифр")
|
|
|
|
|
return
|
|
|
|
|
if len(num2) != 6:
|
|
|
|
|
bot.reply_to(chat, "Во втором числе не 6 цифр")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if not all(i in ("0", "1") for i in num1):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Цифры двоичные должны быть в первом")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if not all(i in ("0", "1") for i in num2):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.reply_to(chat, "Цифры двоичные должны быть во втором")
|
|
|
|
|
return
|
2023-12-13 19:41:19 +01:00
|
|
|
|
db.write(
|
2023-12-18 19:24:25 +01:00
|
|
|
|
f"{forum}.schedule.days",
|
2023-12-12 14:01:52 +01:00
|
|
|
|
(
|
2023-12-18 19:24:25 +01:00
|
|
|
|
[num1[i] == "1" for i in range(6)],
|
|
|
|
|
[num2[i] == "1" for i in range(6)],
|
2023-12-12 14:01:52 +01:00
|
|
|
|
),
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
message.text = ""
|
|
|
|
|
calendar(message)
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["calendar"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def calendar(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to display duty calendar."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) > 1:
|
|
|
|
|
bot.reply_to(chat, "Многа букав")
|
|
|
|
|
return
|
|
|
|
|
en1, en2 = False, False
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
en1 = args[0].lower() in "числитель"
|
|
|
|
|
en2 = args[0].lower() in "знаменатель"
|
|
|
|
|
if en1 == en2 == False:
|
|
|
|
|
en1, en2 = True, True
|
2023-12-18 19:24:25 +01:00
|
|
|
|
days = db.read(f"{forum}.schedule.days", ([False] * 6, [False] * 6))
|
|
|
|
|
skip = db.read(f"{forum}.schedule.skip_days", [])
|
|
|
|
|
work = db.read(f"{forum}.schedule.work_days", [])
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"`\\|#\\|Пн \\|Вт \\|Ср \\|Чт \\|Пт \\|Сб \\|\n"
|
|
|
|
|
+ (
|
|
|
|
|
" Ч"
|
|
|
|
|
+ "".join([" " + ("X" if days[0][i] else " ") + " " for i in range(6)])
|
|
|
|
|
+ "\n"
|
|
|
|
|
if en1
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
+ (
|
|
|
|
|
" З"
|
|
|
|
|
+ "".join([" " + ("X" if days[1][i] else " ") + " " for i in range(6)])
|
|
|
|
|
if en2
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
+ "`"
|
|
|
|
|
+ "\n\n"
|
|
|
|
|
+ "*Пропуски:*\n"
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
"\n".join([".".join(map(str, (d.day, d.month, d.year))) for d in skip])
|
|
|
|
|
)
|
|
|
|
|
+ "\n\n"
|
|
|
|
|
+ "*Рабочие:*\n"
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
"\n".join([".".join(map(str, (d.day, d.month, d.year))) for d in work])
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["skip"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def set_skip_days(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to set skipped days."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
2023-12-18 19:24:25 +01:00
|
|
|
|
mod_days(message, f"{forum}.schedule.skip_days", f"{forum}.schedule.work_days")
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["work"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def set_work_days(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to set work days."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
2023-12-18 19:24:25 +01:00
|
|
|
|
mod_days(message, f"{forum}.schedule.work_days", f"{forum}.schedule.skip_days")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["stop"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def stop_queue(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to stop duty."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
status = db.read(f"{forum}.is_active")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if status is None or not status:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Держурство уже не идёт")
|
|
|
|
|
return
|
|
|
|
|
status = False
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.is_active", status)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Держурство остановлено")
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["honor"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def add_honor(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to add honored person."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) != 1:
|
|
|
|
|
bot.reply_to(chat, "Нужно указать человека")
|
|
|
|
|
return
|
|
|
|
|
user = args[0]
|
|
|
|
|
if user[1:] == bot.get_me().username:
|
|
|
|
|
bot.reply_to(chat, "Рад, что всегда в почёте")
|
|
|
|
|
return
|
|
|
|
|
f = find_uids(forum, user)
|
|
|
|
|
if f is None:
|
|
|
|
|
bot.reply_to(chat, "Никого не нашёл")
|
|
|
|
|
return
|
|
|
|
|
if len(f) > 1:
|
|
|
|
|
bot.reply_to(chat, "Немогу определиться…")
|
|
|
|
|
list_users(message)
|
|
|
|
|
bot.reply_to(chat, "Конкретезируй плиз")
|
|
|
|
|
return
|
|
|
|
|
user = f[0]
|
2023-12-18 19:24:25 +01:00
|
|
|
|
force = db.read(f"{forum}.rookies.force_order", [])
|
|
|
|
|
honor = db.read(f"{forum}.rookies.honor_order", [])
|
|
|
|
|
people = db.read(f"{forum}.people")
|
2023-12-12 14:01:52 +01:00
|
|
|
|
honor.append(user)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
for i in people:
|
|
|
|
|
if (i in force) and (i in honor):
|
|
|
|
|
force.remove(i)
|
|
|
|
|
honor.remove(i)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.force_order", force)
|
|
|
|
|
db.write(f"{forum}.rookies.honor_order", honor)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-12-04 10:04:07 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Вы в почёте, "
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
2023-12-19 06:19:57 +01:00
|
|
|
|
f"{people[user]['name']} {people[user]['surname']}"
|
2023-12-19 06:25:42 +01:00
|
|
|
|
),
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
|
|
|
|
|
2023-12-04 10:04:07 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["force"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def add_force(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to add guilty person."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) != 1:
|
|
|
|
|
bot.reply_to(chat, "Нужно указать человека")
|
|
|
|
|
return
|
|
|
|
|
user = args[0]
|
|
|
|
|
if user[1:] == bot.get_me().username:
|
|
|
|
|
bot.reply_to(chat, "Рад, что всегда в почёте")
|
|
|
|
|
return
|
|
|
|
|
f = find_uids(forum, user)
|
|
|
|
|
if f is None:
|
|
|
|
|
bot.reply_to(chat, "Никого не нашёл")
|
|
|
|
|
return
|
|
|
|
|
if len(f) > 1:
|
|
|
|
|
bot.reply_to(chat, "Немогу определиться…")
|
|
|
|
|
list_users(message)
|
|
|
|
|
bot.reply_to(chat, "Конкретезируй плиз")
|
|
|
|
|
return
|
|
|
|
|
user = f[0]
|
2023-12-18 19:24:25 +01:00
|
|
|
|
force = db.read(f"{forum}.rookies.force_order", [])
|
|
|
|
|
honor = db.read(f"{forum}.rookies.honor_order", [])
|
|
|
|
|
people = db.read(f"{forum}.people")
|
2023-12-12 14:01:52 +01:00
|
|
|
|
force.append(user)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
for i in people:
|
|
|
|
|
if (i in force) and (i in honor):
|
|
|
|
|
force.remove(i)
|
|
|
|
|
honor.remove(i)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.force_order", force)
|
|
|
|
|
db.write(f"{forum}.rookies.honor_order", honor)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-12-04 10:04:07 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Отрабатывай, "
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
2023-12-19 06:25:42 +01:00
|
|
|
|
f"{people[user]['name']} {people[user]['surname']}"
|
|
|
|
|
),
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["sick"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def add_sick(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to add sick person."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) not in (1, 2):
|
|
|
|
|
bot.reply_to(chat, "Нужно указать человека и дату выздоровления")
|
|
|
|
|
return
|
|
|
|
|
user = args[0]
|
|
|
|
|
if user[1:] == bot.get_me().username:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-12-04 10:04:07 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Ты сейчас быканул\\?")
|
|
|
|
|
return
|
|
|
|
|
f = find_uids(forum, user)
|
|
|
|
|
if f is None:
|
|
|
|
|
bot.reply_to(chat, "Никого не нашёл")
|
|
|
|
|
return
|
|
|
|
|
if len(f) > 1:
|
|
|
|
|
bot.reply_to(chat, "Немогу определиться…")
|
|
|
|
|
list_users(message)
|
|
|
|
|
bot.reply_to(chat, "Конкретезируй плиз")
|
|
|
|
|
return
|
|
|
|
|
user = f[0]
|
|
|
|
|
if len(args) > 1:
|
2023-12-04 14:21:21 +01:00
|
|
|
|
dates = parse_dates(forum, args[1:])
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if isinstance(dates, str):
|
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
telebot.formatting.escape_markdown(dates)
|
|
|
|
|
+ " — это точно дата из ближайшего будущего?",
|
|
|
|
|
)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
return
|
|
|
|
|
if dates is None:
|
|
|
|
|
bot.reply_to(chat, "Нечего добавлять")
|
|
|
|
|
return
|
|
|
|
|
date = dates[0]
|
|
|
|
|
else:
|
|
|
|
|
date = get_time(forum).date() + dt.timedelta(days=30)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
sicks = db.read(f"{forum}.rookies.sick_order", {})
|
|
|
|
|
people = db.read(f"{forum}.people")
|
2023-12-04 10:04:07 +01:00
|
|
|
|
sicks[user] = (date, False)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.sick_order", sicks)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-12-04 10:04:07 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
2023-12-23 14:00:07 +01:00
|
|
|
|
telebot.formatting.escape_markdown(
|
2023-12-19 06:25:42 +01:00
|
|
|
|
f"{people[user]['name']} {people[user]['surname']}"
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
|
|
|
|
+ " болеет до "
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
".".join(map(str, (date.day, date.month, date.year)))
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["order"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def view_order(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to display duty order, honored, guilty and sick people."
|
2023-12-06 07:47:46 +01:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) > 0:
|
|
|
|
|
bot.reply_to(chat, "Многа букав")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
order = db.read(f"{forum}.rookies.order", [])
|
|
|
|
|
force = db.read(f"{forum}.rookies.force_order", [])
|
|
|
|
|
honor = db.read(f"{forum}.rookies.honor_order", [])
|
|
|
|
|
sicks = db.read(f"{forum}.rookies.sick_order", {})
|
|
|
|
|
people = db.read(f"{forum}.people")
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-12-06 07:47:46 +01:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"*Очередь:*\n"
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
"\n".join(
|
2023-12-19 06:25:42 +01:00
|
|
|
|
[f"{people[u]['name']} {people[u]['surname']}" for u in order]
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
+ "\n\n"
|
|
|
|
|
+ "*Почитаемые:*\n"
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
"\n".join(
|
2023-12-19 06:25:42 +01:00
|
|
|
|
[f"{people[u]['name']} {people[u]['surname']}" for u in honor]
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
+ "\n"
|
|
|
|
|
+ "*Виноватые:*\n"
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
"\n".join(
|
2023-12-19 06:25:42 +01:00
|
|
|
|
[f"{people[u]['name']} {people[u]['surname']}" for u in force]
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
+ "\n"
|
|
|
|
|
+ "*Больные:*\n"
|
|
|
|
|
+ telebot.formatting.escape_markdown(
|
|
|
|
|
"\n".join(
|
|
|
|
|
[
|
2023-12-19 06:25:42 +01:00
|
|
|
|
f"{people[u]['name']} {people[u]['surname']} до "
|
2023-12-12 14:01:52 +01:00
|
|
|
|
+ ".".join(
|
|
|
|
|
map(
|
|
|
|
|
str,
|
|
|
|
|
(sicks[u][0].day, sicks[u][0].month, sicks[u][0].year),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
for u in sicks
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["begin"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def begin_queue(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to begin queue."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) > 1:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat, "Нужно указать человека, с которого начнётся дежурство"
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
user = args[0]
|
|
|
|
|
if user[1:] == bot.get_me().username:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Ты сейчас быканул\\?")
|
|
|
|
|
return
|
|
|
|
|
f = find_uids(forum, user)
|
|
|
|
|
if f is None:
|
|
|
|
|
bot.reply_to(chat, "Никого не нашёл")
|
|
|
|
|
return
|
|
|
|
|
if len(f) > 1:
|
|
|
|
|
bot.reply_to(chat, "Немогу определиться…")
|
|
|
|
|
list_users(message)
|
|
|
|
|
bot.reply_to(chat, "Конкретезируй плиз")
|
|
|
|
|
return
|
|
|
|
|
start_with = f[0]
|
|
|
|
|
else:
|
2023-10-28 21:05:45 +02:00
|
|
|
|
start_with = None
|
2023-12-18 19:24:25 +01:00
|
|
|
|
phase = db.read(f"{forum}.schedule.phase")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if phase is None:
|
|
|
|
|
bot.reply_to(chat, "Задай фазу с помощью /phase")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
status = db.read(f"{forum}.is_active")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if status:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
|
|
|
|
bot.reply_to(chat, "Держурство уже идёт")
|
|
|
|
|
return
|
|
|
|
|
status = True
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.is_active", status)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
stack_update(forum, start_with)
|
2023-10-31 10:23:14 +01:00
|
|
|
|
now_date = get_time(forum).date()
|
2023-12-18 19:24:25 +01:00
|
|
|
|
start_with = db.read(f"{forum}.rookies.order", [None])[0]
|
2023-10-29 09:28:01 +01:00
|
|
|
|
if start_with is None:
|
|
|
|
|
bot.reply_to(chat, "Людей нет")
|
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.schedule.last_stack_update_date", now_date)
|
|
|
|
|
db.write(f"{forum}.schedule.last_notification_date", now_date)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Держурство начнёт "
|
2023-12-19 06:25:42 +01:00
|
|
|
|
+ f"{db.read(f'{forum}.people.{start_with}.name')} "
|
|
|
|
|
+ f"{db.read(f'{forum}.people.{start_with}.surname')}",
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=["phase"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def set_phase(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to set start phase."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) > 1:
|
|
|
|
|
bot.reply_to(chat, "Многа букав")
|
|
|
|
|
return
|
2023-12-12 14:01:52 +01:00
|
|
|
|
en2 = False
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
en1 = args[0].lower() in "числитель"
|
|
|
|
|
en2 = args[0].lower() in "знаменатель"
|
|
|
|
|
if en1 == en2:
|
|
|
|
|
bot.reply_to(chat, "Это числитель или знаменатель?")
|
|
|
|
|
return
|
|
|
|
|
now = get_time(forum).date()
|
|
|
|
|
phase = int(bool(en2))
|
|
|
|
|
phases = 2
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.schedule.phase", phase)
|
|
|
|
|
db.write(f"{forum}.schedule.phases", phases)
|
|
|
|
|
db.write(f"{forum}.schedule.last_phase_changing_date", now)
|
|
|
|
|
phase = db.read(f"{forum}.schedule.phase", 0)
|
|
|
|
|
phases = db.read(f"{forum}.schedule.phase", 2)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat, "Текущая неделя: " + ("знаменатель" if phase else "числитель")
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
@bot.message_handler(commands=["timezone", "tz"])
|
2023-12-13 19:41:19 +01:00
|
|
|
|
def set_timezone(message: telebot.types.Message):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Command to set forum's timezone."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
forum = message.chat.id
|
|
|
|
|
chat = get_chat(message)
|
|
|
|
|
if chat is not None:
|
|
|
|
|
if antispam(message, chat, forum):
|
|
|
|
|
return
|
|
|
|
|
if check_if_admin(message):
|
|
|
|
|
args = message.text.split()[1:]
|
|
|
|
|
if len(args) > 1:
|
|
|
|
|
bot.reply_to(chat, "Многа букав")
|
|
|
|
|
return
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
try:
|
|
|
|
|
args[0] = args[0].lower()
|
|
|
|
|
if "utc" in args[0]:
|
|
|
|
|
args[0] = args[0][3:]
|
|
|
|
|
tz = int(args[0])
|
|
|
|
|
if tz not in range(-12, 14 + 1):
|
|
|
|
|
raise ValueError
|
|
|
|
|
except ValueError:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Нужно указать [смещение по UTC](https:"
|
|
|
|
|
+ "//ru.wikipedia.org/wiki/%D0%92%D1%81%D"
|
|
|
|
|
+ "0%B5%D0%BC%D0%B8%D1%80%D0%BD%D0%BE%D0%"
|
|
|
|
|
+ "B5_%D0%BA%D0%BE%D0%BE%D1%80%D0%B4%D0%B"
|
|
|
|
|
+ "8%D0%BD%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%"
|
|
|
|
|
+ "D0%BD%D0%BD%D0%BE%D0%B5_%D0%B2%D1%80%D"
|
|
|
|
|
+ "0%B5%D0%BC%D1%8F)",
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
return
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.settings.timezone", tz)
|
|
|
|
|
tz = db.read(f"{forum}.settings.timezone", 3)
|
|
|
|
|
if db.read(f"{forum}.settings.delete_messages"):
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.delete_message(forum, message.id)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
bot.reply_to(
|
|
|
|
|
chat,
|
|
|
|
|
"Часовой пояс: UTC"
|
|
|
|
|
+ ("\\+" if tz >= 0 else "")
|
|
|
|
|
+ telebot.formatting.escape_markdown(str(tz)),
|
|
|
|
|
)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
def get_hours() -> tuple:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Returns forum's work hours."
|
2023-12-18 13:25:51 +01:00
|
|
|
|
# TODO command to set range
|
2023-12-21 18:05:34 +01:00
|
|
|
|
return tuple(range(8, 20))
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def stack_update(forum: int, force_reset: bool = False) -> None:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"Updates forum's stacks."
|
2023-12-04 10:04:07 +01:00
|
|
|
|
now = get_time(forum)
|
|
|
|
|
now_date = now.date()
|
2023-12-18 19:24:25 +01:00
|
|
|
|
order = db.read(f"{forum}.rookies.order", [])
|
|
|
|
|
force = db.read(f"{forum}.rookies.force_order", [])
|
|
|
|
|
honor = db.read(f"{forum}.rookies.honor_order", [])
|
|
|
|
|
sicks = db.read(f"{forum}.rookies.sick_order", {})
|
|
|
|
|
people = db.read(f"{forum}.people", {})
|
2023-12-23 06:09:34 +01:00
|
|
|
|
to_pop = []
|
2023-12-04 10:04:07 +01:00
|
|
|
|
for i in sicks:
|
|
|
|
|
if now_date >= sicks[i][0]:
|
|
|
|
|
if sicks[i][1]:
|
|
|
|
|
prepend_user(forum, ".rookies.force_order", i)
|
2023-12-23 06:09:34 +01:00
|
|
|
|
to_pop.append(i)
|
|
|
|
|
for i in to_pop:
|
|
|
|
|
sicks.pop(i)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.sick_order", sicks)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
for i in people:
|
|
|
|
|
if (i in force) and (i in honor):
|
|
|
|
|
force.remove(i)
|
|
|
|
|
honor.remove(i)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.force_order", force)
|
|
|
|
|
db.write(f"{forum}.rookies.honor_order", honor)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
for i in order:
|
|
|
|
|
if i not in people.keys():
|
|
|
|
|
order.remove(i)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if len(order) == 0 or force_reset is not False:
|
|
|
|
|
order = list(
|
|
|
|
|
dict(sorted(people.items(), key=lambda item: item[1]["surname"])).keys()
|
|
|
|
|
)
|
|
|
|
|
if force_reset is not False:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.force_order", [])
|
|
|
|
|
db.write(f"{forum}.rookies.honor_order", [])
|
|
|
|
|
db.write(f"{forum}.rookies.sick_order", {})
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if force_reset is not None:
|
|
|
|
|
try:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
order = order[order.index(force_reset) :]
|
2023-12-13 19:41:19 +01:00
|
|
|
|
except ValueError:
|
2023-10-27 15:31:53 +02:00
|
|
|
|
pass
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.order", order)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if len(order) == 0:
|
|
|
|
|
return
|
2023-12-12 14:01:52 +01:00
|
|
|
|
if force_reset is False:
|
2023-12-04 10:04:07 +01:00
|
|
|
|
if len(force) > 0:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.current", pop_user(forum, "rookies.force_order"))
|
2023-12-04 10:04:07 +01:00
|
|
|
|
else:
|
|
|
|
|
current = pop_user(forum, "rookies.order")
|
|
|
|
|
if current in honor:
|
|
|
|
|
honor.remove(current)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.honor_order", honor)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
stack_update(forum)
|
2023-12-23 06:08:02 +01:00
|
|
|
|
elif any((sicks[i][1] is False) and (i == current) for i in sicks):
|
2023-12-04 10:04:07 +01:00
|
|
|
|
skipped = list(sicks[current])
|
|
|
|
|
skipped[1] = True
|
|
|
|
|
sicks[current] = tuple(skipped)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.sick_order", sicks)
|
2023-12-04 10:04:07 +01:00
|
|
|
|
stack_update(forum)
|
|
|
|
|
else:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.rookies.current", pop_user(forum, "rookies.order"))
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def clean_old_dates(date: dt.datetime, array: str) -> None:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
"Removes dates from db's array which are older than `date`."
|
2023-12-13 19:41:19 +01:00
|
|
|
|
a = db.read(array)
|
2023-12-18 13:50:25 +01:00
|
|
|
|
a = list(filter(lambda x: x >= date, a))
|
2023-12-13 19:41:19 +01:00
|
|
|
|
db.write(array, a)
|
2023-11-01 07:15:28 +01:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def update(forum: int) -> None:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
"""
|
|
|
|
|
Updates forum's state:
|
|
|
|
|
· Cleans old skip/work dates.
|
|
|
|
|
· Updates order stack.
|
|
|
|
|
· Notifies about upcoming duty.
|
|
|
|
|
"""
|
2023-10-31 10:23:14 +01:00
|
|
|
|
now = get_time(forum)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
now_date = now.date()
|
|
|
|
|
now_time = now.time()
|
2023-12-18 19:24:25 +01:00
|
|
|
|
last_notif = db.read(f"{forum}.schedule.last_notification_date")
|
|
|
|
|
last_upd_stack = db.read(f"{forum}.schedule.last_stack_update_date")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
is_active = get_status(forum, now_date)
|
2023-12-12 14:01:52 +01:00
|
|
|
|
hours_range = get_hours()
|
2023-10-27 15:31:53 +02:00
|
|
|
|
change_phase(forum, now_date)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
clean_old_dates(now_date, f"{forum}.schedule.work_days")
|
|
|
|
|
clean_old_dates(now_date, f"{forum}.schedule.skip_days")
|
2023-10-27 15:31:53 +02:00
|
|
|
|
if is_active and (last_upd_stack is None or now_date > last_upd_stack):
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write(f"{forum}.schedule.last_stack_update_date", now_date)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
stack_update(forum)
|
|
|
|
|
if now_time.hour in hours_range:
|
|
|
|
|
if last_notif is None or now_date > last_notif:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
stdout.write("Notified\n")
|
|
|
|
|
db.write(f"{forum}.schedule.last_notification_date", now_date)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
remind_users(forum)
|
2023-12-18 19:24:25 +01:00
|
|
|
|
if db.read("about.updatedfrom") != db.read("about.version"):
|
|
|
|
|
db.write("about.updatenotified", True)
|
|
|
|
|
update_notify(forum)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 12:01:00 +01:00
|
|
|
|
def update_notify(forum: int) -> None:
|
2023-12-12 14:01:52 +01:00
|
|
|
|
"Notifies the forum about bot's new version."
|
|
|
|
|
bot.reply_to(
|
|
|
|
|
get_chat(forum),
|
2023-12-31 23:15:15 +01:00
|
|
|
|
# f"Обновился до версии {telebot.formatting.escape_markdown(CURRENT_VERSION)}",
|
2024-01-01 00:26:57 +01:00
|
|
|
|
f"Обновился до версии {telebot.formatting.escape_markdown(CURRENT_VERSION)}\n" \
|
|
|
|
|
+ "Дежурик поздравляет всех бета-тестеров С ноВЫыыыыыМ Г0Йда и напоминает о том," \
|
2023-12-31 23:15:15 +01:00
|
|
|
|
+ " что на каникулах и сессии дежурства продолжаются",
|
2023-12-12 14:01:52 +01:00
|
|
|
|
)
|
2023-12-10 17:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
def process1():
|
2023-12-12 14:01:52 +01:00
|
|
|
|
"The process runs telegram infinite polling."
|
2023-10-27 15:31:53 +02:00
|
|
|
|
bot.infinity_polling(none_stop=True)
|
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-12-18 19:24:25 +01:00
|
|
|
|
p2_tmo = tmo.Timeout(120)
|
|
|
|
|
|
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
def process2():
|
2023-12-12 14:01:52 +01:00
|
|
|
|
"The process updates duty order for every forum once a `period` seconds."
|
2023-12-18 19:24:25 +01:00
|
|
|
|
|
|
|
|
|
def p2():
|
|
|
|
|
"Helper function."
|
|
|
|
|
stdout.write("Process 2 update\n")
|
|
|
|
|
if db.read("about.updatenotified", True):
|
|
|
|
|
db.write("about.updatedfrom", db.read("about.version"))
|
|
|
|
|
for f in db.read("").keys():
|
|
|
|
|
try:
|
|
|
|
|
update(int(f))
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
p2_tmo.period = int(cr.read("settings.notify_period"))
|
2023-10-27 15:31:53 +02:00
|
|
|
|
while True:
|
2023-12-18 19:24:25 +01:00
|
|
|
|
p2_tmo.check(p2, lambda: None)
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-12 14:01:52 +01:00
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
funcs = [process1, process2]
|
2023-12-12 14:01:52 +01:00
|
|
|
|
threads = map(lambda x: Thread(target=x), funcs)
|
|
|
|
|
for thread in threads:
|
|
|
|
|
thread.daemon = True
|
|
|
|
|
thread.start()
|
2023-10-27 15:31:53 +02:00
|
|
|
|
|
2023-12-18 19:24:25 +01:00
|
|
|
|
db.write("about.updatenotified", False)
|
2023-12-18 13:31:28 +01:00
|
|
|
|
db.write("about.updatedfrom", VERSION)
|
|
|
|
|
db.write("about.version", CURRENT_VERSION)
|
2023-12-10 19:40:11 +01:00
|
|
|
|
|
2023-10-27 15:31:53 +02:00
|
|
|
|
while True:
|
|
|
|
|
time.sleep(1)
|