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:
Kumi 2024-11-10 18:54:58 +01:00
parent 24a095c2d5
commit 6b8ccd0336
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -62,12 +62,15 @@ class ApplicationBot:
sender = event.sender sender = event.sender
body = event.body if hasattr(event, "body") else None body = event.body if hasattr(event, "body") else None
if body and body.startswith("!applicationbot"): if body:
await self.handle_command(room, sender, body) mention = body.split()[0]
elif body and body.startswith("!apply") and not sender == self.client.user_id: if mention == "!applicationbot" or mention in self.username:
await self.process_application(room, sender, event) return await self.handle_command(room, sender, body.split(" ", 1)[1])
else:
await self.relay_message(room, sender, event) 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): async def invite_callback(self, room: MatrixRoom, event: InviteMemberEvent):
logging.info(f"Received invite event: {event}") logging.info(f"Received invite event: {event}")
@ -178,43 +181,43 @@ class ApplicationBot:
) )
async def handle_command(self, room, sender, command): async def handle_command(self, room, sender, command):
if command == "!applicationbot help": if command == "help":
return await self.help_command(room, sender) return await self.help_command(room, sender)
elif command == "!applicationbot openticket": elif command == "openticket":
return await self.open_ticket(room, sender) return await self.open_ticket(room, sender)
elif await self.is_operator(sender): elif await self.is_operator(sender):
if command.startswith("!applicationbot invite"): if command.startswith("invite"):
return await self.invite_operator(room, sender, command) 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) return await self.close_ticket(room, sender, command)
elif command.startswith("!applicationbot approve"): elif command.startswith("approve"):
return await self.approve_or_reject_application( return await self.approve_or_reject_application(
room, sender, command, approved=True room, sender, command, approved=True
) )
elif command.startswith("!applicationbot reject"): elif command.startswith("reject"):
return await self.approve_or_reject_application( return await self.approve_or_reject_application(
room, sender, command, approved=False room, sender, command, approved=False
) )
elif command == "!applicationbot list": elif command == "list":
return await self.list_tickets(room) return await self.list_tickets(room)
elif command.startswith("!applicationbot monitor"): elif command.startswith("monitor"):
room_id = self.resolve_room_id(command.split()[2]) room_id = self.resolve_room_id(command.split()[1])
return await self.add_monitored_room(room_id) return await self.add_monitored_room(room_id)
elif command.startswith("!applicationbot unmonitor"): elif command.startswith("unmonitor"):
room_id = self.resolve_room_id(command.split()[2]) room_id = self.resolve_room_id(command.split()[1])
return await self.remove_monitored_room(room_id) return await self.remove_monitored_room(room_id)
elif command.startswith("!applicationbot monitored"): elif command.startswith("monitored"):
return await self.list_monitored_rooms(room) return await self.list_monitored_rooms(room)
elif command.startswith("!applicationbot protect "): elif command.startswith("protect "):
try: 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) return await self.add_protected_room(room_id)
except IndexError: except IndexError:
logging.error(f"No room ID specified for protect command: {command}") logging.error(f"No room ID specified for protect command: {command}")
elif command.startswith("!applicationbot unprotect"): elif command.startswith("unprotect"):
room_id = self.resolve_room_id(command.split()[2]) room_id = self.resolve_room_id(command.split()[1])
return await self.remove_protected_room(room_id) return await self.remove_protected_room(room_id)
elif command.startswith("!applicationbot protected"): elif command.startswith("protected"):
return await self.list_protected_rooms(room) return await self.list_protected_rooms(room)
await self.client.room_send( await self.client.room_send(