async def morning(): """ 早安 """ hello_str = get_message() await bot.get_bot().send_msg(message_type='group', group_id=bot.get_bot().config.GROUP_ID, message=hello_str) bot.logger.info('发送早安信息')
async def save_recorder(): """ 每隔一分钟保存一次数据 """ # 保存数据前先清理 msg_send_time 列表,仅保留最近 10 分钟的数据 for group_id in bot.get_bot().config.GROUP_ID: recorder.message_number(10, group_id) recorder.save_data()
def init_data(self): """ 初始化数据 """ self._last_message_on = { group_id: datetime.now() for group_id in bot.get_bot().config.GROUP_ID } self._msg_send_time = { group_id: [] for group_id in bot.get_bot().config.GROUP_ID } self._repeat_list = { group_id: {} for group_id in bot.get_bot().config.GROUP_ID } self._msg_number_list = { group_id: {} for group_id in bot.get_bot().config.GROUP_ID }
async def check_status(): """ 检测是否需要发送问好信息 """ if recorder.coolq_status and not recorder.send_hello: hello_str = get_message() await bot.get_bot().send_msg(message_type='group', group_id=bot.get_bot().config.GROUP_ID, message=hello_str) recorder.send_hello = True bot.logger.info('发送首次启动的问好信息')
def _load_data(self): """ 加载数据 """ if not self._data.exists(f'{self._name}.pkl'): bot.logger.error(f'{self._name}.pkl does not exist!') return data = self._data.load_pkl(self._name) # 如果是老版本格式的数据则先升级在加载 # 默认使用配置中第一个群来升级老数据 if 'version' not in data or data['version'] != VERSION: bot.logger.info('发现旧版本数据,正在升级数据') data = update(data, bot.get_bot().config.GROUP_ID[0]) self._data.save_pkl(data, self._name) bot.logger.info('升级数据成功') # 加载数据 self._last_message_on = data['last_message_on'] self._msg_send_time = data['msg_send_time'] self._repeat_list = data['repeat_list'] self._msg_number_list = data['msg_number_list'] # 如果群列表新加了群,则补充所需的数据 for group_id in bot.get_bot().config.GROUP_ID: if group_id not in self._last_message_on: self._last_message_on[group_id] = datetime.now() if group_id not in self._msg_send_time: self._msg_send_time[group_id] = [] if group_id not in self._repeat_list: self._repeat_list[group_id] = {} if group_id not in self._msg_number_list: self._msg_number_list[group_id] = {}
async def nikcname(user_id): """ 输入 QQ 号,返回群昵称,如果群昵称为空则返回 QQ 昵称 """ try: msg = await bot.get_bot().get_group_member_info( group_id=bot.get_bot().config.GROUP_ID, user_id=user_id, no_cache=True) if msg['card']: return msg['card'] return msg['nickname'] except: # 如果不在群里的话(因为有可能会退群) msg = await bot.get_bot().get_stranger_info(user_id=user_id) return msg['nickname']