Skip to content

Commit

Permalink
🎨 适配编译时重启
Browse files Browse the repository at this point in the history
  • Loading branch information
AirportR committed Feb 29, 2024
1 parent e541ace commit 1a586f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions botmodule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from botmodule.command.setting import *
from botmodule.command.download import *
from botmodule.command.connect import *
from botmodule.command.reboot import *
from botmodule.command import common_command
from botmodule.command.edit import *
from botmodule import register
Expand Down
28 changes: 4 additions & 24 deletions botmodule/command/grant.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
import signal
import sys
import pyrogram.types
from loguru import logger
from pyrogram import Client
from pyrogram.errors import RPCError
from pyrogram.types import Message
from botmodule.init_bot import admin, config, reloadUser
from utils.cron.utils import message_delete_queue


async def grant(client: Client, message: pyrogram.types.Message):
async def grant(client: "Client", message: "Message"):
try:
if int(message.from_user.id) not in admin and str(
message.from_user.username) not in admin: # 如果不在USER_TARGET名单是不会有权限的
Expand Down Expand Up @@ -51,7 +48,7 @@ async def grant(client: Client, message: pyrogram.types.Message):
print(r)


async def ungrant(_, message: pyrogram.types.Message):
async def ungrant(_, message: "Message"):
try:
if int(message.from_user.id) not in admin and str(
message.from_user.username) not in admin: # 如果不在USER_TARGET名单是不会有权限的
Expand Down Expand Up @@ -91,7 +88,7 @@ async def ungrant(_, message: pyrogram.types.Message):
logger.error(str(r))


async def user(_, message):
async def user(_, message: "Message"):
try:
if int(message.from_user.id) not in admin and str(
message.from_user.username) not in admin:
Expand All @@ -104,20 +101,3 @@ async def user(_, message):
USER_TARGET = config.getuser()
text = "当前用户有:" + str(set(USER_TARGET)) + "\n共{}个".format(len(USER_TARGET))
await message.reply(text)


async def restart_or_killme(_, message, kill=False):
try:
if kill:
await message.reply("再见~")
os.kill(os.getpid(), signal.SIGINT)
else:
await message.reply("开始重启(大约等待五秒)")
# p = sys.executable
# 用 main.py 替换当前进程,传递 sys.argv 中的参数
# 注意:这个函数不会返回,除非出现错误
os.execlp(sys.executable, "main.py", *sys.argv)
# os.execl(p, p, *sys.argv)
sys.exit()
except RPCError as r:
logger.error(str(r))
23 changes: 23 additions & 0 deletions botmodule/command/reboot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import sys

from loguru import logger
from pyrogram.errors import RPCError


async def restart_or_killme(_, message, kill=False):
try:
if kill:
await message.reply("再见~")
os.kill(os.getpid(), 2)
else:
await message.reply("开始重启(大约等待五秒)")
compiled = getattr(restart_or_killme, "__compiled__", None) # 是否处于编译状态
if compiled:
filename = sys.argv[0]
os.execlp(filename, filename)
else:
os.execlp(sys.executable, "main.py", *sys.argv)
sys.exit()
except RPCError as r:
logger.error(str(r))

0 comments on commit 1a586f6

Please sign in to comment.