admin.py 1.2 KB

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