2018-11-15 21:05:13 +01:00
from pluralkit . bot . commands import CommandContext
2018-11-13 12:34:19 +01:00
2019-03-31 04:05:13 +09:30
disclaimer = " \u26A0 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`. "
2018-11-13 12:34:19 +01:00
2018-12-05 11:44:10 +01:00
async def token_root ( ctx : CommandContext ) :
if ctx . match ( " refresh " ) or ctx . match ( " expire " ) or ctx . match ( " invalidate " ) or ctx . match ( " update " ) :
await token_refresh ( ctx )
else :
await token_get ( ctx )
2018-11-13 12:34:19 +01:00
2018-12-05 11:44:10 +01:00
async def token_get ( ctx : CommandContext ) :
2018-11-13 12:34:19 +01:00
system = await ctx . ensure_system ( )
if system . token :
token = system . token
else :
token = await system . refresh_token ( ctx . conn )
2019-03-27 08:49:34 +09:30
token_message = " {} \n \u2705 Here ' s your API token: " . format ( disclaimer )
if token :
2019-03-31 04:05:13 +09:30
await ctx . reply_ok ( " DM ' d! " )
2019-03-27 08:49:34 +09:30
await ctx . message . author . send ( token_message )
await ctx . message . author . send ( token )
return
2018-11-13 12:34:19 +01:00
2018-12-05 11:44:10 +01:00
async def token_refresh ( ctx : CommandContext ) :
2018-11-13 12:34:19 +01:00
system = await ctx . ensure_system ( )
token = await system . refresh_token ( ctx . conn )
2019-03-27 08:49:34 +09:30
token_message = " Your previous API token has been invalidated. You will need to change it anywhere it ' s currently used. \n {} \n \u2705 Here ' s your new API token: " . format ( disclaimer )
if token :
await ctx . message . author . send ( token_message )
await ctx . message . author . send ( token )