41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
import asyncio,re
|
|||
|
|
from discord.ext import commands
|
||
|
|
def uid(s):
|
||
|
|
m=re.fullmatch(r"<@!?(\d+)>|(\d+)",s.strip())
|
||
|
|
return int(m[1] or m[2]) if m else None
|
||
|
|
class perm(commands.Cog):
|
||
|
|
def __init__(s,bot):
|
||
|
|
s.bot=bot;s.names={};s.sem=asyncio.Semaphore(2)
|
||
|
|
async def cog_check(s,ctx):return ctx.author.id==s.bot.user.id
|
||
|
|
async def name(s,i):
|
||
|
|
if i in s.names:return s.names[i]
|
||
|
|
u=s.bot.get_user(i)
|
||
|
|
if u is None:
|
||
|
|
async with s.sem:
|
||
|
|
try:u=await s.bot.fetch_user(i)
|
||
|
|
except Exception:u=None
|
||
|
|
await asyncio.sleep(.6)
|
||
|
|
n=(u.global_name or u.name) if u else "unknown"
|
||
|
|
if u:s.names[i]=n
|
||
|
|
return n
|
||
|
|
@commands.group(invoke_without_command=True)
|
||
|
|
async def perm(s,ctx):await ctx.send("`;perm list|add <id|mention>|remove <id|mention>`")
|
||
|
|
@perm.command(name="list")
|
||
|
|
async def _list(s,ctx):
|
||
|
|
ids=sorted(s.bot.wl.ids)
|
||
|
|
if not ids:return await ctx.send("whitelist empty")
|
||
|
|
n=await asyncio.gather(*(s.name(i) for i in ids))
|
||
|
|
await ctx.send("\n".join(f"{i} - {x}" for i,x in zip(ids,n)))
|
||
|
|
@perm.command(name="add")
|
||
|
|
async def _add(s,ctx,target:str):
|
||
|
|
i=uid(target)
|
||
|
|
if i is None:return await ctx.send("invalid id")
|
||
|
|
await s.bot.wl.mutate(i,True);s.bot.hook.put(f"whitelist: {i} added by {ctx.author.id}")
|
||
|
|
await ctx.send(f"{i} added ({await s.name(i)})")
|
||
|
|
@perm.command(name="remove")
|
||
|
|
async def _remove(s,ctx,target:str):
|
||
|
|
i=uid(target)
|
||
|
|
if i is None:return await ctx.send("invalid id")
|
||
|
|
await s.bot.wl.mutate(i,False);s.bot.hook.put(f"whitelist: {i} removed by {ctx.author.id}")
|
||
|
|
await ctx.send(f"{i} removed")
|
||
|
|
async def setup(bot):await bot.add_cog(perm(bot))
|