admin.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #import discord
  2. import secrets
  3. from discord.ext import commands
  4. from local_settings import WEB_SCHEME, WEB_HOST
  5. from query.guild_access_token import get_active_token, upsert_token
  6. from common.datetime import plus10min
  7. from common.logging import report
  8. from query.user import is_ignored
  9. def setup(bot: commands.Bot):
  10. bot.add_cog(Admin(bot))
  11. class Admin(commands.Cog):
  12. """Administrative functionality."""
  13. def __init__(self, bot: commands.Bot):
  14. self.bot = bot
  15. @commands.command(
  16. description="Modify channel settings",
  17. brief="Set channel specific settings via the webgui",
  18. help="Sends a single-use time based token to the webportal"
  19. )
  20. @commands.has_guild_permissions(administrator=True)
  21. async def chanset(self, ctx: commands.Context):
  22. # Ignore user if on the ignore list.
  23. if await is_ignored(self.bot.pg, ctx.author.id):
  24. return
  25. record = await get_active_token(self.bot.pg, ctx.guild.id)
  26. if record: # Check for active token
  27. await ctx.send(f"Token {record['id']} is in use by {record['user']} until {plus10min(record['created'])}.")
  28. await report(self.bot, f"`{ctx.author}` has requested a token for `{ctx.guild.name}`, but got rejected until {plus10min(record['created'])} for use by `{record['user']}`.", ctx.guild)
  29. else: # No active token
  30. token = secrets.token_urlsafe(40)[:40]
  31. await upsert_token(self.bot.pg, ctx.guild.id, ctx.author.id, token)
  32. await ctx.author.send(f"{WEB_SCHEME}://{WEB_HOST}/config/channel-settings/{ctx.channel.id}/{token}") # DM token
  33. await ctx.send("Your access token has been sent to you in a private DM.")
  34. await report(self.bot, f"`{ctx.author}` has requested a token for `{ctx.guild.name}`.", ctx.guild)