def cure_all(queue: JobQueue, bot: Bot) -> None: # wipe db and ununrestrict all infected users for user in _db.find_all(): # unrestrict all except admins (they are so good) try: # TODO: extract it more properly unmute_perm = ChatPermissions( can_add_web_page_previews=True, can_send_media_messages=True, can_send_other_messages=True, can_send_messages=True, ) bot.restrict_chat_member(get_group_chat_id(), user.id, unmute_perm) logger.debug("user: %s was unrestrict", _get_username(user)) except TelegramError as err: logger.warning("can't unrestrict %s: %s", _get_username(user), err) _db.remove_all() # clean up the jobs queue covid_daily_infection_job: Tuple = queue.get_jobs_by_name( JOB_QUEUE_DAILY_INFECTION_KEY ) if covid_daily_infection_job: covid_daily_infection_job[0].schedule_removal() covid_repeating_coughing_job: Tuple = queue.get_jobs_by_name( JOB_QUEUE_REPEATING_COUGHING_KEY ) if covid_repeating_coughing_job: covid_repeating_coughing_job[0].schedule_removal()
def _longest(update: Update, context: CallbackContext): message = "🍆 🔝🔟 best known lengths 🍆: \n\n" n = 1 for col in _db.get_best_n(10): username = _get_username(col) message += f"{n} → {username}\n" n += 1 result: Optional[Message] = context.bot.send_message( update.effective_chat.id, message, disable_notification=True, ) cleanup_queue_update( context.job_queue, update.message, result, 120, remove_cmd=True, remove_reply=False, )
def random_cough(bot: Bot): users = _db.find_all() message = "" for user in users: # todo: move "_get_username" to commons full_name = _get_username(user) if random() <= RANDOM_COUGH_UNINFECTED_CHANCE: message += f"{full_name} чихнул в пространство \n" if message: bot.send_message(get_group_chat_id(), message)
def random_cough(bot: Bot, queue: JobQueue): users = _db.find_all() message = '' for user in users: _rng = random() # todo: move "_get_username" to commons full_name = _get_username(user) lethaled = _db.is_lethaled(user['_id']) if 'infected_since' in user and 'cured_since' not in user and lethaled is None: chance = RANDOM_COUGH_INFECTED_CHANCE delta_seconds = (datetime.now() - user['infected_since']).total_seconds() delta_days_float = delta_seconds / (60 * 60 * 24) if _rng <= RANDOM_CURE_RATE**(2 - delta_days_float): chance = .0 _db.cure(user['_id']) message += f"{full_name} излечился от коронавируса\n" if _rng <= LETHALITY_RATE * (delta_days_float**delta_days_float): chance = .0 try: _db.add_lethality(user['_id'], datetime.now()) # TODO: Extract it more properly mute_perm = ChatPermissions( can_add_web_page_previews=False, can_send_media_messages=False, can_send_other_messages=False, can_send_messages=False) bot.restrict_chat_member(get_group_chat_id(), user['_id'], mute_perm) message += f"{full_name} умер от коронавируса, F\n" except BadRequest as err: err_msg = f"can't restrict user: {err}" logger.warning(err_msg) else: chance = RANDOM_COUGH_UNINFECTED_CHANCE if _rng <= chance: message += f"{full_name} чихнул в пространство \n" if message: bot.send_message(get_group_chat_id(), message)
def random_fate(bot: Bot): """health or death""" users = _db.find_all() message = "" for user in users: _rng = random() # todo: move "_get_username" to commons full_name = _get_username(user) lethaled = _db.is_lethaled(user["_id"]) if "infected_since" in user and "cured_since" not in user and lethaled is None: delta_seconds = (datetime.now() - user["infected_since"]).total_seconds() delta_days_float = delta_seconds / (60 * 60 * 24) if _rng <= RANDOM_CURE_RATE ** (2 - delta_days_float): _db.cure(user["_id"]) message += f"{full_name} has recovered from coronavirus!\n" continue if _rng <= LETHALITY_RATE * (delta_days_float ** delta_days_float): try: _db.add_lethality(user["_id"], datetime.now()) # TODO: Extract it more properly mute_perm = ChatPermissions( can_add_web_page_previews=False, can_send_media_messages=False, can_send_other_messages=False, can_send_messages=False, ) until = datetime.now() + RESPAWN_TIME bot.restrict_chat_member( get_group_chat_id(), user["_id"], mute_perm, until ) message += f"{full_name} died from coronavirus F (time to respawn: {RESPAWN_TIME}) 🧟\n" except BadRequest as err: err_msg = f"can't restrict user: {err}" logger.warning(err_msg) if message: bot.send_message(get_group_chat_id(), message)
def random_cough(bot: Bot, queue: JobQueue): users = _db.find_all() message = "" for user in users: # todo: move "_get_username" to commons full_name = _get_username(user) if random() <= RANDOM_COUGH_UNINFECTED_CHANCE: message += f"{full_name} чихнул в пространство \n" if message: result = bot.send_message(get_group_chat_id(), message) cleanup_queue_update( queue, None, result, 30, remove_cmd=True, remove_reply=False )