Add commands for API token retrieval/refreshing

This commit is contained in:
Ske
2018-11-13 12:34:19 +01:00
parent a72a7c3de9
commit 6da7436aed
4 changed files with 60 additions and 7 deletions

View File

@@ -129,6 +129,7 @@ class CommandContext:
raise CommandError("Timed out - try again.")
import pluralkit.bot.commands.api_commands
import pluralkit.bot.commands.import_commands
import pluralkit.bot.commands.member_commands
import pluralkit.bot.commands.message_commands
@@ -179,7 +180,10 @@ async def command_dispatch(client: discord.Client, message: discord.Message, con
(r"switch move", switch_commands.switch_move),
(r"switch out", switch_commands.switch_out),
(r"switch", switch_commands.switch_member)
(r"switch", switch_commands.switch_member),
(r"token (refresh|expire|update)", api_commands.refresh_token),
(r"token", api_commands.get_token)
]
for pattern, func in commands:

View File

@@ -0,0 +1,31 @@
import logging
from discord import DMChannel
from pluralkit.bot.commands import CommandContext, CommandSuccess
logger = logging.getLogger("pluralkit.commands")
disclaimer = "Please note that this grants access to modify (and delete!) all your system data, so keep it safe and secure. If it leaks or you need a new one, you can invalidate this one with `pk;token refresh`."
async def reply_dm(ctx: CommandContext, message: str):
await ctx.message.author.send(message)
if not isinstance(ctx.message.channel, DMChannel):
return CommandSuccess("DM'd!")
async def get_token(ctx: CommandContext):
system = await ctx.ensure_system()
if system.token:
token = system.token
else:
token = await system.refresh_token(ctx.conn)
token_message = "Here's your API token: \n**`{}`**\n{}".format(token, disclaimer)
return await reply_dm(ctx, token_message)
async def refresh_token(ctx: CommandContext):
system = await ctx.ensure_system()
token = await system.refresh_token(ctx.conn)
token_message = "Your previous API token has been invalidated. You will need to change it anywhere it's currently used.\nHere's your new API token:\n**`{}`**\n{}".format(token, disclaimer)
return await reply_dm(ctx, token_message)