def waiter(waiter_friend: Friend, waiter_message: MessageChain): if all([ waiter_friend.id == kwargs[Friend].id, ]): if waiter_message.asDisplay() in confirm_command: return True else: return False
async def reFriend(app: GraiaMiraiApplication, sender: Friend, message: MessageChain): text = message.asDisplay().strip().replace('\r\n', '\n').replace('\r', '\n') keywords = ['个人课表', '个人课程表', 'selfcourse', 'owncourse', 'self course', 'own course', 'privatecourse'] if any([text.lower().startswith(opt) for opt in ('.owncourse', '。owncourse', '.groupcourse', '。groupcourse')]): msg = await Command.parseCommand(text, qq=sender.id) if not msg: msg = '命令执行失败,请检查格式' await app.sendFriendMessage(sender, MessageChain.create([Plain(msg)])) elif (text.lower() in keywords) or at_me(app, message) and any(keyword in text.lower() for keyword in keywords): await app.sendFriendMessage(sender, MessageChain.create([await getCourseTable(f'qq{sender.id}')]))
def waiter(waiter_group: Group, waiter_member: Member, waiter_message: MessageChain): if all([ waiter_group.id == kwargs[Group].id, waiter_member.id == kwargs[Member].id, ]): if waiter_message.asDisplay() in confirm_command: return True else: return False
async def handle_manager_msg(self, message: MessageChain, sender, friend): reply = await self.ero_image.solve_receive_msg(message) if reply is not None: await sender(Plain(reply)) return if config.SERVER_FLAG: for cmd in Arknights.cmd: if re.search(cmd, message.asDisplay()): await Arknights().run(sender) return
def __find_ranges(self, message: MessageChain) -> T.Tuple[int, int]: """ 找出消息中指定的排行范围 :return: 排行范围的列表 """ content = message.asDisplay() regex = "[1-9][0-9]*-[1-9][0-9]*" res = re.search(regex, content) if res is None: res = re.search(regex, self.default_range) begin, end = res.group().split('-') return int(begin), int(end)
def __find_ranking_mode(self, message: MessageChain) -> T.Optional[str]: """ 找出消息中所指定的排行榜种类 :return: 排行榜种类,若没有则为None """ content = message.asDisplay() for key in self.trigger: for x in self.trigger[key]: if x in content: if key == "default": return self.default_ranking_mode else: return key return None
async def solve_receive_msg(self, message: MessageChain): plain = message.asDisplay() if re.search(EroImage.cmd_receive, plain): self.set_receive_flag(True) return self.msg_receive elif re.search(EroImage.cmd_receive_done, plain): self.set_receive_flag(False) return self.msg_receive_done if self.receive_flag: images = message.get(Image) if len(images) == 0: return False else: return await self.receive_image(images)
async def handle(self, app: GraiaMiraiApplication, subject: T.Union[Group, Friend], message: MessageChain, channel: asyncio.Queue) -> bool: # 检测是否触发 accept = False content = message.asDisplay() for x in self.trigger: if (self.trigger_mode == "match" and x == content) or (self.trigger_mode == "search" and x in content): accept = True break if not accept: return False await channel.put(self.text) return True
async def handle(self, message: MessageChain, sender, friend=None, group=None, member=None): plain = message.asDisplay() if friend is not None: log.i(friend.id, Config().get_manager_id()) if friend.id in Config().get_manager_id(): await self.handle_manager_msg(message, sender, friend) return for cmd in EroImage.cmd: if re.search(cmd, plain): img = EroImage().get_rand_img() log.i("img: %s" % img) await sender(img, 10.0, Plain("别冲了别冲了")) return reply = self.corpus.compile(plain, None) log.i("answer: %s", reply) await sender(Plain(reply))
def __find_number(self, message: MessageChain) -> T.Optional[int]: """ 找出消息中所指定的数字(随机多少张书签) :return: 数字。若未触发则为None,若未指定数字则为1 """ content = message.asDisplay() for x in self.trigger: result = match_groups(x, ["$number"], content) if result is None: continue number = result["$number"] if number is None or number == "": number = 1 elif number.isdigit(): number = int(number) else: number = decode_chinese_int(number) return number return None
async def handle(self, app: GraiaMiraiApplication, subject: T.Union[Group, Friend], message: MessageChain, channel: asyncio.Queue) -> bool: # 检测是否触发 accept = False content = message.asDisplay() for x in self.trigger: if x in content: accept = True break if not accept: return False # 提取消息中的所有id regex = re.compile("[1-9][0-9]*") ids = [int(x) for x in regex.findall(content)] logger.info(f"{self.tag}: {ids}") # 每个id建立一个task,以获取插画并扔到channel中 async def make_msg(illust_id): try: result = await launch(papi.illust_detail, illust_id=illust_id) if "error" in result: raise PixivResultError(result["error"]) else: msg = await make_illust_message(result["illust"]) logger.info( f"""{self.tag}: [{result["illust"]["id"]}] ok""") except Exception as exc: msg = self.handle_and_make_error_message(exc) await channel.put(msg) tasks = [] for x in ids: tasks.append(asyncio.create_task(make_msg(x))) await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED) return True
def __find_attrs(self, message: MessageChain) -> T.Optional[T.Tuple[str, int]]: """ 找出消息中的搜索关键字 :return: 搜索关键字,若未触发则为None """ content = message.asDisplay() for x in self.trigger: result = match_groups(x, ["$illustrator", "$number"], content) if result is None: continue illustrator, number = result["$illustrator"], result["$number"] if number is None or number == "": number = 1 elif number.isdigit(): number = int(number) else: number = decode_chinese_int(number) return illustrator, number return None
def __find_attrs(self, message: MessageChain) -> T.Optional[T.Tuple[str, int]]: """ 找出消息中所指定的关键字和数字 :return: 关键字和数字。若未触发则为None,若未指定关键字则为"",若未指定数字则为1 """ content = message.asDisplay() for x in self.trigger: result = match_groups(x, ["$keyword", "$number"], content) if result is None: continue keyword, number = result["$keyword"], result["$number"] if keyword is None: keyword = "" if number is None or number == "": number = 1 elif number.isdigit(): number = int(number) else: number = decode_chinese_int(number) return keyword, number return None
async def command(app: GraiaMiraiApplication, sender: T.Union[Member, Friend], event_type: "Type", message: MessageChain): text = message.asDisplay().strip().replace('\r', '\n') if text.lower().startswith(r'.monitor') or text.lower().startswith( r'。monitor'): app_reply = reply(app, sender, event_type) obj = re.match( r'\s*[.。]monitor\s*(group|qq)?\s*(\d+)?\s*add\s*(xg|jwc|sist|all|hello)', text.lower(), re.I) if obj: tp, num, web = obj.groups() if all((web, tp, num)): Command.add(web, tp, num) await app_reply( f'已为{alias.get(tp, tp)}{num}添加{alias.get(web, web)}监控') elif web and not tp and not num: tp, num = ('qq' if event_type == 'FriendMessage' else 'group', sender.id if event_type == 'FriendMessage' else sender.group.id) Command.add(web, tp, num) await app_reply( f'已为{alias.get(tp, tp)}{num}添加{alias.get(web, web)}监控') else: await app_reply('命令格式错误') config.save() return obj = re.match( r'\s*[.。]monitor\s*(group|qq)?\s*(\d+)?\s*rm\s*(xg|jwc|sist|all|hello)', text.lower(), re.I) if obj: tp, num, web = obj.groups() if all((web, tp, num)): Command.rm(web, tp, num) await app_reply( f'已为{alias.get(tp, tp)}{num}移除{alias.get(web, web)}监控') elif web and not tp and not num: tp, num = ('qq' if event_type == 'FriendMessage' else 'group', sender.id if event_type == 'FriendMessage' else sender.group.id) Command.rm(web, tp, num) await app_reply( f'已为{alias.get(tp, tp)}{num}移除{alias.get(web, web)}监控') else: await app_reply('命令格式错误') config.save() return obj = re.match(r'[.。]monitor\s*list', text.lower()) if obj: msglist = [] msg = "" for item in config.jwc: if 'qq' in item: msglist.append(f"QQ{item['qq']}") elif 'group' in item: msglist.append(f"群{item['group']}") if msglist: msg = msg + f"教务处: {', '.join(msglist)}\n" msglist = [] for item in config.xg: if 'qq' in item: msglist.append(f"QQ{item['qq']}") elif 'group' in item: msglist.append(f"群{item['group']}") if msglist: msg = msg + f"学工: {', '.join(msglist)}\n" msglist = [] for item in config.sist: if 'qq' in item: msglist.append(f"QQ{item['qq']}") elif 'group' in item: msglist.append(f"群{item['group']}") if msglist: msg = msg + f"信院: {', '.join(msglist)}\n" if msg == "": await app_reply("暂无配置") else: await app_reply(msg[:-1]) return obj = re.match( r'\s*[.。]monitor\s*(group|qq)?\s*(\d+)?\s*(shield|unshield)\s*(xg|jwc|sist|all)\s*(contain|startwith|endwith|equal|re)\s*([\S\s]+)', text, re.I) if obj: tp, num, shield, web, cmdType, cmd = obj.groups() unshield = False if shield.lower() == 'shield' else True qq = None group = None tp = tp.lower() if tp else tp web = web.lower() if web else web cmdType = cmdType.lower() if cmdType else cmdType if not tp: tp, num = ('qq' if event_type == 'FriendMessage' else 'group', sender.id if event_type == 'FriendMessage' else sender.group.id) if event_type == 'FriendMessage': qq = sender.id else: group = sender.group.id else: if tp == 'qq': qq = int(num) else: group = int(num) Command.shield(web=web, rule={ cmdType: cmd, 'unshield': unshield }, qq=qq, group=group) config.save() await app_reply( f"已{'取消屏蔽' if unshield else '屏蔽'}来自{alias.get(web, web)}的符合规则{cmdType}: {cmd}的消息" ) return await app_reply("命令格式错误")
async def replyGroup(app: GraiaMiraiApplication, sender: Member, message: MessageChain): keywords = ['课程表', '.course', 'course'] text = message.asDisplay().strip().lower().replace('\r', '\n') if (text in keywords) or (at_me(app, message) and any([keyword in text for keyword in keywords])): msg = MessageChain.create([courseTable, Plain(timeTable)]) await app.sendGroupMessage(sender.group, msg)
def cmdFilter(self, message: MessageChain): dis: str = message.asDisplay().split(' ') if dis[0].upper() not in self.APP_COMMANDS: raise ExecutionStop()