TWT233 / khl.py

Python SDK for kaiheila.cn API
MIT License
239 stars 43 forks source link

开发时与部署时 命令不带参数的行为不一致 #213

Closed is-a-gamer closed 1 year ago

is-a-gamer commented 1 year ago

Describe the bug 我的两个环境的@bot.command()的表现行为不一致 开发环境使用的是 WSL2 Ubuntu22.04 部署环境是腾讯云 Ubuntu20.04 python3 和 khl.py的版本一致

@bot.command(name="hideout_all", aliases=["藏身处满级"])
@log(command="hideout_all")
async def ta_price(msg: Message, version: str = ""):
    if not version:
        await msg.channel.send("当前未输入版本,默认为黑边(仓库满级不做计算) 如果需要修改 /藏身处满级 白边")
    pass

To Reproduce

git clone https://github.com/is-a-gamer/sticky_rice_bot.git
cd sticky_rice_bot
python3 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt
# vim .env
python3 startup.py

Expected behavior

在开发环境, 也就是WSL2中,当我输入/hideout_all时,机器人提醒我当前未输入版本 当我部署到腾讯云上时,直接输入/hideout_all无任何返回,查看后台,出现报错

Logs/Screenshots 0df53f5663809b3f565a7e52951e92ba

Environment

is-a-gamer commented 1 year ago

贴一下配置文件

# your bot token
TOKEN=1/XXXXXXXXX

# default voice channel
CHANNEL=276XXXXXXXXXXXX

# the name of khl-voice sdk container, it should be different with your manager(bot) container name
CONTAINER_NAME=sticky_rice_bot

# the admin user id list
ADMIN_USERS=["1774XXXXXX"]

# the file_logger switch
FILE_LOGGER=false

PUBLIC=false
TWT233 commented 1 year ago

可以贴一下装饰器log的实现吗

TWT233 commented 1 year ago

这个是解析参数列表长度时发现参数过多/过少导致报错,

实现上是通过command的函数签名解析出需要的参数列表长度,然后和实际传入的参数列表比较 所以这里加了一层装饰器log可能会导致函数签名解析出问题

但是为什么会出现表现不一致的问题我也没什么头绪

is-a-gamer commented 1 year ago

确实是log引起的,惊扰到大佬了 而且当我把log放在了command上方的时候就正常了,目前还不知道是什么原因导致服务器上和wsl2中表现不一致

import uuid
import traceback

from loguru import logger
from khl import Message
from app.config.common import settings

def loguru_decorator_factory(command: str=""):
    def loguru_decorator(func):
        async def wrapped_function(msg: Message, *args, **kwargs):
            log_id = uuid.uuid4()
            logger.info(f"user: {msg.author.username} user_id: {msg.author.id} in guild_id: {msg.ctx.guild.id} "
                        f"used command {command} with args {args} and {kwargs}, log_id: {log_id}")
            try:
                msg.ctx.log_id = log_id
                await func(msg, *args, **kwargs)
            except Exception as e:
                logger.error(f"error occurred, msg: {e}, log_id: {log_id}, traceback: {traceback.format_exc()}")
                try:
                    if settings.debug:
                        await msg.channel.send(traceback.format_exc())
                    else:
                        await msg.channel.send(str(e))
                except Exception as e:
                    logger.critical(f"fatal error, log_id: {log_id}, traceback: {traceback.format_exc()}")
        return wrapped_function
    return loguru_decorator