refactor(bot): improve code readability by reformatting

Refactor the ApplicationBot's methods for enhanced readability. This includes reformatting multiline structures and improving code consistency by adjusting indentation and ensuring uniform string formatting. These changes do not alter functionality but streamline future maintenance and readability. No functional impact or changes to logic were made.
This commit is contained in:
Kumi 2024-11-08 08:26:18 +01:00
parent ddc9b05fed
commit ac18806308
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -78,7 +78,7 @@ class ApplicationBot:
"body": "Hello! To open a support ticket, please type `!supportbot openticket`.", "body": "Hello! To open a support ticket, please type `!supportbot openticket`.",
}, },
) )
async def member_event_callback(self, room: MatrixRoom, event: RoomMemberEvent): async def member_event_callback(self, room: MatrixRoom, event: RoomMemberEvent):
# Check if the room is monitored # Check if the room is monitored
monitored_rooms = await self.get_monitored_rooms() monitored_rooms = await self.get_monitored_rooms()
@ -97,9 +97,13 @@ class ApplicationBot:
elif command.startswith("!supportbot close"): elif command.startswith("!supportbot close"):
await self.close_ticket(room, sender, command) await self.close_ticket(room, sender, command)
elif command.startswith("!supportbot approve"): elif command.startswith("!supportbot approve"):
await self.approve_or_reject_application(room, sender, command, approved=True) await self.approve_or_reject_application(
room, sender, command, approved=True
)
elif command.startswith("!supportbot reject"): elif command.startswith("!supportbot reject"):
await self.approve_or_reject_application(room, sender, command, approved=False) await self.approve_or_reject_application(
room, sender, command, approved=False
)
elif command == "!supportbot list": elif command == "!supportbot list":
await self.list_tickets(room) await self.list_tickets(room)
elif command.startswith("!supportbot monitor"): elif command.startswith("!supportbot monitor"):
@ -144,7 +148,11 @@ class ApplicationBot:
await self.relay_message(room, sender, message_body) await self.relay_message(room, sender, message_body)
async def approve_or_reject_application(self, room, sender, command, approved): async def approve_or_reject_application(self, room, sender, command, approved):
user_id = command.split()[2] if len(command.split()) > 2 else await self.get_user_id_from_ticket_room(room.room_id) user_id = (
command.split()[2]
if len(command.split()) > 2
else await self.get_user_id_from_ticket_room(room.room_id)
)
if not user_id: if not user_id:
await self.client.room_send( await self.client.room_send(
@ -163,7 +171,9 @@ class ApplicationBot:
for protected_room in protected_rooms: for protected_room in protected_rooms:
invite_response = await self.client.room_invite(protected_room, user_id) invite_response = await self.client.room_invite(protected_room, user_id)
if not isinstance(invite_response, RoomInviteResponse): if not isinstance(invite_response, RoomInviteResponse):
logging.error(f"Failed to invite user {user_id} to protected room {protected_room}: {invite_response}") logging.error(
f"Failed to invite user {user_id} to protected room {protected_room}: {invite_response}"
)
await self.client.room_send( await self.client.room_send(
room.room_id, room.room_id,
@ -188,7 +198,9 @@ class ApplicationBot:
for monitored_room in monitored_rooms: for monitored_room in monitored_rooms:
kick_response = await self.client.room_kick(monitored_room, user_id) kick_response = await self.client.room_kick(monitored_room, user_id)
if not isinstance(kick_response, RoomKickResponse): if not isinstance(kick_response, RoomKickResponse):
logging.error(f"Failed to kick user {user_id} from monitored room {monitored_room}: {kick_response}") logging.error(
f"Failed to kick user {user_id} from monitored room {monitored_room}: {kick_response}"
)
async def get_user_id_from_ticket_room(self, room_id): async def get_user_id_from_ticket_room(self, room_id):
# Find the user associated with the ticket id based on the room id # Find the user associated with the ticket id based on the room id
@ -230,13 +242,13 @@ class ApplicationBot:
await self.client.room_put_state( await self.client.room_put_state(
room_id=self.operator_room_id, room_id=self.operator_room_id,
event_type="m.room.monitored", event_type="m.room.monitored",
content={"rooms": rooms} content={"rooms": rooms},
) )
async def list_monitored_rooms(self, room): async def list_monitored_rooms(self, room):
monitored_rooms = await self.get_monitored_rooms() monitored_rooms = await self.get_monitored_rooms()
if monitored_rooms: if monitored_rooms:
rooms_list = ', '.join(monitored_rooms) rooms_list = ", ".join(monitored_rooms)
await self.client.room_send( await self.client.room_send(
room.room_id, room.room_id,
"m.room.message", "m.room.message",
@ -281,13 +293,13 @@ class ApplicationBot:
await self.client.room_put_state( await self.client.room_put_state(
room_id=self.operator_room_id, room_id=self.operator_room_id,
event_type="m.room.protected", event_type="m.room.protected",
content={"rooms": rooms} content={"rooms": rooms},
) )
async def list_protected_rooms(self, room): async def list_protected_rooms(self, room):
protected_rooms = await self.get_protected_rooms() protected_rooms = await self.get_protected_rooms()
if protected_rooms: if protected_rooms:
rooms_list = ', '.join(protected_rooms) rooms_list = ", ".join(protected_rooms)
await self.client.room_send( await self.client.room_send(
room.room_id, room.room_id,
"m.room.message", "m.room.message",
@ -430,13 +442,19 @@ class ApplicationBot:
await self.client.room_send( await self.client.room_send(
operator_room_id, operator_room_id,
"m.room.message", "m.room.message",
{"msgtype": "m.text", "body": f"Application #{ticket_id} has been closed."}, {
"msgtype": "m.text",
"body": f"Application #{ticket_id} has been closed.",
},
) )
else: else:
await self.client.room_send( await self.client.room_send(
room.room_id, room.room_id,
"m.room.message", "m.room.message",
{"msgtype": "m.text", "body": f"Application #{ticket_id} does not exist."}, {
"msgtype": "m.text",
"body": f"Application #{ticket_id} does not exist.",
},
) )
async def list_tickets(self, room): async def list_tickets(self, room):
@ -529,4 +547,4 @@ class ApplicationBot:
"msgtype": "m.text", "msgtype": "m.text",
"body": help_message, "body": help_message,
}, },
) )