import logging import niobot from thefuzz import fuzz class InfoModule(niobot.Module): log = logging.getLogger(__name__) @niobot.command(name="room-info", aliases=["ri"]) async def room_info(self, ctx: niobot.Context, room: str = None): """ Dumps information about a room in a human readable format can be a room ID or alias. """ room = room or ctx.room.room_id 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 return await x.edit( "Sorry, I was unable to resolve that room alias.\n\n{!r}".format( 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: self.log.debug("%r is not in %r", ctx.event.sender, room_id) continue ratio = fuzz.ratio(room, room_obj.display_name) self.log.debug("Ratio for %r: %r", room_obj.display_name, ratio) if ratio > 1: matches.append((room_obj, ratio)) matches.sort( key=lambda v: v[1], 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." ) room = matches[0][0].room_id if room not in ctx.bot.rooms: return await ctx.respond( "Unknown room %r. Am I in it?" % room ) room_obj: niobot.MatrixRoom = ctx.bot.rooms[room] lines = [ "* 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}" ] body = "\n".join(lines).format( obj=room_obj, user_count=room_obj.member_count, invited_count=room_obj.invited_count ) return await ctx.respond(body)