From 6ec1765d70f1c4990964b4d47e9f276983c8dc64 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 18 Dec 2024 01:15:40 +0300 Subject: [PATCH] automining gems --- README.md | 3 ++- src/avocmine.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ ubot.py | 15 +++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/avocmine.py diff --git a/README.md b/README.md index e4fc6d2..8ad5f73 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ before start, create config.json file with this content: "db_pymysql": false, "db_sqlite3": true, "a_h": true, - "a_404_patient": true + "a_404_patient": true, + "automine": true } ``` ___ diff --git a/src/avocmine.py b/src/avocmine.py new file mode 100644 index 0000000..342fe90 --- /dev/null +++ b/src/avocmine.py @@ -0,0 +1,50 @@ +from ubot import states + +from telethon import events +from loguru import logger +import asyncio +import time +import random +import re + + +async def automine_avocado_task(client): + logger.info('Automine module started') + + @client.on(events.NewMessage(pattern='⛏️ Удачно, удалось намайнить', chats=(6333102398,))) + async def automine_success(event): + m = event.message + if states.automine_enabled: + gems = re.findall(r'([0-9\ ]+) гемов', m.text)[0] + logger.success(f'Success mining {gems} gems') + states.latest_successfull_mine = time.time() + states.wait_before_next_mine = None + + @client.on(events.NewMessage(pattern='📉 Неудачная попытка', chats=(6333102398,))) + async def automine_fail(event): + m = event.message + if states.automine_enabled: + states.latest_successfull_mine = None + wait = re.findall(r'через ([0-9\ ]+) м.*', m.text)[0] + states.wait_before_next_mine = int(wait) * 60 + logger.warning(f'failed mine, waiting for {wait} minutes') + + while states.automine_enabled: + wait_before_mine = 1 + minutes = 0 + if states.latest_successfull_mine: + wait_before_mine = (time.time() - states.latest_successfull_mine) + random.uniform(240 * 60, 242 * 60) + minutes = wait_before_mine / 60 + elif states.wait_before_next_mine: + wait_before_mine = random.uniform(states.wait_before_next_mine, states.wait_before_next_mine + 3) + minutes = wait_before_mine / 60 + + logger.debug(f'Waiting {minutes} minutes before mining of gems') + await asyncio.sleep(wait_before_mine) + + if states.automine_enabled is False: + break + logger.debug('trying mine...') + await client.send_message(6333102398, 'майн') + await asyncio.sleep(5) # because avocado may slow answer + logger.warning('Automine stopped by user') diff --git a/ubot.py b/ubot.py index b778b20..f8168c1 100644 --- a/ubot.py +++ b/ubot.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # https://docs-python.ru/packages/telegram-klient-telethon-python/ <-info +from src import avocmine import asyncio from datetime import datetime, timedelta @@ -49,6 +50,7 @@ default_directory = '' default_config_file_path = 'config.json' treat_as_true = ('true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh') if is_termux: + # TODO: make notifications about update for termux default_directory = '/sdcard/ub4tg' os.system(f'mkdir -p {default_directory}') default_config_file_path = f'{default_directory}/config.json' @@ -61,13 +63,15 @@ if not os.path.exists(default_config_file_path): db_sqlite3 = True a_h = input('enable automatic use medkit? [y/n]: ').lower() in treat_as_true a_404_patient = input('enable automatic bioeb if victim not found or expired? It will be trigger on "Жертва не найдена" [y/n]: ').lower() in treat_as_true + automine = input('enable automatic mining of gems? [y/n]: ').lower() in treat_as_true new_config = {'api_id': api_id, 'api_hash': api_hash, 'timezone': timezone, 'db_pymysql': db_pymysql, 'db_sqlite3': db_sqlite3, 'a_h': a_h, - 'a_404_patient': a_404_patient} + 'a_404_patient': a_404_patient, + 'automine': automine} with open(default_config_file_path, "w") as configfile: json.dump(new_config, configfile, indent=4) @@ -99,6 +103,9 @@ class states: last_sent_bioeb = 0 # for measure time between reply avocado and bioeb last_reply_bioeb_avocado = 0 # same as above avocado_reply_timeout = 3 # increase interval if lag more than this timeout in secs + automine_enabled = config.automine or True + latest_successfull_mine = None + wait_before_next_mine = None stats_medkit = 0 stats_most_infect_spam_chats = Counter() @@ -785,6 +792,10 @@ async def main(): await asyncio.sleep(10) await client.delete_messages(event.chat_id, [event.id, m.id]) + asyncio.ensure_future(avocmine.automine_avocado_task(client)) + await client.run_until_disconnected() -asyncio.run(main()) + +if __name__ == '__main__': + asyncio.run(main())