11import asyncio
22from asyncio .tasks import wait
3+ from datetime import date
34import codingame
45import discord
56import random
910
1011with open ("./config/config.json" , "r" ) as cjson :
1112 config = json .load (cjson )
13+ with open ("./config/db.json" , "r" ) as dbjson :
14+ db = json .load (dbjson )
1215bot = commands .Bot (command_prefix = config ["prefix" ])
1316client = codingame .Client ()
1417
@@ -22,12 +25,53 @@ async def on_ready():
2225 presences = ["!game to join a game" , "!profile to get a profile" , "coding ..." ]
2326 while not bot .is_closed :
2427 presence = random .choice (presences )
25- await bot .change_presence (activity = discord . Game ( name = presence ) )
28+ await bot .change_presence (activity = presence )
2629 await asyncio .sleep (6 )
2730
31+ @bot .command (name = "unlink" )
32+ async def unlink (ctx ):
33+ with open ("./config/db.json" , "r+" ) as file :
34+ file_data = json .load (file )
35+ try :
36+ if not file_data [str (ctx .author .id )]["user" ]:
37+ await ctx .send ("You are not linked, use !link to link your codingame profile" )
38+ else :
39+ tmp = file_data [str (ctx .author .id )]["user" ]
40+ file_data [str (ctx .author .id )]["user" ] = ""
41+ await ctx .send ("Succesfully unlinked from " + tmp )
42+ except KeyError :
43+ await ctx .send ("You are not linked, use !link to link your codingame profile" )
44+ with open ("./config/db.json" , "w+" ) as fp :
45+ json .dump (file_data , fp , sort_keys = True , indent = 4 )
46+
47+ @bot .command (name = "link" )
48+ async def link (ctx , arg ):
49+ with open ("./config/db.json" , "r+" ) as file :
50+ file_data = json .load (file )
51+ try :
52+ if not file_data [str (ctx .author .id )]["user" ]:
53+ file_data [str (ctx .author .id )]["user" ] = arg
54+ await ctx .send ("Succesfully linked to " + arg )
55+ else :
56+ await ctx .send ("You are already linked, use !unlink to reset your link" )
57+ except KeyError :
58+ file_data [str (ctx .author .id )] = {"user" : arg }
59+ await ctx .send ("Succesfully linked to " + arg )
60+ with open ("./config/db.json" , "w+" ) as fp :
61+ json .dump (file_data , fp , sort_keys = True , indent = 4 )
62+
2863@bot .command (name = "profil" )
29- async def profil (ctx , arg ):
30- codingamer = client .get_codingamer (arg )
64+ async def profil (ctx , arg = None ):
65+ if not arg :
66+ file = open ("./config/db.json" , "r+" )
67+ file_data = json .load (file )
68+ if not file_data [str (ctx .author .id )]["user" ]:
69+ await ctx .send ("This command required an argument or to be linked" )
70+ return (84 )
71+ else :
72+ codingamer = client .get_codingamer (file_data [str (ctx .author .id )]["user" ])
73+ else :
74+ codingamer = client .get_codingamer (arg )
3175 embed = discord .Embed (title = codingamer .pseudo , url = "https://www.codingame.com/profile/" + codingamer .public_handle , color = Color .orange ())
3276 embed .add_field (name = "Clash Of Code Global rank:" , value = str (codingamer .get_clash_of_code_rank ()) + " ème" , inline = True )
3377 embed .add_field (name = "Global Rank" , value = str (codingamer .rank ) + " ème" , inline = True )
0 commit comments