dev #19

Merged
profitroll merged 98 commits from dev into master 2023-04-24 13:48:22 +03:00
4 changed files with 134 additions and 25 deletions
Showing only changes of commit 20666fc0f8 - Show all commits

View File

@ -116,6 +116,6 @@
"commands_admin": [ "commands_admin": [
"import", "import",
"export", "export",
"reboot" "shutdown"
] ]
} }

View File

@ -0,0 +1,29 @@
from os import getpid, makedirs, path
from time import time
from modules.app import app
from pyrogram import filters
from pyrogram.types import CallbackQuery
from classes.poster_client import PosterClient
from modules.scheduler import scheduler
from modules.logger import logWrite
from modules.utils import configGet, jsonSave, locale
@app.on_callback_query(filters.regex("shutdown"))
async def callback_query_nothing(app: PosterClient, clb: CallbackQuery):
if clb.from_user.id in app.admins:
pid = getpid()
logWrite(f"Shutting down bot with pid {pid}")
await clb.answer()
await clb.message.reply_text(
locale("shutdown", "message", locale=clb.from_user.language_code).format(
pid
),
)
scheduler.shutdown()
makedirs(configGet("cache", "locations"), exist_ok=True)
jsonSave(
{"timestamp": time()},
path.join(configGet("cache", "locations"), "shutdown_time"),
)
exit()

View File

@ -1,24 +1,45 @@
from os import getpid from os import getpid, makedirs, path
from time import time
from pyrogram import filters from pyrogram import filters
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
from classes.poster_client import PosterClient from classes.poster_client import PosterClient
from pyrogram.types import Message from modules.app import app, users_with_context
from modules.app import app
from modules.logger import logWrite from modules.logger import logWrite
from modules.utils import configGet, killProc, locale from modules.scheduler import scheduler
from modules.utils import configGet, jsonSave, locale
@app.on_message( @app.on_message(~filters.scheduled & filters.command(["shutdown"], prefixes=["", "/"]))
~filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"])
)
async def cmd_kill(app: PosterClient, msg: Message): async def cmd_kill(app: PosterClient, msg: Message):
if msg.from_user.id in app.admins: if msg.from_user.id in app.admins:
pid = getpid() global users_with_context
logWrite( if len(users_with_context) > 0:
locale("shutdown", "console", locale=configGet("locale")).format(str(pid))
)
await msg.reply_text( await msg.reply_text(
locale("shutdown", "message", locale=configGet("locale")).format(str(pid)) f"There're {len(users_with_context)} unfinished users' contexts. If you turn off the bot, those will be lost. Please confirm shutdown using a button below.",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Confirm shutdown", callback_data="shutdown"
) )
killProc(pid) ]
]
),
)
return
pid = getpid()
logWrite(f"Shutting down bot with pid {pid}")
await msg.reply_text(
locale("shutdown", "message", locale=msg.from_user.language_code).format(
pid
),
)
scheduler.shutdown()
makedirs(configGet("cache", "locations"), exist_ok=True)
jsonSave(
{"timestamp": time()},
path.join(configGet("cache", "locations"), "shutdown_time"),
)
exit()

View File

@ -1,14 +1,18 @@
from os import getpid from os import getpid, path
from datetime import datetime
from sys import exit from sys import exit
from time import time
from modules.cli import * from modules.cli import *
from modules.logger import logWrite from modules.logger import logWrite
from modules.scheduler import scheduler from modules.scheduler import scheduler
from modules.utils import configGet, killProc, locale from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale
# Import =================================================================================================================================== # Import ===================================================================================================================================
try: try:
from pyrogram.sync import idle from pyrogram.sync import idle
from pyrogram.errors import bad_request_400
from dateutil.relativedelta import relativedelta
from modules.app import app from modules.app import app
except ModuleNotFoundError: except ModuleNotFoundError:
@ -57,6 +61,7 @@ pid = getpid()
# Imports ================================================================================================================================== # Imports ==================================================================================================================================
from plugins.commands.general import * from plugins.commands.general import *
from plugins.callbacks.shutdown import *
if configGet("submit", "mode"): if configGet("submit", "mode"):
from plugins.callbacks.nothing import * from plugins.callbacks.nothing import *
@ -112,14 +117,54 @@ if configGet("post", "mode"):
# asyncio.run(main()) # asyncio.run(main())
if __name__ == "__main__": if __name__ == "__main__":
logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid))) logWrite(locale("startup", "console").format(str(pid)))
app.start() app.start()
if configGet("startup", "reports"): if configGet("startup", "reports"):
try:
if path.exists(path.join(configGet("cache", "locations"), "shutdown_time")):
downtime = relativedelta(
datetime.now(),
datetime.fromtimestamp(
jsonLoad(
path.join(configGet("cache", "locations"), "shutdown_time")
)["timestamp"]
),
)
if downtime.days >= 1:
app.send_message( app.send_message(
app.owner, configGet("owner"),
locale("startup", "message", locale=configGet("locale")).format(str(pid)), locale(
"startup_downtime_days",
"message",
).format(pid, downtime.days),
)
elif downtime.hours >= 1:
app.send_message(
configGet("owner"),
locale(
"startup_downtime_hours",
"message",
).format(pid, downtime.hours),
)
else:
app.send_message(
configGet("owner"),
locale(
"startup_downtime_minutes",
"message",
locale=configGet("locale"),
).format(pid, downtime.minutes),
)
else:
app.send_message(
configGet("owner"),
locale("startup", "message").format(pid),
)
except bad_request_400.PeerIdInvalid:
logWrite(
f"Could not send startup message to bot owner. Perhaps user has not started the bot yet."
) )
if configGet("post", "mode"): if configGet("post", "mode"):
@ -132,10 +177,24 @@ if __name__ == "__main__":
idle() idle()
try:
app.send_message( app.send_message(
app.owner, configGet("owner"),
locale("shutdown", "message", locale=configGet("locale")).format(str(pid)), locale("shutdown", "message").format(pid),
) )
logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid))) except bad_request_400.PeerIdInvalid:
logWrite(
f"Could not send shutdown message to bot owner. Perhaps user has not started the bot yet."
)
makedirs(configGet("cache", "locations"), exist_ok=True)
jsonSave(
{"timestamp": time()},
path.join(configGet("cache", "locations"), "shutdown_time"),
)
logWrite(locale("shutdown", "console").format(str(pid)))
scheduler.shutdown()
killProc(pid) killProc(pid)