2019-02-16 16:29:44 +01:00
import aiohttp
import asyncio
import io
import json
2018-12-11 21:02:30 +01:00
import os
2018-07-24 22:47:57 +02:00
from datetime import datetime
2019-02-16 16:29:44 +01:00
from pluralkit . errors import TupperboxImportError
2018-07-24 22:47:57 +02:00
from pluralkit . bot . commands import *
2018-12-05 11:44:10 +01:00
async def import_root ( ctx : CommandContext ) :
2019-02-16 16:29:44 +01:00
# Only one import method rn, so why not default to Tupperbox?
await import_tupperbox ( ctx )
2018-12-11 19:42:42 +01:00
2018-12-11 20:52:43 +01:00
2019-02-16 16:29:44 +01:00
async def import_tupperbox ( ctx : CommandContext ) :
await ctx . reply ( " To import from Tupperbox, reply to this message with a `tuppers.json` file imported from Tupperbox. \n \n To obtain such a file, type `tul!export` (or your server ' s equivalent). " )
def predicate ( msg ) :
if msg . author . id != ctx . message . author . id :
2018-10-27 23:47:28 +02:00
return False
2019-02-16 16:29:44 +01:00
if msg . attachments :
if msg . attachments [ 0 ] . filename . endswith ( " .json " ) :
return True
return False
2018-09-07 17:34:38 +02:00
2018-12-11 00:08:51 +01:00
try :
2019-02-16 16:29:44 +01:00
message = await ctx . client . wait_for ( " message " , check = predicate , timeout = 60 * 5 )
2018-12-11 00:08:51 +01:00
except asyncio . TimeoutError :
2019-02-16 16:29:44 +01:00
raise CommandError ( " Timed out. Try running `pk;import` again. " )
2018-07-24 22:47:57 +02:00
2019-02-16 16:29:44 +01:00
s = io . BytesIO ( )
await message . attachments [ 0 ] . save ( s )
data = json . load ( s )
2018-09-07 23:03:05 +02:00
system = await ctx . get_system ( )
2019-02-16 16:29:44 +01:00
if not system :
2019-02-18 16:45:40 +01:00
system = await System . create_system ( ctx . conn , account_id = ctx . message . author . id )
2019-02-16 16:29:44 +01:00
result = await system . import_from_tupperbox ( ctx . conn , data )
tag_note = " "
if len ( result . tags ) > 1 :
tag_note = " \n \n PluralKit ' s tags work on a per-system basis. Since your Tupperbox members have more than one unique tag, PluralKit has not imported the tags. Set your system tag manually with `pk;system tag <tag>`. "
await ctx . reply_ok ( " Updated {} member {} , created {} member {} . Type `pk;system` to check! {} " . format (
len ( result . updated ) , " s " if len ( result . updated ) != 1 else " " ,
len ( result . created ) , " s " if len ( result . created ) != 1 else " " ,
tag_note
) )