2024-09-14 23:14:07 +01:00
|
|
|
import niobot
|
2024-09-14 23:20:19 +01:00
|
|
|
from thefuzz import fuzz
|
2024-09-14 23:14:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class InfoModule(niobot.Module):
|
2024-09-14 23:16:30 +01:00
|
|
|
@niobot.command(name="room-info", aliases=["ri"])
|
2024-09-14 23:14:07 +01:00
|
|
|
async def room_info(self, ctx: niobot.Context, room: str = None):
|
|
|
|
"""
|
|
|
|
Dumps information about a room in a human readable format
|
|
|
|
|
|
|
|
<room> can be a room ID or alias.
|
|
|
|
"""
|
2024-09-14 23:15:32 +01:00
|
|
|
room = room or ctx.room.room_id
|
2024-09-14 23:14:07 +01:00
|
|
|
if room.startswith("#"):
|
|
|
|
x = await ctx.respond("Resolving room...")
|
|
|
|
resolved = await ctx.bot.room_resolve_alias(room)
|
|
|
|
if not isinstance(resolved, niobot.RoomResolveAliasResponse):
|
|
|
|
resolved: niobot.RoomResolveAliasError
|
|
|
|
await x.edit(
|
|
|
|
"Sorry, I was unable to resolve that room alias.",
|
|
|
|
override={
|
|
|
|
"uk.co.nexy7574.nonsensebot.error": str(resolved).replace(
|
|
|
|
self.bot.access_token,
|
|
|
|
"[REDACTED]"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
room = resolved.room_id
|
|
|
|
await x.delete("Room resolved to " + room)
|
|
|
|
elif room.startswith("!"):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Search for the room
|
|
|
|
matches: list[tuple[niobot.MatrixRoom, float]] = []
|
|
|
|
for room_id, room_obj in ctx.bot.rooms.items():
|
|
|
|
if ctx.event.sender not in room_obj.users:
|
|
|
|
continue
|
2024-09-14 23:20:19 +01:00
|
|
|
ratio = fuzz.ratio(room, room_obj.display_name)
|
2024-09-14 23:14:07 +01:00
|
|
|
matches.append((room_obj, ratio))
|
|
|
|
matches.sort(
|
2024-09-14 23:21:18 +01:00
|
|
|
key=lambda v: v[1],
|
2024-09-14 23:14:07 +01:00
|
|
|
reverse=True
|
|
|
|
)
|
|
|
|
if not matches:
|
|
|
|
return await ctx.respond(
|
|
|
|
"[room] was not a room ID or alias, and I do not know of any mutual rooms that have"
|
|
|
|
" a similar name."
|
|
|
|
)
|
2024-09-14 23:21:44 +01:00
|
|
|
room = matches[0][0].room_id
|
2024-09-14 23:14:07 +01:00
|
|
|
|
|
|
|
if room not in ctx.bot.rooms:
|
|
|
|
return await ctx.respond(
|
2024-09-14 23:19:16 +01:00
|
|
|
"Unknown room %r. Am I in it?" % room
|
2024-09-14 23:14:07 +01:00
|
|
|
)
|
|
|
|
|
2024-09-14 23:15:32 +01:00
|
|
|
room_obj: niobot.MatrixRoom = ctx.bot.rooms[room]
|
2024-09-14 23:14:07 +01:00
|
|
|
lines = [
|
2024-09-14 23:17:31 +01:00
|
|
|
"* Name: {obj.display_name!r} (is explicitly named: {obj.is_named!s})",
|
|
|
|
"* ID: `{obj.room_id}`",
|
|
|
|
"* Version: {obj.room_version!s}",
|
|
|
|
"* Federated? {obj.federate!s}",
|
|
|
|
"* Encrypted? {obj.encrypted!s}",
|
|
|
|
"* Guest access: {obj.guest_access!r}",
|
|
|
|
"* Join rule: {obj.join_rule!r}",
|
|
|
|
"* History visibility: {obj.history_visibility!r}",
|
|
|
|
"* User count: {user_count:,} ({invited_count:,} invited)",
|
|
|
|
"* Avatar URL: `{obj.room_avatar_url!s}`",
|
|
|
|
"* Canonical alias: {obj.canonical_alias}"
|
2024-09-14 23:14:07 +01:00
|
|
|
]
|
|
|
|
body = "\n".join(lines).format(
|
|
|
|
obj=room_obj,
|
2024-09-14 23:16:30 +01:00
|
|
|
user_count=room_obj.member_count,
|
|
|
|
invited_count=room_obj.invited_count
|
2024-09-14 23:14:07 +01:00
|
|
|
)
|
|
|
|
return await ctx.respond(body)
|