Fix command handling and error messages

This commit is contained in:
shenjack 2023-12-22 00:17:10 +08:00
parent 5ab5f2082e
commit 0c29753b68
Signed by: shenjack
GPG Key ID: 7B1134A979775551

29
cmds.py
View File

@ -70,30 +70,31 @@ def cmds(app, data):
action="store_true") action="store_true")
args = parser.parse_args(sqt.args) args = parser.parse_args(sqt.args)
reply = ReplyMessage(id=sqt.message_id)
e: AsyncEnforcer = sqt.app.ctx.e e: AsyncEnforcer = sqt.app.ctx.e
msg = '' msg = ''
if args.addgroup: if args.addgroup:
if args.group and args.command: if args.group and args.command:
if await e.add_policy(args.group, args.command): if await e.add_policy(args.group, args.command):
msg = Message(content=f'添加成功p, {args.group}, {args.command}', room_id=sqt.room_id) msg = Message(content=f'添加成功p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id) msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if args.adduser: if args.adduser:
if args.group: if args.group:
if await e.add_role_for_user(str(args.user or sqt.room_id), args.group): if await e.add_role_for_user(str(args.user or sqt.room_id), args.group):
msg = Message(content=f'添加成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id) msg = Message(content=f'添加成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id) msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if len(sqt.args) == 0: if len(sqt.args) == 0:
parser.parse_args(["-h"]) parser.parse_args(["-h"])
else: else:
if msg == '': if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy() await e.save_policy()
@ -113,25 +114,25 @@ def cmds(app, data):
if args.rmgroup: if args.rmgroup:
if args.group and args.command: if args.group and args.command:
if await e.remove_policy(args.group, args.command): if await e.remove_policy(args.group, args.command):
msg = Message(content=f'移除成功p, {args.group}, {args.command}', room_id=sqt.room_id) msg = Message(content=f'移除成功p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id) msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if args.rmuser: if args.rmuser:
if args.group: if args.group:
if await e.delete_role_for_user(str(args.user or sqt.room_id), args.group): if await e.delete_role_for_user(str(args.user or sqt.room_id), args.group):
msg = Message(content=f'移除成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id) msg = Message(content=f'移除成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id) msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if len(sqt.args) == 0: if len(sqt.args) == 0:
parser.parse_args(["-h"]) parser.parse_args(["-h"])
else: else:
if msg == '': if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy() await e.save_policy()