This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
BWTAqua/bwtbot.py

176 lines
7.7 KiB
Python
Raw Normal View History

2022-09-08 13:12:25 +03:00
#-*- coding: utf-8 -*-
2022-12-30 23:13:10 +02:00
from os import getpid, system
from subprocess import call
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.sync import idle
from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat, Message
2022-09-08 13:12:25 +03:00
from pyrogram.enums.chat_action import ChatAction
from functions import *
from modules.colors import *
from modules.bwt import *
config = jsonLoad("config.json")
owner_id = config["owner_id"]
app = Client(config["bot_name"], api_id=config["api_id"], api_hash=config["api_hash"], bot_token=config["bot_token"])
@app.on_message(~ filters.scheduled & filters.command(["setcard", "задать карту"], prefixes=["/", ""]))
2022-12-30 23:13:10 +02:00
async def setcard(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if userGet(msg.from_user.id, "context") is None:
userSet(msg.from_user.id, "context", "set")
await msg.reply_text(string("send_number"), reply_markup=ForceReply(placeholder=string("enter_number")))
else:
await msg.reply_text(string("cancel_first"))
@app.on_message(~ filters.scheduled & filters.command(["cancel", "відміна"], prefixes=["/", ""]))
2022-12-30 23:13:10 +02:00
async def cancel(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if userGet(msg.from_user.id, "context") is not None:
userReset(msg.from_user.id, "context")
await msg.reply_text(string("cancel"))
else:
await msg.reply_text(string("cancel_none"))
@app.on_message(~ filters.scheduled & filters.command(["resetcard", "забути картку"], prefixes=["/", ""]))
2022-12-30 23:13:10 +02:00
async def resetcard(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if userGet(msg.from_user.id, "context") is None:
if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]:
userReset(msg.from_user.id, "card")
await msg.reply_text(string("card_unlinked"))
appendLog(f"User {str(msg.from_user.id)} reseted his card")
else:
await msg.reply_text(string("card_not_linked").format(string("get_number")))
appendLog(f"User {str(msg.from_user.id)} tried to reset non-existent card")
else:
await msg.reply_text(string("cancel_first"))
@app.on_message(~ filters.scheduled & filters.command(["balance", "баланс"], prefixes=["/", ""]))
2022-12-30 23:13:10 +02:00
async def balance(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if userGet(msg.from_user.id, "context") is None:
try:
if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]:
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
water_left = await getWaterLeft(userGet(msg.from_user.id, "card"), msg.from_user.id, app)
if water_left == "":
raise EmptyCardException("Card information is empty")
elif water_left == "Failure":
await msg.reply_text(string("error_occured").format(string("get_number")))
appendLog(f"User {str(msg.from_user.id)} could not get left water amount")
else:
await msg.reply_text(string("card_balance").format(water_left))
appendLog(f"User {str(msg.from_user.id)} has {water_left} liters remaining")
else:
await msg.reply_text(string("card_not_linked").format(string("get_number")))
appendLog(f"User {str(msg.from_user.id)} tried to get balance without card set")
except Exception as exp:
2022-12-30 23:13:10 +02:00
if msg.from_user.id != config["owner_id"]:
await msg.reply_text(string("error_occured").format(string("get_number")))
await app.send_message(owner_id, f"Error occured by {str(msg.from_user.id)}:\nException: `{exp}`\nTraceback: `{format_exc()}`")
2022-09-08 13:12:25 +03:00
appendLog(f"User {str(msg.from_user.id)} could not get left water amount")
else:
await msg.reply_text(string("cancel_first"))
@app.on_message(~ filters.scheduled & filters.command(["topup", "refill", "поповнити"], prefixes=["/", ""]))
2022-12-30 23:13:10 +02:00
async def topup_cmd(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if userGet(msg.from_user.id, "context") is None:
try:
if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]:
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
await msg.reply_text(string("top_up").format(str(userGet(msg.from_user.id, "card"))))
appendLog(f"User {str(msg.from_user.id)} requested top up")
else:
await msg.reply_text(string("card_not_linked").format(string("get_number")))
appendLog(f"User {str(msg.from_user.id)} tried to request top up without card set")
except Exception as exp:
2022-12-30 23:13:10 +02:00
await msg.reply_text(str(exp))
2022-09-08 13:12:25 +03:00
else:
await msg.reply_text(string("cancel_first"))
@app.on_message(~ filters.scheduled & filters.command(["start", "help", "допомога"], prefixes=["/", ""]))
2022-12-30 23:13:10 +02:00
async def help(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if userGet(msg.from_user.id, "context") is None:
await msg.reply_text(string("welcome").format(string("get_number")))
if msg.from_user.language_code in jsonLoad("strings.json"):
userSet(msg.from_user.id, "locale", msg.from_user.language_code)
else:
userSet(msg.from_user.id, "locale", "en")
else:
await msg.reply_text(string("cancel_first"))
2022-12-30 23:13:10 +02:00
pid = getpid()
2022-09-08 13:12:25 +03:00
@app.on_message(~ filters.scheduled & filters.command(["kill", "die", "shutdown"], prefixes="/"))
2022-12-30 23:13:10 +02:00
async def kill(_: Client, msg: Message):
2022-09-08 13:12:25 +03:00
if msg.from_user.id == owner_id:
await msg.reply_text(f"Shutting down bot with pid **{pid}**")
2022-12-30 23:13:10 +02:00
system(f"kill -9 {pid}")
2022-09-08 13:12:25 +03:00
@app.on_message(~ filters.scheduled)
async def any_message_handler(app, msg):
if userGet(msg.from_user.id, "context") == "set":
userSet(msg.from_user.id, "card", msg.text)
userReset(msg.from_user.id, "context")
appendLog(f"User {str(msg.from_user.id)} set card id to {msg.text}")
await msg.reply_text(string("card_linked").format(msg.text))
print(f'{nowtime()} {WHITE}Starting with PID {YELLOW}{pid}{RESET}')
2022-12-30 23:13:10 +02:00
app.start() # type: ignore
app.send_message(owner_id, f"Starting bot with pid **{pid}**") # type: ignore
2022-09-08 13:12:25 +03:00
app.set_bot_commands([
BotCommand("help", "Меню допомоги"),
BotCommand("balance", "Баланс картки"),
BotCommand("topup", "Поповнити картку"),
BotCommand("setcard", "Прив'язати картку"),
BotCommand("resetcard", "Відв'язати картку"),
BotCommand("cancel", "Відмінити операцію"),
],
2022-12-30 23:13:10 +02:00
language_code="uk") # type: ignore
2022-09-08 13:12:25 +03:00
app.set_bot_commands([
BotCommand("help", "Меню допомоги"),
BotCommand("balance", "Баланс картки"),
BotCommand("topup", "Поповнити картку"),
BotCommand("setcard", "Прив'язати картку"),
BotCommand("resetcard", "Відв'язати картку"),
BotCommand("cancel", "Відмінити операцію"),
],
2022-12-30 23:13:10 +02:00
language_code="ru") # type: ignore
2022-09-08 13:12:25 +03:00
app.set_bot_commands([
BotCommand("help", "Help menu"),
BotCommand("balance", "Card's balance"),
BotCommand("topup", "Refill card"),
BotCommand("setcard", "Link card"),
BotCommand("resetcard", "Unlink card"),
BotCommand("cancel", "Cancel operation"),
2022-12-30 23:13:10 +02:00
]) # type: ignore
2022-09-08 13:12:25 +03:00
app.set_bot_commands([
BotCommand("help", "Help menu"),
BotCommand("balance", "Card's balance"),
BotCommand("topup", "Refill card"),
BotCommand("setcard", "Link card"),
BotCommand("resetcard", "Unlink card"),
BotCommand("shutdown", "Turn off the bot"),
BotCommand("cancel", "Cancel operation"),
],
2022-12-30 23:13:10 +02:00
scope=BotCommandScopeChat(chat_id=owner_id)) # type: ignore
2022-09-08 13:12:25 +03:00
idle()
2022-12-30 23:13:10 +02:00
app.send_message(owner_id, f"Shutting down bot with pid **{pid}**") # type: ignore
2022-09-08 13:12:25 +03:00
print(f'\n{nowtime()} {WHITE}Shutting down with PID {YELLOW}{pid}{RESET}')
2022-12-30 23:13:10 +02:00
call(f'kill -9 {pid}', shell=True)