PagerMaid-Discord/pagermaid/__init__.py

93 lines
2.2 KiB
Python
Raw Normal View History

2021-07-25 06:00:08 +00:00
""" PagerMaid initialization. """
import discord
2021-07-25 08:38:21 +00:00
from typing import Dict
from subprocess import run, PIPE
2021-07-25 06:00:08 +00:00
from discord.ext import commands
from os import getcwd
2021-07-25 08:38:21 +00:00
from distutils.util import strtobool
2021-07-25 06:00:08 +00:00
from yaml import load, FullLoader
from redis import StrictRedis
from coloredlogs import ColoredFormatter
from logging import getLogger, INFO, DEBUG, ERROR, StreamHandler, basicConfig
persistent_vars = {}
module_dir = __path__[0]
working_dir = getcwd()
config = None
2021-07-25 08:38:21 +00:00
des_map: Dict[str, str] = {}
par_map: Dict[str, str] = {}
2021-07-25 06:00:08 +00:00
logs = getLogger(__name__)
logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s"
logging_handler = StreamHandler()
logging_handler.setFormatter(ColoredFormatter(logging_format))
root_logger = getLogger()
root_logger.setLevel(ERROR)
root_logger.addHandler(logging_handler)
basicConfig(level=INFO)
logs.setLevel(INFO)
try:
config = load(open(r"config.yml"), Loader=FullLoader)
except FileNotFoundError:
logs.fatal("错误:配置文件不存在。")
exit(1)
if strtobool(config['debug']):
logs.setLevel(DEBUG)
else:
logs.setLevel(INFO)
token = config['token']
2021-07-25 08:38:21 +00:00
color = int(config['color'], 16)
2021-07-25 06:00:08 +00:00
prefix = config['prefix']
game_des = config['game_des']
redis_host = config['redis']['host']
redis_port = config['redis']['port']
redis_db = config['redis']['db']
redis = StrictRedis(host=redis_host, port=redis_port, db=redis_db)
git_hash = run("git rev-parse HEAD", stdout=PIPE, shell=True).stdout.decode()
if token == 'Here':
logs.fatal("错误Token 不存在。")
exit(1)
if game_des:
bot = commands.Bot(
command_prefix=prefix,
activity=discord.Game(
name=game_des
)
)
else:
bot = commands.Bot(command_prefix=prefix)
bot.remove_command('help')
2021-07-25 08:38:21 +00:00
def des_handler(cmd: str, des: str) -> None:
global des_map
if des_map.get(cmd) is not None:
return
des_map[cmd] = des
def par_handler(cmd: str, par: str) -> None:
global par_map
if par_map.get(cmd) is not None:
return
par_map[cmd] = par
2021-07-25 06:00:08 +00:00
def redis_status():
try:
redis.ping()
return True
except BaseException:
return False
async def log(message):
logs.info(
message.replace('`', '\"')
)
if not strtobool(config['log']):
return