async def add_job(session: CommandSession): global saved_jobs global running_jobs stripped_arg = session.current_arg_text.strip() id = str(time.time()) if session.is_first_run and stripped_arg: id = stripped_arg gid = session.event['group_id'] hour: str = session.get('hour', prompt='您想几点运行,多个时间用逗号隔开') minute: str = session.get('minute', prompt='您想几分运行,多个时间用逗号隔开') msg: str = session.get('msg', prompt='请输入要发送的内容') saved_jobs[id] = { 'groups': [gid], 'hour': hour, 'minute': minute, 'msg': msg } try: job = add_cron_job(send_group_msg, id=id, hour=hour, minute=minute, args=[gid, msg]) running_jobs.append(job) save_config(saved_jobs, job_path) await bot.send(session.event, f'好的,我记住了,将在每天{hour}点{minute}分发送{msg}', at_sender=True) except Exception as ex: sv.logger.error(f'添加定时任务时出现异常{ex}') await session.finish('参数错误,请重新设置')
async def _(session: CommandSession): hour: str = session.get('hour', prompt='您想几点运行,多个时间用逗号隔开') minute: str = session.get('minute', prompt='您想几分运行,多个时间用逗号隔开') cmd: str = session.get('cmd', prompt='请输入要运行的指令') cmd = cmd.strip('命令') #Hoshino消息只处理一次,加上命令前缀防止触发命令 session.event.raw_message = cmd session.event.message = Message(MessageSegment.text(cmd)) try: global _running_jobs job = add_cron_job(task, hour=hour, minute=minute, args=[session.event]) _running_jobs.append(job) global _tasks #对event先处理,剔除由RexTrigger添加的match对象 if 'match' in session.event: del session.event['match'] _tasks['data'].append( dict({ "id": job.id, "event": session.event, "hour": hour, "minute": minute })) save_config(_tasks, _task_path) await session.send('设置成功') except ValueError as ex: sv.logger.error(f'添加定时任务时出现异常{ex}') await session.send('参数错误,请重新设置') except: print_exc()
async def delete_job(bot, event: Event): global running_jobs global saved_jobs id = event.raw_message.strip('删除计划任务') if id in saved_jobs: del saved_jobs[id] save_config(saved_jobs, job_path) for job in running_jobs: if id == job.id: running_jobs.remove(job) job.remove() await bot.send(event, '已删除计划任务')
async def check_BiliVideo(): for BV in _BVs: await BV.get() video = BV.parse_xml() if BV.latest != video: #投稿有更新 sv.logger.info(f'检测到up{BV.UID}投稿更新') BV.latest = video global _subscribes _subscribes[str(BV.UID)]['latest_time'] = video.time save_config(_subscribes, subs_path) groups = _subscribes[str(BV.UID)]['subs_groups'] await broadcast(f'up投稿提醒========\n{video.title}\n{video.link}', groups=groups) else: sv.logger.info(f'未检测到up{BV.UID}投稿更新')
async def subscribe(session: CommandSession): UID_str = session.get('UID', prompt='请输入订阅up的UID') if not UID_str.isdigit(): del session.state['UID'] session.pause('参数错误,请重新输入') global _subscribes gid = session.event['group_id'] if UID_str in _subscribes.keys(): if gid not in _subscribes[UID_str]['subs_groups']: _subscribes[UID_str]['subs_groups'].append(gid) else: await session.send('本群已经订阅过该UP了') return else: _subscribes[UID_str] = { "UID": int(UID_str), "subs_groups": [gid], "latest_time": "" } BV = BiliVideo(int(UID_str)) global _BVs _BVs.append(BV) if save_config(_subscribes, subs_path): await session.send('订阅成功') else: await session.send('订阅失败,请与bot维护中联系')
async def check(): for sv in _inf_svs: for info in _inf_svs[sv]: try: await info.get() except Exception as ex: sv.logger.error(ex) if info.check_update(): _latest_data[info.route] = info._latest save_config(_latest_data, _latest_path) sv.logger.info(f'检查到{sv.name}消息更新') data = info.parse_xml() title = data['title'] link = data['link'] await broadcast(f'{title}\n{link}', sv_name=sv.name) else: sv.logger.info(f'未检查到{sv.name}消息更新')
async def cancel(session: CommandSession): UID_str = session.get('UID', prompt='请输入UID') global _subscribes global _BVs if UID_str in _subscribes.keys(): if len(_subscribes[UID_str]['subs_groups']) == 1: #只有一个群订阅该up投稿 for BV in _BVs[::-1]: if BV.UID == int(UID_str): _BVs.remove(BV) del _subscribes[UID_str] save_config(_subscribes, subs_path) sv.logger.info(f'成功取消up{UID_str}的投稿提醒') session.send(f'成功取消up{UID_str}的投稿提醒') else: gid = session.event['group_id'] _subscribes[UID_str]['subs_groups'].remove(gid) save_config(_subscribes, subs_path) session.send(f'成功取消up{UID_str}的投稿提醒')
async def cancel(session: CommandSession): room = session.get('room', prompt='请输入房间号') global _subscribes global _lives if room in _subscribes.keys(): if len(_subscribes[room]['subs_groups']) == 1: #只有一个群订阅该直播 for lv in _lives[::-1]: if lv.room_id == int(room): _lives.remove(lv) del _subscribes[room] save_config(_subscribes, subs_path) sv.logger.info(f'成功取消直播间{room}的开播提醒') session.send(f'成功取消直播间{room}直播提醒') else: gid = session.event['group_id'] _subscribes[room]['subs_groups'].remove(gid) save_config(_subscribes, subs_path) session.send(f'成功取消直播间{room}直播提醒')
async def check_live(): for lv in _lives: await lv.get() data = lv.parse_xml() if data.get('title'): #开播状态 title = data['title'] link = data['link'] tuber = data['tuber'] latest_time = data['latest_time'] if latest_time != lv.latest_time: lv.latest_time = latest_time global _subscribes _subscribes[str(lv.room_id)]['latest_time'] = latest_time save_config(_subscribes, subs_path) sv.logger.info(f'检测到{lv.platform}{lv.room_id}直播间开播了') await notice(lv.room_id, f'开播提醒=========\n{tuber}\n{title}\n{link}') else: #未开播 pass
async def _(session: CommandSession): stripped_arg = session.current_arg_text.strip() if session.is_first_run and stripped_arg: id = stripped_arg else: id = session.get('id', prompt='请输入任务id,任务id可通过/show_cron_job得到') global _running_jobs job_num = len(_running_jobs) for jb in _running_jobs[::-1]: if jb.id == id: _running_jobs.remove(jb) global _tasks for tsk in _tasks['data'][::-1]: if tsk['id'] == id: _tasks['data'].remove(tsk) if job_num == len(_running_jobs): await session.send('没有找到该任务') session.finish() else: save_config(_tasks, _task_path) await session.send('删除周期任务成功')
async def subscribe(session: CommandSession): session.get('platform', prompt='请选择订阅的平台,目前支持哔哩哔哩和斗鱼') if session.state['platform'] == '哔哩哔哩' or session.state[ 'platform'] == 'bilibili': platform = 'bilibili' elif session.state['platform'] == '斗鱼' or session.state[ 'platform'] == 'douyu': platform = 'douyu' else: del session.state['platform'] session.pause('参数错误,请重新输入') room = session.get('room', prompt='请输入订阅的房间号') if not session.state['room'].isdigit(): del session.state['room'] session.pause('参数错误,请重新输入') global _subscribes gid = session.event['group_id'] if room in _subscribes.keys(): if gid not in _subscribes[room]['subs_groups']: _subscribes[room]['subs_groups'].append(gid) else: await session.send('本群已经订阅过该直播间了') return else: _subscribes[room] = { "platform": platform, "room": int(room), "subs_groups": [gid], "latest_time": "" } lv = BiliLive(int(room)) global _lives _lives.append(lv) if save_config(_subscribes, subs_path): await session.send('订阅成功') else: await session.send('订阅失败,请与bot维护中联系')