Add approve and reject method to RequestSession

This commit is contained in:
Richard Chien 2018-06-30 18:26:48 +08:00
parent 0c6468fa70
commit 4c07626124
2 changed files with 35 additions and 3 deletions

View File

@ -1,6 +1,6 @@
from typing import Dict, Any, Optional, Callable, Union from typing import Dict, Any, Optional, Callable, Union
from aiocqhttp import CQHttp from aiocqhttp import CQHttp, Error as CQHttpError
from aiocqhttp.bus import EventBus from aiocqhttp.bus import EventBus
from .session import BaseSession from .session import BaseSession
@ -44,7 +44,33 @@ class RequestSession(BaseSession):
def __init__(self, bot: CQHttp, ctx: Dict[str, Any]): def __init__(self, bot: CQHttp, ctx: Dict[str, Any]):
super().__init__(bot, ctx) super().__init__(bot, ctx)
# TODO: 添加 approve、deny 等方法 async def approve(self, remark: str = ''):
# TODO: should use ".handle_quick_operation" action in the future
try:
if self.ctx['request_type'] == 'friend':
await self.bot.set_friend_add_request(**self.ctx,
approve=True,
remark=remark)
elif self.ctx['request_type'] == 'group':
await self.bot.set_group_add_request(**self.ctx,
type=self.ctx['sub_type'],
approve=True)
except CQHttpError:
pass
async def reject(self, reason: str = ''):
# TODO: should use ".handle_quick_operation" action in the future
try:
if self.ctx['request_type'] == 'friend':
await self.bot.set_friend_add_request(**self.ctx,
approve=False)
elif self.ctx['request_type'] == 'group':
await self.bot.set_group_add_request(**self.ctx,
type=self.ctx['sub_type'],
approve=False,
reason=reason)
except CQHttpError:
pass
async def handle_notice_or_request(bot: CQHttp, ctx: Dict[str, Any]) -> None: async def handle_notice_or_request(bot: CQHttp, ctx: Dict[str, Any]) -> None:

View File

@ -1,6 +1,6 @@
from aiocqhttp import Error as CQHttpError from aiocqhttp import Error as CQHttpError
from none import on_notice, NoticeSession from none import on_notice, NoticeSession, on_request, RequestSession
GROUP_GREETING = ( GROUP_GREETING = (
'欢迎新同学 {name}[][CQ:face,id=63][CQ:face,id=63][CQ:face,id=63]', '欢迎新同学 {name}[][CQ:face,id=63][CQ:face,id=63][CQ:face,id=63]',
@ -21,3 +21,9 @@ async def _(session: NoticeSession):
await session.send_expr(GROUP_GREETING, name=name, **session.ctx) await session.send_expr(GROUP_GREETING, name=name, **session.ctx)
except CQHttpError: except CQHttpError:
pass pass
@on_request('group')
async def _(session: RequestSession):
if session.ctx['group_id'] == 672076603:
await session.approve()