1@client.event
2async def on_member_join(member):
3 with open('users.json', 'r') as f:
4 users = json.load(f)
5
6 await update_data(users, member)
7
8 with open('users.json', 'w') as f:
9 json.dump(users, f)
10
11
12@client.event
13async def on_message(message):
14 if message.author.bot == False:
15 with open('users.json', 'r') as f:
16 users = json.load(f)
17
18 await update_data(users, message.author)
19 await add_experience(users, message.author, 5)
20 await level_up(users, message.author, message)
21
22 with open('users.json', 'w') as f:
23 json.dump(users, f)
24
25 await client.process_commands(message)
26
27
28async def update_data(users, user):
29 if not f'{user.id}' in users:
30 users[f'{user.id}'] = {}
31 users[f'{user.id}']['experience'] = 0
32 users[f'{user.id}']['level'] = 1
33
34
35async def add_experience(users, user, exp):
36 users[f'{user.id}']['experience'] += exp
37
38
39async def level_up(users, user, message):
40 with open('levels.json', 'r') as g:
41 levels = json.load(g)
42 experience = users[f'{user.id}']['experience']
43 lvl_start = users[f'{user.id}']['level']
44 lvl_end = int(experience ** (1 / 4))
45 if lvl_start < lvl_end:
46 await message.channel.send(f'{user.mention} has leveled up to level {lvl_end}')
47 users[f'{user.id}']['level'] = lvl_end
48
49@client.command()
50async def level(ctx, member: discord.Member = None):
51 if not member:
52 id = ctx.message.author.id
53 with open('users.json', 'r') as f:
54 users = json.load(f)
55 lvl = users[str(id)]['level']
56 await ctx.send(f'You are at level {lvl}!')
57 else:
58 id = member.id
59 with open('users.json', 'r') as f:
60 users = json.load(f)
61 lvl = users[str(id)]['level']
62 await ctx.send(f'{member} is at level {lvl}!')
1@client.event
2async def on_member_join(member):
3 with open('users.json', 'r') as f:
4 users = json.load(f)
5
6 await update_data(users, member)
7
8 with open('users.json', 'w') as f:
9 json.dump(users, f)
10
11
12@client.event
13async def on_message(message):
14 if message.author.bot == False:
15 with open('users.json', 'r') as f:
16 users = json.load(f)
17
18 await update_data(users, message.author)
19 await add_experience(users, message.author, 5)
20 await level_up(users, message.author, message)
21
22 with open('users.json', 'w') as f:
23 json.dump(users, f)
24
25 await client.process_commands(message)
26
27
28async def update_data(users, user):
29 if not f'{user.id}' in users:
30 users[f'{user.id}'] = {}
31 users[f'{user.id}']['experience'] = 0
32 users[f'{user.id}']['level'] = 1
33
34
35async def add_experience(users, user, exp):
36 users[f'{user.id}']['experience'] += exp
37
38
39async def level_up(users, user, message):
40 with open('levels.json', 'r') as g:
41 levels = json.load(g)
42 experience = users[f'{user.id}']['experience']
43 lvl_start = users[f'{user.id}']['level']
44 lvl_end = int(experience ** (1 / 4))
45 if lvl_start < lvl_end:
46 await message.channel.send(f'{user.mention} has leveled up to level {lvl_end}')
47 users[f'{user.id}']['level'] = lvl_end
48
49@client.command()
50async def level(ctx, member: discord.Member = None):
51 if not member:
52 id = ctx.message.author.id
53 with open('users.json', 'r') as f:
54 users = json.load(f)
55 lvl = users[str(id)]['level']
56 await ctx.send(f'You are at level {lvl}!')
57 else:
58 id = member.id
59 with open('users.json', 'r') as f:
60 users = json.load(f)
61 lvl = users[str(id)]['level']
62 await ctx.send(f'{member} is at level {lvl}!')
63