async def _fetch_message(self, count=100) -> ApiResult[List[Event]]: res = await self._api.fetchMessage(count=count, request_method='get') events = [] if res.get('code') == 0: for ev in res.get('data', []): event = self.as_event(ev) if event: events.append(event) return ApiResult(Code.Success, events) return ApiResult(res.get('code'))
async def get_message( self, message_id: MessageIdType, ) -> ApiResult[MessageEvent]: res: dict = await self._api.messageFromId(id=message_id, request_method='get') if res.get('code') == 0: return ApiResult(Code.Success, self.as_event(res['data'])) else: return ApiResult(res.get('code'))
async def set_group_unmute( self, group: ContactIdType, qq: Optional[ContactIdType], ) -> ApiResult[None]: if qq: res = await self._api.unmute(target=group, memberId=qq, request_method='post') return ApiResult(res.get('code')) else: res = await self._api.muteAll(target=group, request_method='post') return ApiResult(res.get('code'))
async def set_group_kick( self, group: ContactIdType, qq: ContactIdType, ) -> ApiResult[None]: await self._api.set_group_kick(group_id=group, user_id=qq) return ApiResult(Code.Success)
async def send_friend_message( self, qq: ContactIdType, message: Message_T, ) -> ApiResult[MessageSendResult]: message = await self.prepare_message(message) res: dict = await self._api.send_private_msg( user_id=qq, message=self.as_cq_chain(message)) return ApiResult(Code.Success, MessageSendResult(res['message_id']))
async def get_group_list(self) -> ApiResult[List[Group]]: res = await self._api.groupList(request_method='get') groups = [] for g in res: groups.append( Group( id=g.get('id'), name=g.get('name'), permission=self._role_to_permission[g.get('permission')], )) return ApiResult(Code.Success, groups)
async def get_friend_list(self) -> ApiResult[List[Friend]]: res = await self._api.get_friend_list() friends = [] for friend in res: friends.append( Friend( id=friend.get('user_id'), name=friend.get('nickname'), remark=friend.get('remark'), )) return ApiResult(Code.Success, friends)
async def get_group_list(self) -> ApiResult[List[Group]]: res = await self._api.get_group_list() groups = [] for g in res: groups.append( Group( id=g.get('group_id'), name=g.get('group_name'), permission=GroupPermission.NONE, )) return ApiResult(Code.Success, groups)
async def set_group_unmute( self, group: ContactIdType, qq: Optional[ContactIdType], ) -> ApiResult[None]: if qq: await self._api.set_group_ban(group_id=group, user_id=qq, duration=0) else: await self._api.set_group_whole_ban(group_id=group, enable=False) return ApiResult(Code.Success)
async def send_friend_message( self, qq: ContactIdType, message: Message_T, ) -> ApiResult[MessageSendResult]: token = upload_method.set(METHOD_FRIEND) msg = await self.prepare_message(message) res: dict = await self._api.sendFriendMessage( qq=qq, messageChain=self.as_mirai_chain(msg)) upload_method.reset(token) return ApiResult(res.get('code'), MessageSendResult(res.get('messageId')))
async def wrapper(self, *args, **kwargs): while True: try: return await func(self, *args, **kwargs) except ApiError as e: if e.code == ApiError.CODE_SESSION_FAILED: logger.info('Re-login Mirai') if not await self.relogin(): return ApiResult(Code.Unavailable) if await self._api.verify(qq=self.qq): logger.info('Re-login Success') continue else: return ApiResult(Code.Unavailable) else: return ApiResult(e.code) except Exception as e: logger.exception(e) return ApiResult(Code.Unspecified, message=str(e))
async def get_group_member_info( self, group: ContactIdType, qq: ContactIdType, ) -> ApiResult[GroupMember]: res = await self._api.get_group_member_info(group_id=group, user_id=qq) return ApiResult( Code.Success, GroupMember( id=res.get('user_id'), name=res.get('nickname'), permission=self._role_to_permission[res.get('role')], ))
async def wrapper(self, *args, **kwargs): try: return await func(self, *args, **kwargs) except Exception as e: logger.exception(e) if (not _ALLOW_RETRY): if isinstance(self, CQSession): self.handle_event_nowait( ApiNotSuccessfulEvent(func.__name__, self.qq, args, kwargs)) return ApiResult(Code.Unspecified, message=str(e)) logger.info("Retrying...") try: return await func(*args, **kwargs) except Exception as e: logger.exception(e) logger.error("Retrying failed") if isinstance(self, CQSession): self.handle_event_nowait( ApiNotSuccessfulEvent(func.__name__, self.qq, args, kwargs)) return ApiResult(Code.Unspecified, message=str(e))
async def get_group_member_list( self, group: ContactIdType, ) -> ApiResult[List[GroupMember]]: res = await self._api.get_group_member_list(group_id=group) members = [] for member in res: members.append( GroupMember( id=member.get('user_id'), name=member.get('card') or member.get('nickname'), permission=self._role_to_permission[member.get('role')], )) return ApiResult(Code.Success, members)
async def get_group_member_list( self, group: ContactIdType, ) -> ApiResult[List[GroupMember]]: res = await self._api.memberList(target=group, request_method='get') members = [] for member in res: members.append( GroupMember( id=member.get('id'), name=member.get('memberName'), permission=self._role_to_permission[member.get( 'permission')], )) return ApiResult(Code.Success, members)
async def get_group_member_info( self, group: ContactIdType, qq: ContactIdType, ) -> ApiResult[GroupMember]: res = await self._api.memberInfo(target=group, memberId=qq, request_method='get') return ApiResult( Code.Success, GroupMember( id=qq, name=res.get('name'), permission=self._role_to_permission[res.get('permission')], ))
async def send_group_message( self, group: ContactIdType, message: Message_T, ) -> ApiResult[MessageSendResult]: message = await self.prepare_message(message) # Go-CQHTTP Forward Message if message.get_first(raw_message.Forward): res: dict = await self._api.send_group_forward_msg( group_id=group, messages=self._as_cq_forward_nodes( message.get_first(raw_message.Forward))) else: res: dict = await self._api.send_group_msg( group_id=group, message=self.as_cq_chain(message)) return ApiResult(Code.Success, MessageSendResult(res['message_id']))
async def send_group_message( self, group: ContactIdType, message: Message_T, ) -> ApiResult[MessageSendResult]: token = upload_method.set(METHOD_GROUP) msg = await self.prepare_message(message) quote = msg.get_first(Quote) if quote: msg.remove(quote) res: dict = await self._api.sendGroupMessage( group=group, messageChain=self.as_mirai_chain(msg), quote=quote.id) else: res: dict = await self._api.sendGroupMessage( group=group, messageChain=self.as_mirai_chain(msg)) upload_method.reset(token) return ApiResult(res.get('code'), MessageSendResult(res.get('messageId')))
async def recall( self, message_id: MessageIdType, ) -> ApiResult[None]: res: dict = await self._api.recall(target=message_id) return ApiResult(res.get('code'))
async def set_group_leave( self, group: ContactIdType, ) -> ApiResult[None]: await self._api.set_group_leave(group_id=group) return ApiResult(Code.Success)
async def recall( self, message_id: MessageIdType, ) -> ApiResult[None]: res: dict = await self._api.delete_msg(message_id=message_id) return ApiResult(Code.Success)