import secrets from models import User from sio_model import SioDecorator, Message, SioRequest def cmds(app, data): # 此处会检查注册的指令是否重复 sio_decorator = SioDecorator(app, data) @sio_decorator.cmd('ping') async def ping(sqt: SioRequest): msg = Message(content='pong', room_id=sqt.room_id) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) @sio_decorator.cmd('gitea') async def gitea(sqt: SioRequest): parser = sqt.parser parser.add_argument('-vd', '--validate', help='绑定QQ与gitea', action="store_true") args = parser.parse_args(sqt.args) if args.validate: state = secrets.token_urlsafe(16) user = await User.filter(id=sqt.sender_id).get_or_none() if user: user.name = sqt.sender_name user.state = state await user.save() else: user = User(id=sqt.sender_id, name=sqt.sender_name, state=state) await user.save() url = (f'{app.ctx.sio_config.validate_host}/login/oauth/authorize?' f'client_id={app.ctx.sio_config.client_id}&' f'redirect_uri={app.ctx.sio_config.localhost}/redirect&' f'response_type=code&state={state}') msg = Message(content=f'click: \n{url}', room_id=sqt.room_id) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) if len(sqt.args) == 0: parser.parse_args(["-h"]) return sio_decorator