fix: handle unknown commands and correct async indentation errors

Added a response for unknown commands to guide users on valid usage, improving user experience and preventing misuse. Fixed indentation for async functions to ensure correct code execution. These changes address issues with command handling and function performance.
This commit is contained in:
Kumi 2024-08-07 12:22:17 +02:00
parent 840ae90376
commit 309e4968c3
Signed by: kumi
GPG key ID: ECBCC9082395383F

14
bot.py
View file

@ -48,6 +48,11 @@ class RoombaBot:
await self.block_room(event.body.split()[2], True) await self.block_room(event.body.split()[2], True)
elif event.body.startswith("!roomba unblock"): elif event.body.startswith("!roomba unblock"):
await self.block_room(event.body.split()[2], False) await self.block_room(event.body.split()[2], False)
elif event.body.startswith("!roomba "):
await self.send_message(
self.moderation_room_id,
"Unknown command. Use '!roomba block <room_id>' or '!roomba unblock <room_id>'.",
)
async def block_room(self, room_id, block): async def block_room(self, room_id, block):
"""Block or unblock a room. """Block or unblock a room.
@ -84,7 +89,6 @@ class RoombaBot:
f"Failed to {'block' if block else 'unblock'} room {room_id}.", f"Failed to {'block' if block else 'unblock'} room {room_id}.",
) )
async def get_local_users(self, room_id): async def get_local_users(self, room_id):
"""Get the local users in a room. """Get the local users in a room.
@ -94,7 +98,9 @@ async def get_local_users(self, room_id):
Returns: Returns:
list: The list of local users in the room. list: The list of local users in the room.
""" """
members_url = f"{self.client.homeserver}/_matrix/client/r0/rooms/{room_id}/members" members_url = (
f"{self.client.homeserver}/_matrix/client/r0/rooms/{room_id}/members"
)
headers = { headers = {
"Authorization": f"Bearer {self.client.access_token}", "Authorization": f"Bearer {self.client.access_token}",
"Content-Type": "application/json", "Content-Type": "application/json",
@ -108,7 +114,9 @@ async def get_local_users(self, room_id):
members = await resp.json() members = await resp.json()
for member in members.get("chunk", []): for member in members.get("chunk", []):
user_id = member.get("user_id") user_id = member.get("user_id")
if user_id and user_id.endswith(self.client.user_id.split(":")[1]): if user_id and user_id.endswith(
self.client.user_id.split(":")[1]
):
local_users.append(user_id) local_users.append(user_id)
return local_users return local_users