refactor(bot): simplify command handling logic
Streamlined command handling by decoupling the bot's prefix from commands, reducing redundancy. Commands are now directly matched after splitting the prefix. This change enhances readability, maintains clarity, and potentially reduces command parsing errors. It also ensures commands are processed more consistently. This update should not affect current usage but may facilitate future extensions to command handling rules.
This commit is contained in:
parent
24a095c2d5
commit
6b8ccd0336
1 changed files with 26 additions and 23 deletions
|
@ -62,12 +62,15 @@ class ApplicationBot:
|
|||
sender = event.sender
|
||||
body = event.body if hasattr(event, "body") else None
|
||||
|
||||
if body and body.startswith("!applicationbot"):
|
||||
await self.handle_command(room, sender, body)
|
||||
elif body and body.startswith("!apply") and not sender == self.client.user_id:
|
||||
await self.process_application(room, sender, event)
|
||||
else:
|
||||
await self.relay_message(room, sender, event)
|
||||
if body:
|
||||
mention = body.split()[0]
|
||||
if mention == "!applicationbot" or mention in self.username:
|
||||
return await self.handle_command(room, sender, body.split(" ", 1)[1])
|
||||
|
||||
if body and body.startswith("!apply") and not sender == self.client.user_id:
|
||||
return await self.process_application(room, sender, event)
|
||||
|
||||
return await self.relay_message(room, sender, event)
|
||||
|
||||
async def invite_callback(self, room: MatrixRoom, event: InviteMemberEvent):
|
||||
logging.info(f"Received invite event: {event}")
|
||||
|
@ -178,43 +181,43 @@ class ApplicationBot:
|
|||
)
|
||||
|
||||
async def handle_command(self, room, sender, command):
|
||||
if command == "!applicationbot help":
|
||||
if command == "help":
|
||||
return await self.help_command(room, sender)
|
||||
elif command == "!applicationbot openticket":
|
||||
elif command == "openticket":
|
||||
return await self.open_ticket(room, sender)
|
||||
elif await self.is_operator(sender):
|
||||
if command.startswith("!applicationbot invite"):
|
||||
if command.startswith("invite"):
|
||||
return await self.invite_operator(room, sender, command)
|
||||
elif command.startswith("!applicationbot close"):
|
||||
elif command.startswith("close"):
|
||||
return await self.close_ticket(room, sender, command)
|
||||
elif command.startswith("!applicationbot approve"):
|
||||
elif command.startswith("approve"):
|
||||
return await self.approve_or_reject_application(
|
||||
room, sender, command, approved=True
|
||||
)
|
||||
elif command.startswith("!applicationbot reject"):
|
||||
elif command.startswith("reject"):
|
||||
return await self.approve_or_reject_application(
|
||||
room, sender, command, approved=False
|
||||
)
|
||||
elif command == "!applicationbot list":
|
||||
elif command == "list":
|
||||
return await self.list_tickets(room)
|
||||
elif command.startswith("!applicationbot monitor"):
|
||||
room_id = self.resolve_room_id(command.split()[2])
|
||||
elif command.startswith("monitor"):
|
||||
room_id = self.resolve_room_id(command.split()[1])
|
||||
return await self.add_monitored_room(room_id)
|
||||
elif command.startswith("!applicationbot unmonitor"):
|
||||
room_id = self.resolve_room_id(command.split()[2])
|
||||
elif command.startswith("unmonitor"):
|
||||
room_id = self.resolve_room_id(command.split()[1])
|
||||
return await self.remove_monitored_room(room_id)
|
||||
elif command.startswith("!applicationbot monitored"):
|
||||
elif command.startswith("monitored"):
|
||||
return await self.list_monitored_rooms(room)
|
||||
elif command.startswith("!applicationbot protect "):
|
||||
elif command.startswith("protect "):
|
||||
try:
|
||||
room_id = await self.resolve_room_id(command.split()[2])
|
||||
room_id = await self.resolve_room_id(command.split()[1])
|
||||
return await self.add_protected_room(room_id)
|
||||
except IndexError:
|
||||
logging.error(f"No room ID specified for protect command: {command}")
|
||||
elif command.startswith("!applicationbot unprotect"):
|
||||
room_id = self.resolve_room_id(command.split()[2])
|
||||
elif command.startswith("unprotect"):
|
||||
room_id = self.resolve_room_id(command.split()[1])
|
||||
return await self.remove_protected_room(room_id)
|
||||
elif command.startswith("!applicationbot protected"):
|
||||
elif command.startswith("protected"):
|
||||
return await self.list_protected_rooms(room)
|
||||
|
||||
await self.client.room_send(
|
||||
|
|
Loading…
Reference in a new issue