Skip to content

Commit

Permalink
Add hello message (#9)
Browse files Browse the repository at this point in the history
* 添加启动问好
* 在状态插件中添加在线时间
  • Loading branch information
he0119 authored Mar 21, 2019
1 parent a0c9ea3 commit ea14bd5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/plugins/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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状态')

Expand Down
10 changes: 10 additions & 0 deletions src/plugins/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
44 changes: 44 additions & 0 deletions src/plugins/status.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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}


Expand All @@ -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 '早上好呀!'

0 comments on commit ea14bd5

Please sign in to comment.