Skip to content

Commit d31c803

Browse files
add .acl show_pretty command
1 parent 44adbc3 commit d31c803

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

bot/acl.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ def parse_data(data: ACLData) -> ACLExpr:
104104
return NestedACL(data["acl"])
105105
raise ValueError("Invalid ACL data: {!r}".format(data))
106106

107+
@staticmethod
108+
def format_markdown(data: ACLData, indent: int = 0) -> str:
109+
"""
110+
Return a markdown-formatted string representation of the ACL's data,
111+
converting IDs into Discord mentions where possible.
112+
"""
113+
pad = " " * indent + "- "
114+
115+
if "role" in data:
116+
return f"{pad}role: <@&{data['role']}>"
117+
elif "user" in data:
118+
return f"{pad}user: <@{data['user']}>"
119+
elif "channel" in data:
120+
return f"{pad}channel: <#{data['channel']}>"
121+
elif "category" in data:
122+
cat = data["category"]
123+
return f"{pad}category: {f'<#{cat}>' if cat else '*(none)*'}"
124+
elif "not" in data:
125+
inner = ACL.format_markdown(data["not"], indent + 1)
126+
return f"{pad}not:\n{inner}"
127+
elif "and" in data:
128+
parts = [ACL.format_markdown(d, indent + 1) for d in data["and"]]
129+
return f"{pad}and:\n" + "\n".join(parts)
130+
elif "or" in data:
131+
parts = [ACL.format_markdown(d, indent + 1) for d in data["or"]]
132+
return f"{pad}or:\n" + "\n".join(parts)
133+
elif "acl" in data:
134+
return f"{pad}acl: `{data['acl']}`"
135+
raise ValueError(f"Invalid ACL data: {data!r}")
136+
107137
def parse(self) -> ACLExpr:
108138
return ACL.parse_data(self.data)
109139

plugins/db_manager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Dict, List, Optional, Set, Union, cast
33

44
import asyncpg
5+
import discord
56
from discord.ext.commands import Greedy, command, group
67
from sqlalchemy import select
78
from sqlalchemy.ext.asyncio import AsyncSession
@@ -158,6 +159,17 @@ async def acl_show(ctx: Context, acl: str) -> None:
158159
await ctx.send(format("{!b:yaml}", yaml.dump(data.data)))
159160

160161

162+
@acl_command.command("show_pretty")
163+
@privileged
164+
async def acl_show_pretty(ctx: Context, acl: str) -> None:
165+
"""Show the formula for the given ACL, formatted as markdown."""
166+
async with AsyncSession(util.db.engine) as session:
167+
if (acl_obj := await session.get(bot.acl.ACL, acl)) is None:
168+
raise UserError(format("No such ACL: {!i}", acl))
169+
170+
await ctx.send(ACL.format_markdown(acl_obj.data), allowed_mentions=discord.AllowedMentions.none())
171+
172+
161173
acl_override = register_action("acl_override")
162174

163175

0 commit comments

Comments
 (0)