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@commands.command() # uses command decorators, in this case inside a cog
2@commands.has_permissions(ban_members=True) # only people that have permissions to ban users can use this command
3async def ban(self, ctx, user: discord.Member, *, reason): # The person banning someone has to ping the user to ban, and input a reason. Remove self if you are outside a cog.
4 await ctx.guild.ban(user, reason=reason) # Bans the user.
5 await user.send(f"You have been banned in {ctx.guild} for {reason}") # Private messages user.
6 await ctx.send(f"{user} has been successfully banned.") # messages channel to tell everyone it worked
1
2async def update_data(users, user):
3 if not f'{user.id}' in users:
4 users[f'{user.id}'] = {}
5 users[f'{user.id}']['experience'] = 0
6 users[f'{user.id}']['level'] = 1
7
8
9async def add_experience(users, user, exp):
10 users[f'{user.id}']['experience'] += exp
11
12
13async def level_up(users, user, message):
14 with open('levels.json', 'r') as g:
15 levels = json.load(g)
16 experience = users[f'{user.id}']['experience']
17 lvl_start = users[f'{user.id}']['level']
18 lvl_end = int(experience ** (1 / 4))
19 if lvl_start < lvl_end:
20 await message.channel.send(f'{user.mention} has leveled up to level {lvl_end}')
21 users[f'{user.id}']['level'] = lvl_end
22
23@client.command()
24async def level(ctx, member: discord.Member = None):
25 if not member:
26 id = ctx.message.author.id
27 with open('users.json', 'r') as f:
28 users = json.load(f)
29 lvl = users[str(id)]['level']
30 await ctx.send(f'You are at level {lvl}!')
31 else:
32 id = member.id
33 with open('users.json', 'r') as f:
34 users = json.load(f)
35 lvl = users[str(id)]['level']
36 await ctx.send(f'{member} is at level {lvl}!')
37
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