From ea14bd5ca35d5636eaf60e29c0a56bbbeab51eef Mon Sep 17 00:00:00 2001 From: hemengyang Date: Thu, 21 Mar 2019 15:14:20 +0800 Subject: [PATCH] Add hello message (#9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加启动问好 * 在状态插件中添加在线时间 --- src/plugins/basic.py | 7 +++++++ src/plugins/recorder.py | 10 ++++++++++ src/plugins/status.py | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/src/plugins/basic.py b/src/plugins/basic.py index e01f63df..9a0e7089 100644 --- a/src/plugins/basic.py +++ b/src/plugins/basic.py @@ -4,6 +4,7 @@ from coolqbot.bot import bot from coolqbot.utils import scheduler +from plugins.recorder import recorder @scheduler.scheduled_job('interval', minutes=1) @@ -16,9 +17,15 @@ async def coolq_status(): try: msg = await bot.get_status() bot.logger.debug(msg) + # 检测是否需要重启 if not msg['good']: await bot.set_restart() + recorder.coolq_status = False + recorder.is_restart = True + recorder.send_hello = False bot.logger.info('重启酷Q') + else: + recorder.coolq_status = True except: bot.logger.error('无法获取酷Q状态') diff --git a/src/plugins/recorder.py b/src/plugins/recorder.py index 64d0ed83..d4156d43 100644 --- a/src/plugins/recorder.py +++ b/src/plugins/recorder.py @@ -10,10 +10,20 @@ class Recorder: def __init__(self): self._name = 'recorder.pkl' + + # 运行数据 self.last_message_on = datetime.utcnow() self.msg_send_time = [] self.repeat_list = {} self.msg_number_list = {} + + # 酷Q状态 + self.start_time = datetime.utcnow() + self.coolq_status = False + # 是否需要发送问好 + self.send_hello = False + self.is_restart = False + # 初始化插件数据管理 self._data = PluginData('recorder') diff --git a/src/plugins/status.py b/src/plugins/status.py index be20ef2c..6763bc36 100644 --- a/src/plugins/status.py +++ b/src/plugins/status.py @@ -1,8 +1,13 @@ """ 运行状态插件 """ import re +from datetime import datetime + +from dateutil.relativedelta import relativedelta from coolqbot.bot import bot +from coolqbot.config import GROUP_ID +from coolqbot.utils import scheduler from plugins.recorder import recorder @@ -19,6 +24,22 @@ async def status(context): repeat_rate = 0 str_data += f'\n现在的群内聊天总数是{msg_num}条' str_data += f'\n复读概率是{repeat_rate*100:.2f}%' + + # 距离第一次启动之后经过的时间 + rdate = relativedelta(datetime.utcnow(), recorder.start_time) + str_data += f'\n已在线' + if rdate.years: + str_data += f'{rdate.years}年' + if rdate.months: + str_data += f'{rdate.months}月' + if rdate.days: + str_data += f'{rdate.days}天' + if rdate.hours: + str_data += f'{rdate.hours}小时' + if rdate.minutes: + str_data += f'{rdate.minutes}分钟' + if rdate.seconds: + str_data += f'{rdate.seconds}秒' return {'reply': str_data, 'at_sender': False} @@ -27,3 +48,26 @@ def get_total_number(record_list): for dummy, v in record_list.items(): num += v return num + + +@scheduler.scheduled_job('interval', seconds=20) +async def check_status(): + """ 检测是否需要发送问好信息 + """ + if recorder.coolq_status and not recorder.send_hello: + hello_str = get_message(recorder.is_restart) + await bot.send_msg(message_type='group', group_id=GROUP_ID, message=hello_str) + recorder.send_hello = True + bot.logger.info('发送问好信息') + + +def get_message(is_restart): + """ 获得消息 + + 第一次启动和重启后的消息应该不一样 + """ + if is_restart: + recorder.is_restart = False + return '我又回来了!' + + return '早上好呀!'