def test_parse_uri(): assert libwampli.parse_uri("a.*.b").match_policy == aiowamp.MATCH_WILDCARD assert libwampli.parse_uri("a.**").match_policy == aiowamp.MATCH_PREFIX with pytest.raises(ValueError): libwampli.parse_uri("a.**.b") assert libwampli.parse_uri("*.b*.*.*") == "..b*..."
async def __connect(self) -> aiowamp.ClientABC: config = self.config client = await aiowamp.connect(config.endpoint, realm=config.realm) await asyncio.gather(*(client.subscribe(libwampli.parse_uri(topic), self.__on_event) for topic in self.subscriptions)) return client
async def sub(self, topic: str) -> None: if topic in self.subscriptions: return client = self.client if client: await client.subscribe(libwampli.parse_uri(topic), self.__on_event) self.subscriptions.add(topic)
async def on_ready(self) -> None: """Handler for when the bot is ready. Loads the channels for the subscriptions. """ for raw_conn_id, raw_item in iter_items(self._db): item = DBItem.unmarshal_json(raw_item) conn_id = int(raw_conn_id) subscriptions = {} for topic, channel_id in item.subscriptions.items(): channel = self.bot.get_channel(channel_id) if channel: subscriptions[libwampli.parse_uri(topic)] = channel else: log.warning(f"couldn't find channel {channel_id}") log.debug(f"loaded %s subscription channel(s) for id %s from database", len(subscriptions), conn_id) self._subscription_channels[conn_id] = subscriptions
async def unsubscribe_cmd(self, ctx: commands.Context, *topics: str) -> None: """Unsubscribe from a topic. You can also pass multiple topics to unsubscribe from. """ client = self._cmd_get_lazy_client(ctx) conn_id = get_conn_id(ctx) subscriptions = self.__get_channel_map(conn_id) unsubscribed = [] already_unsubscribed = [] for topic in topics: if topic not in client.subscriptions: already_unsubscribed.append(topic) continue await client.unsub(topic) del subscriptions[libwampli.parse_uri(topic)] unsubscribed.append(topic) self.__update_db_subscriptions(conn_id, subscriptions) embed = discord.Embed(colour=discord.Colour.green()) if not unsubscribed: embed.title = "Not subscribed to any topic" elif not already_unsubscribed: if len(unsubscribed) == 1: embed.title = f"Unsubscribed from {escape_dis(topics[0])}" else: embed.title = "Unsubscribed from all topics" else: embed.title = "Unsubscribed from some topics" embed.add_field(name="Unsubscribed", value="\n".join(map(escape_dis, unsubscribed)), inline=False) embed.add_field(name="Not subscribed", value="\n".join(map(escape_dis, already_unsubscribed)), inline=False) await ctx.send(embed=embed)
async def subscriptions_cmd(self, ctx: commands.Context) -> None: """See the subscriptions.""" subscriptions = self.__get_channel_map(get_conn_id(ctx)) embed = discord.Embed(colour=discord.Colour.blue()) if not subscriptions: embed.title = "No active subscriptions in this guild" await ctx.send(embed=embed) return embed.title = "Subscriptions" by_channel = {} for topic in self._cmd_get_lazy_client(ctx).subscriptions: channel = subscriptions[libwampli.parse_uri(topic)] by_channel.setdefault(channel, []).append(topic) for channel, topics in by_channel.items(): topics_str = "\n".join(f"- {escape_dis(topic)}" for topic in topics) embed.add_field(name=f"#{channel.name}", value=topics_str, inline=False) await ctx.send(embed=embed)