main
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""在Docker中退出时, 会自动重启Bot服务."""
4
5import os
6
7from glom import glom
8from loguru import logger
9from pyrogram.types import Message
10
11from config import DEVICE_NAME
12from custom.config import ACCOUNT_NAME, USER_BENNY
13from networking import hx_req
14
15
16async def restart_bot(message: Message):
17 # VPS上的账号不响应 开发Group 的消息
18 if message.chat.id == -1002434113592 and DEVICE_NAME in ["BennyBot-JP", "BennyBot-US", "BennyBot-CN"]:
19 return
20 uid = glom(message, "from_user.id", default=0)
21 cid = glom(message, "chat.id", default=0)
22 if uid != USER_BENNY:
23 return
24 texts = str(message.text).strip().lower()
25
26 # restart
27 if texts == "/restart" and ACCOUNT_NAME == "benny" and uid == USER_BENNY and cid == USER_BENNY:
28 logger.warning("Restart benny")
29 os._exit(0) # benny个人对话收到/restart命令
30 elif texts == "/restart" and ACCOUNT_NAME == "xiaohao" and uid == USER_BENNY and cid == USER_BENNY:
31 logger.warning("Restart xiaohao")
32 os._exit(0) # xiaohao收到大号发送的/restart命令
33 elif texts == "/restart" and ACCOUNT_NAME == "bot" and uid == USER_BENNY and cid == USER_BENNY:
34 logger.warning("Restart bot")
35 os._exit(0) # bot收到大号发送的/restart命令
36
37 # reboot
38 elif texts == "/reboot".lower():
39 await message.reply("`/reboot US`\n`/reboot HK`\n`/reboot JP`", quote=True)
40 elif texts == "/reboot HK".lower():
41 server_id = "8e88dd20-a9c4-48ed-9d45-1e33d4c75d38"
42 headers = {"Accept": "application/json", "Authorization": f"Bearer {os.environ['VPS_HK_API']}"}
43 resp = await hx_req(f"https://dash.halocloud.net/api/server/{server_id}/restart", "POST", headers=headers)
44 if isinstance(resp, dict):
45 resp.pop("headers", None)
46 resp.pop("status_code", None)
47 await message.reply(str(resp))
48 elif texts == "/reboot US".lower():
49 server_id = "3f839be5-5065-4f82-8dc1-71000192f38f"
50 headers = {"Accept": "application/json", "Authorization": f"Bearer {os.environ['VPS_US_API']}"}
51 resp = await hx_req(f"https://virt.crunchbits.com/api/server/{server_id}/restart", "POST", headers=headers)
52 if isinstance(resp, dict):
53 resp.pop("headers", None)
54 resp.pop("status_code", None)
55 await message.reply(str(resp))
56 elif texts == "/reboot JP".lower():
57 server_id = "1876269"
58 resp = await hx_req(f"https://api.64clouds.com/v1/restart?veid={server_id}&api_key={os.environ['VPS_JP_API']}")
59 if isinstance(resp, dict):
60 resp.pop("headers", None)
61 resp.pop("status_code", None)
62 await message.reply(str(resp))