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

215 lines
7.3 KiB
Python
Raw Normal View History

2023-03-18 01:52:15 +02:00
# -*- coding: utf-8 -*-
2022-09-08 13:12:25 +03:00
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,
ReplyKeyboardRemove,
)
2022-09-08 13:12:25 +03:00
from pyrogram.enums.chat_action import ChatAction
from convopyro import Conversation, listen_message
2022-09-08 13:12:25 +03:00
from functions import *
from modules.colors import *
from modules.bwt import *
config = jsonLoad("config.json")
owner_id = config["owner_id"]
2023-03-18 01:52:15 +02:00
app = Client(
config["bot_name"],
api_id=config["api_id"],
api_hash=config["api_hash"],
bot_token=config["bot_token"],
)
2022-09-08 13:12:25 +03:00
Conversation(app)
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
@app.on_message(
~filters.scheduled
& filters.command(["setcard", "задать карту"], prefixes=["/", ""])
)
2022-12-30 23:13:10 +02:00
async def setcard(_: Client, msg: Message):
await msg.reply_text(
string("send_number"),
reply_markup=ForceReply(placeholder=string("enter_number")),
)
answer = await listen_message(_, msg.chat.id, timeout=None)
if answer is None:
return
elif answer.text.strip() in ["/cancel", "cancel", "/відміна", "відміна"]:
await msg.reply_text(string("cancel"), reply_markup=ReplyKeyboardRemove())
return
userSet(answer.from_user.id, "card", answer.text)
appendLog(f"User {str(msg.from_user.id)} set card id to {answer.text}")
await msg.reply_text(
string("card_linked").format(answer.text), reply_markup=ReplyKeyboardRemove()
)
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
@app.on_message(
~filters.scheduled
& filters.command(["resetcard", "забути картку"], prefixes=["/", ""])
)
2022-12-30 23:13:10 +02:00
async def resetcard(_: Client, msg: Message):
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")
2022-09-08 13:12:25 +03:00
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")
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
@app.on_message(
~filters.scheduled & filters.command(["balance", "баланс"], prefixes=["/", ""])
)
2022-12-30 23:13:10 +02:00
async def balance(_: Client, msg: Message):
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 == "":
await msg.reply_text(
string("error_new").format(
f'https://bwtaqua.com.ua/card-topup/?id={userGet(msg.from_user.id, "card")}'
2023-03-18 01:52:15 +02:00
)
)
# raise EmptyCardException("Card information is empty")
elif water_left == "Failure":
2023-03-18 01:52:15 +02:00
await msg.reply_text(
string("error_occured").format(string("get_number"))
2023-03-18 01:52:15 +02:00
)
appendLog(
f"User {str(msg.from_user.id)} could not get left water amount"
2023-03-18 01:52:15 +02:00
)
else:
await msg.reply_text(string("card_balance").format(water_left))
appendLog(
f"User {str(msg.from_user.id)} has {water_left} liters remaining"
2023-03-18 01:52:15 +02:00
)
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"
2023-03-18 01:52:15 +02:00
)
except Exception as exp:
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()}`",
)
appendLog(f"User {str(msg.from_user.id)} could not get left water amount")
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
@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):
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:
await msg.reply_text(str(exp))
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
@app.on_message(
~filters.scheduled
& filters.command(["start", "help", "допомога"], prefixes=["/", ""])
)
2022-12-30 23:13:10 +02:00
async def help(_: Client, msg: Message):
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)
2022-09-08 13:12:25 +03:00
else:
userSet(msg.from_user.id, "locale", "en")
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
2022-12-30 23:13:10 +02:00
pid = getpid()
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02: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
2023-03-18 01:52:15 +02:00
print(f"{nowtime()} {WHITE}Starting with PID {YELLOW}{pid}{RESET}")
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
2023-03-18 01:52:15 +02:00
app.set_bot_commands(
[
BotCommand("help", "Меню допомоги"),
BotCommand("balance", "Баланс картки"),
BotCommand("topup", "Поповнити картку"),
BotCommand("setcard", "Прив'язати картку"),
BotCommand("resetcard", "Відв'язати картку"),
2022-09-08 13:12:25 +03:00
],
2023-03-18 01:52:15 +02:00
language_code="uk",
) # type: ignore
app.set_bot_commands(
[
BotCommand("help", "Меню допомоги"),
BotCommand("balance", "Баланс картки"),
BotCommand("topup", "Поповнити картку"),
BotCommand("setcard", "Прив'язати картку"),
BotCommand("resetcard", "Відв'язати картку"),
2022-09-08 13:12:25 +03:00
],
2023-03-18 01:52:15 +02:00
language_code="ru",
) # type: ignore
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"),
]
) # type: ignore
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"),
2022-09-08 13:12:25 +03:00
],
2023-03-18 01:52:15 +02:00
scope=BotCommandScopeChat(chat_id=owner_id),
) # type: ignore
2022-09-08 13:12:25 +03:00
idle()
2023-03-18 01:52:15 +02:00
app.send_message(owner_id, f"Shutting down bot with pid **{pid}**") # type: ignore
print(f"\n{nowtime()} {WHITE}Shutting down with PID {YELLOW}{pid}{RESET}")
2022-09-08 13:12:25 +03:00
2023-03-18 01:52:15 +02:00
call(f"kill -9 {pid}", shell=True)