Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[增强] 服务器启动完成后,还原机器人群名片 #145

Open
CC-3301 opened this issue Jan 10, 2025 · 2 comments
Open

[增强] 服务器启动完成后,还原机器人群名片 #145

CC-3301 opened this issue Jan 10, 2025 · 2 comments
Assignees
Labels
增强 有什么好的新点子要分享吗?

Comments

@CC-3301
Copy link

CC-3301 commented Jan 10, 2025

当前情况:

服务器设置了每天早上6点自动重启,而群名片更新是玩家上下线的时候才会更新
故早上6点自动重启后,一般到下午前基本没有玩家进入服务器,导致早上时群名片一直是"在线人数: x",没有进行还原

预期效果:

检测到服务器"正在"关闭时(因为启动脚本配置了自循环启动,故检测不到"关服"),还原机器人群名片
可以检测是否有“Stopping server”字样

MCDR及插件版本号

MCDR=2.14.3
GUGUbot=1.9.8
cq-qq-api=1.1.0
@CC-3301 CC-3301 added the 增强 有什么好的新点子要分享吗? label Jan 10, 2025
@CC-3301
Copy link
Author

CC-3301 commented Jan 10, 2025

如果服务器正在关闭,查询不到人数以进行群名片更新的话
检测到刚开服完毕也可以
捕获这个信息

Done (4.424s)! For help, type "help" or "?"

Done (x.xxxs)还需要处理一下

@CC-3301 CC-3301 changed the title [增强] 检测到服务器“正在”关闭时,还原机器人群名片 [增强] 检测到服务器重启,还原机器人群名片 Jan 10, 2025
@CC-3301
Copy link
Author

CC-3301 commented Jan 10, 2025

研究了一下,原来在检测到开服的时候更新一下群名片就好了

init.py

# 开服
def on_server_startup(server:PluginServerInterface)->None:
    if isinstance(qq_bot, qbot):
        # 机器人名字更新
        if qq_bot.config["command"]["name"]:
            qq_bot.set_number_as_name(server, restore_name=True)
        # 开服提示
        qq_bot.send_msg_to_all_qq(get_style_template('server_start', qq_bot.style))
        # 开服指令
        for _, command in qq_bot.start_command.data.items():
            # 执行指令
            server.execute(command)

bot.py
给set_number_as_name加上restore_name: bool = False,添加if restore_name

    def set_number_as_name(self, server:PluginServerInterface, restore_name: bool = False)->None:
        """
        Set the number of online player as bot's groupcard

        Args:
            server (mcdreforged.api.types.PluginServerInterface): The MCDR game interface.
        """
        bot_data = asyncio.run(self.bot.get_login_info())["data"]
        bot_qq_id = int(bot_data['user_id'])
        bot_name = bot_data['nickname']
        
        if restore_name:
            for gid in self.config.get('group_id', []):
                self.bot.set_group_card(gid, bot_qq_id, bot_name)
            return
    
        def list_callback(content:str):
            player_list, _ = parse_list_content(self.data, server, content)

            number = len(player_list)

            if number != 0:
                bot_name = "在线人数: {}".format(number) if \
                    not self.server_name else \
                    f"{self.server_name} {number} 人在线"
            
            # Call update API
            for gid in self.config.get('group_id', []):
                self.bot.set_group_card(gid, bot_qq_id, bot_name)

        if server.is_rcon_running(): # use rcon to get command return 
            list_callback(server.rcon_query("list"))
        else:         # use MCDR's on_info to get command return
            self._list_callback.append(list_callback)
            server.execute("list")

@CC-3301 CC-3301 changed the title [增强] 检测到服务器重启,还原机器人群名片 [增强] 服务器启动完成后,还原机器人群名片 Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
增强 有什么好的新点子要分享吗?
Projects
None yet
Development

No branches or pull requests

2 participants