admin.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  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. def setup(bot: commands.Bot):
  9. bot.add_cog(Admin(bot))
  10. class Admin(commands.Cog):
  11. """Administrative functionality."""
  12. def __init__(self, bot: commands.Bot):
  13. self.bot = bot
  14. @commands.command(
  15. description="Modify channel settings",
  16. brief="Set channel specific settings via the webgui",
  17. help="Sends a single-use time based token to the webportal"
  18. )
  19. @commands.has_guild_permissions(administrator=True)
  20. async def chanset(self, ctx: commands.Context):
  21. record = await get_active_token(self.bot.pg, ctx.guild.id)
  22. if record: # Check for active token
  23. await ctx.send(f"Token {record['id']} is in use by {record['user']} until {plus10min(record['created'])}.")
  24. else: # No active token
  25. token = secrets.token_urlsafe(40)[:40]
  26. await upsert_token(self.bot.pg, ctx.guild.id, ctx.author.id, token)
  27. await ctx.author.send(f"{WEB_SCHEME}://{WEB_HOST}/config/channel-settings/{ctx.channel.id}/{token}") # DM token
  28. await ctx.send("Your access token has been sent to you in a private DM.")
  29. await report(self.bot, f"`{ctx.author}` has requested a token for `{ctx.guild.name}`.")