async def rmup_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''取消订阅UP主投稿 用法: /rmup UP主uid''' if len(args) != 1: return MessageChain.create([Plain("缺少参数或参数过多")]) up_id = args[0] res = getCards(up_id) if res == "error": msg = [Plain("未找到该UP主!")] logger.info("[RMUP]未找到该UP主") else: up_dict = readJSON(BILI_UP_JSON_PATH) if not isinstance(up_dict, dict): raise TypeError("Expected dict but found:", up_dict) if up_id in up_dict.keys(): if isinstance(subject, Member): up_dict[up_id].remove(groupToStr(subject.group)) elif isinstance(subject, Friend): up_dict[up_id].remove(subject.id) if len(up_dict[up_id]) == 0: del up_dict[up_id] updateJSON(BILI_UP_JSON_PATH, up_dict) msg = [Plain("已将 {} 移出监视列表\n".format(getNameByUid(up_id)))] logger.info("[RMUP]返回成功") return MessageChain.create(msg)
async def stat_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''展示最近指定场数(默认20场)游戏平均数据 用法: /stat (id) (num)''' if len(args) < 1 or len(args) > 2: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /stat (id) (num)")]) query_id, *num = args if isinstance(subject, Member): dota_id = bot.db.get(subject.group, "dota_id").get(query_id) else: dota_id = bot.db.get(subject, "dota_id").get(query_id) if not dota_id: logger.info(f"[STAT]未添加该用户{query_id}") return MessageChain.create([Plain(f"未添加该用户{query_id}!")]) else: if num and type(num[0]) == type(query_id) and query_id == num[0]: num = [20] query_id = dota_id args = 20 if len(num) == 1: try: args = int(num[0]) if args > 50 or args <= 0: args = 20 except ValueError: args = 20 res = getStat(query_id, args) logger.info("[STAT]返回成功") return MessageChain.create([Plain(res)])
async def rmlive_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''取消订阅直播间 用法: /rmlive 房间号''' if len(args) != 1: return MessageChain.create([Plain("缺少参数或参数过多")]) room_id = args[0] res = getLiveInfo(room_id) if isinstance(res, str): msg = [Plain("未找到该直播!")] logger.info("[RMLIVE]未找到该直播") else: monitor_dict = readJSON(BILI_LIVE_JSON_PATH) if not isinstance(monitor_dict, dict): raise TypeError("Expected dict but found:", monitor_dict) if room_id in monitor_dict.keys(): if isinstance(subject, Member): monitor_dict[room_id].remove(groupToStr(subject.group)) elif isinstance(subject, Friend): monitor_dict[room_id].remove(subject.id) if len(monitor_dict[room_id]) == 0: del monitor_dict[room_id] updateJSON(BILI_LIVE_JSON_PATH, monitor_dict) msg = [Plain("已将 {} 移出监视列表\n".format(res['name']))] logger.info("[RMLIVE]返回成功") return MessageChain.create(msg)
async def live_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''B站直播间开播订阅 用法: /live 房间号''' if len(args) == 0: msg = [] monitor_dict = readJSON(BILI_LIVE_JSON_PATH) if not isinstance(monitor_dict, dict): raise TypeError("Expected dict but found:", monitor_dict) for room_id, target in monitor_dict.items(): if room_id == "time": continue if (isinstance(subject, Member) and groupToStr(subject.group) in target) \ or (isinstance(subject, Friend) and subject.id in target): res = getLiveInfo(room_id) if isinstance(res, str): continue if res['isLive'] == 0: msg.append(Plain(res['name'] + " 未在直播.\n")) else: msg.append( Plain(res['name'] + " 正在直播 " + "[{}]{}\n{}".format( res["area_name"], res["title"], res["url"]))) msg.append(Image.fromNetworkAddress(res["keyframe"])) return MessageChain.create(msg) room_id = args[0] res = getLiveInfo(room_id) if isinstance(res, str): msg = [Plain("未找到该直播!")] logger.info("[LIVE]未找到该直播") else: monitor_dict = readJSON(BILI_LIVE_JSON_PATH) if not isinstance(monitor_dict, dict): raise TypeError("Expected dict but found:", monitor_dict) if room_id in monitor_dict.keys(): if isinstance(subject, Member) and groupToStr( subject.group) not in monitor_dict[room_id]: monitor_dict[room_id].append(groupToStr(subject.group)) elif isinstance(subject, Friend) and subject.id not in monitor_dict[room_id]: monitor_dict[room_id].append(subject.id) else: if isinstance(subject, Member): monitor_dict[room_id] = [groupToStr(subject.group)] elif isinstance(subject, Friend): monitor_dict[room_id] = [subject.id] updateJSON(BILI_LIVE_JSON_PATH, monitor_dict) if res['isLive'] == 0: msg = [Plain("已加入监视列表\n" + res['name'] + " 未在直播.")] else: msg = [ Plain("已加入监视列表\n" + res['name'] + " 正在直播 " + "[{}]{}\n{}".format( res["area_name"], res["title"], res["url"])), Image.fromNetworkAddress(res["keyframe"]) ] logger.info("[LIVE]返回成功") return MessageChain.create(msg)
async def hero_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''展示玩家英雄平均数据 用法: /hero (id) 英雄名''' if len(args) != 2: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /hero (id) 英雄名")]) query_id = args[0] if isinstance(subject, Member): dota_id = bot.db.get(subject.group, "dota_id").get(query_id) else: dota_id = bot.db.get(subject, "dota_id").get(query_id) if not dota_id: logger.info(f"[HERO]未添加该用户{query_id}") return MessageChain.create([Plain(f"未添加该用户{query_id}!")]) else: query_id = dota_id res = getDotaHero(query_id, args[1]) if isinstance(res, tuple): res = res[1] logger.info("[HERO]返回成功") elif res == 0: res = f"参数有误:{args[1]}" logger.info(f"[HERO]参数有误:{args[1]}") else: logger.info("[HERO]返回成功") return MessageChain.create([Plain(res)])
async def recommend_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''td金牌推荐舞见视频 用法: /recommend''' title, author, pic, url, racy = getRecommendDance() msg = [Plain(text="本次核心推荐up随机视频:\n")] for i, ti in enumerate(title): msg.append(Plain(str(i + 1) + ":" + ti + " by " + author[i] + "\n")) msg.append(Plain(url[i] + "\n")) msg.append(Plain("se指数(by Google):" + RACY_LIST[racy[i] - 1] + "\n")) msg.append(Image.fromNetworkAddress(pic[i])) # type: ignore msg.append(Plain("\n")) logger.info("[RECOMMEND]返回成功") return MessageChain.create(msg)
async def dance_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''B站舞蹈区排行 用法: /dance''' title, author, pic, url, racy = getTop3DanceToday() msg = [Plain("B站舞蹈区实时排名前3(已剔除潜在不适内容)\n")] for i, ti in enumerate(title): msg.append(Plain(str(i + 1) + ":" + ti + " by " + author[i] + "\n")) msg.append(Plain(url[i] + "\n")) msg.append(Plain("se指数(by Google):" + RACY_LIST[racy[i] - 1] + "\n")) msg.append(Image.fromNetworkAddress(pic[i])) # type: ignore msg.append(Plain("\n")) logger.info("[DANCE]返回成功") return MessageChain.create(msg)
async def up_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''订阅UP主投稿 用法: /up UP主uid''' if len(args) == 0: res = "目前关注的UP主有:\n" up_dict = readJSON(BILI_UP_JSON_PATH) if not isinstance(up_dict, dict): raise TypeError("Expected dict but found:", up_dict) for up, target in up_dict.items(): if up == "time": continue if (isinstance(subject, Member) and groupToStr(subject.group) in target) \ or (isinstance(subject, Friend) and subject.id in target): res += getNameByUid(up) + " " return MessageChain.create([Plain(res)]) up_id = args[0] up_name = getNameByUid(up_id) res = getCards(up_id) if isinstance(res, str): msg = [Plain("未找到该UP主!")] logger.info("[UP]未找到该UP主") else: up_dict = readJSON(BILI_UP_JSON_PATH) if not isinstance(up_dict, dict): raise TypeError("Expected dict but found:", up_dict) if up_id in up_dict.keys(): if isinstance(subject, Member) and groupToStr( subject.group) not in up_dict[up_id]: up_dict[up_id].append(groupToStr(subject.group)) if isinstance(subject, Friend) and subject.id not in up_dict[up_id]: up_dict[up_id].append(subject.id) else: if isinstance(subject, Member): up_dict[up_id] = [groupToStr(subject.group)] elif isinstance(subject, Friend): up_dict[up_id] = [subject.id] updateJSON(BILI_UP_JSON_PATH, up_dict) if len(res) == 0: msg = [Plain("已加入关注列表 " + up_name + " 暂无新投稿.")] else: msg = [Plain(f"已加入关注列表 {up_name}\n")] for i in res: msg.append(Plain(f"{up_name} 投稿了视频《{i['title']}》:{i['url']}\n")) msg.append(Image.fromNetworkAddress(i["pic"])) # type: ignore msg.append(Plain("\n")) logger.info("[UP]返回成功") return MessageChain.create(msg)
async def dota_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''展示最近24小时(上限10场)游戏数据 用法: /dota (id)''' if len(args) != 1: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args}, 用法: /dota (id)")]) query_id = args[0] if isinstance(subject, Member): dota_id = bot.db.get(subject.group, "dota_id").get(query_id) else: dota_id = bot.db.get(subject, "dota_id").get(query_id) if not dota_id: logger.info(f"[DOTA]未添加该用户{query_id}") return MessageChain.create([Plain(f"未添加该用户{query_id}!")]) else: res = getGamesIn24Hrs(dota_id) logger.info("[DOTA]返回成功") return MessageChain.create([Plain(f"{res}")])
async def story_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''dota战报展示 用法: /story 比赛id''' if len(args) != 1: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /story 比赛id")]) match_id = args[0] path = await getDotaStory(match_id) msg = None if not isinstance(path, str): if path == 404: msg = MessageChain.create([Plain(f"未找到比赛:{match_id}")]) logger.info(f"[STORY]未找到比赛:{match_id}") elif path == 1: msg = MessageChain.create([Plain(f"比赛{match_id}解析中,请等待2min后重试")]) logger.info(f"[STORY]解析中:{match_id}") else: msg = MessageChain.create([Image.fromLocalFile(path)]) logger.info("[STORY]返回成功") return msg
async def star_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''展示最近指定场数(默认20场)游戏五星图数据 用法: /star (id) (num)''' if len(args) < 1 or len(args) > 2: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /star (id) (num)")]) query_id, *num = args if isinstance(subject, Member): dota_id = bot.db.get(subject.group, "dota_id").get(query_id) else: dota_id = bot.db.get(subject, "dota_id").get(query_id) if not dota_id: logger.info(f"[STAR]未添加该用户{query_id}") return MessageChain.create([Plain(f"未添加该用户{query_id}!")]) else: if num and type(num[0]) == type(query_id) and query_id == num[0]: num = [20] query_id = dota_id args = 20 if len(num) == 1: try: args = int(num[0]) if args > 50 or args <= 0: args = 20 except ValueError: args = 20 pic_name, player_name = getStarStat(query_id, args) if isinstance(player_name, int): msg = MessageChain.create([Plain(pic_name)]) logger.info("[STAR]用户不存在") else: msg = MessageChain.create([ Plain(player_name + " 最近 " + str(args) + " 场游戏五星图\n"), Image.fromLocalFile(pic_name) ]) logger.info("[STAR]返回成功") return msg
async def star_compare_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''玩家间最近五星图对比 用法: /stcp (id_a) id_b (num)''' args = list(args) if len(args) < 2 or len(args) > 3: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /stcp (id_a) id_b (num)")]) try: num = int(args[-1]) ids = args[:len(args) - 1] except: num = 0 ids = list(args) for no, i in enumerate(ids): if isinstance(subject, Member): dota_id = bot.db.get(subject.group, "dota_id").get(i) else: dota_id = bot.db.get(subject, "dota_id").get(i) if not dota_id: logger.info("[STCP]未添加用户 " + i) return MessageChain.create([Plain("未添加用户 " + i + " !")]) ids[no] = dota_id else: if num <= 0 or num > 50: num = 20 pic_name, player_name_a, _ = getCompStarStat(ids[0], ids[1], num) if isinstance(player_name_a, int): msg = MessageChain.create([Plain(pic_name)]) logger.info("[STCP]用户不存在") else: msg = MessageChain.create([ Plain("最近 " + str(num) + " 场游戏数据比较图\n"), Image.fromLocalFile(pic_name) ]) logger.info("[STCP]返回成功") return msg
async def winrate_compare_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''玩家间最近胜率数据对比 用法: /wrcp id_a id_b (num)''' args = list(args) if len(args) < 2: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /wrcp id_a id_b (num)")]) try: num = int(args[-1]) ids = args[:len(args) - 1] except ValueError: num = 0 ids = args for no, i in enumerate(ids): if isinstance(subject, Member): dota_id = bot.db.get(subject.group, "dota_id").get(i) else: dota_id = bot.db.get(subject, "dota_id").get(i) if not dota_id: logger.info("[WRCP]未添加用户 " + i) return MessageChain.create([Plain("未添加用户 " + i + " !")]) ids[no] = dota_id else: if num <= 0 or num > 50: num = 20 pic_name, player_name_list = getCompWinRateGraph(ids, num) if type(player_name_list) == type(0): msg = MessageChain.create([Plain(pic_name)]) logger.info("[WRCP]用户不存在") else: msg = MessageChain.create([ Plain("最近 " + str(num) + " 场游戏胜率比较图\n"), Image.fromLocalFile(pic_name) ]) logger.info("[WRCP]返回成功") return msg
async def compare_handler(*args, bot: Bot, subject: Union[Member, Friend]): '''玩家间最近平均数据对比 用法: /comp (id_a) id_b (num)''' if len(args) < 2 or len(args) > 3: return MessageChain.create( [Plain(f"缺少参数或参数过多:{args},用法: /comp (id_a) id_b (num)")]) [id_a, id_b, *num] = args if type(id_a) == type(id_b) and id_a == id_b: id_b = num[0] if isinstance(subject, Member): dota_id_a = bot.db.get(subject.group, "dota_id").get(id_a) dota_id_b = bot.db.get(subject.group, "dota_id").get(id_b) else: dota_id_a = bot.db.get(subject, "dota_id").get(id_a) dota_id_b = bot.db.get(subject, "dota_id").get(id_b) if not dota_id_a: logger.info("[COMP]未添加用户 " + id_a) return MessageChain.create([Plain("未添加用户 " + id_a + " !")]) elif not dota_id_b: logger.info("[COMP]未添加用户 " + id_b) return MessageChain.create([Plain("未添加用户 " + id_b + " !")]) else: id_a = dota_id_a id_b = dota_id_b args = 20 if len(num) != 0: try: args = int(num[0]) if args > 50 or args <= 0: args = 20 except ValueError: args = 20 res = getLatestComparingStat(id_a, id_b, args) msg = MessageChain.create([Plain(res)]) logger.info("[COMP]返回成功") return msg