title: discord.py Levelling System using json. How can I add these few things to my code?
body: <p>So I have this code for a Discord levelling system where every time a user sends a message in a server that my bot is in, they gain a random amount of xp between 5 and 10. This is the code:</p>
  
  <pre><code>@bot.event
  async def on_member_join(member):
      with open("users.json", "r") as f:
          users = json.load(f)
  
          await update_data(users, member)
  
          with open("users.json", "w") as f:
              json.dump(users, f)
  
  @bot.event
  async def on_message(message):
      with open("users.json", "r") as f:
          users = json.load(f)
  
          if message.author.bot:
              return
          else:
              await update_data(users, message.author)
              number = random.randint(5,10)
              await add_experience(users, message.author, number)
              await level_up(users, message.author, message.channel)
  
          with open("users.json", "w") as f:
              json.dump(users, f)
      await bot.process_commands(message)
  
  async def update_data(users, user):
      if not user.id in users:
          users[user.id] = {}
          users[user.id]["experience"] = 0
          users[user.id]["level"] = 1
  
  async def add_experience(users, user, exp):
      users[user.id]["experience"] += exp
  
  async def level_up(users, user, channel):
      experience = users[user.id]["experience"]
      lvl_start = users[user.id]["level"]
      lvl_end = int(experience ** (1/4))
  
      if lvl_start &lt; lvl_end:
          await bot.send_message(channel, f":tada: Congrats {user.mention}, you levelled up to level {lvl_end}!")
          users[user.id]["level"] = lvl_end
  </code></pre>
  
  <p>This works fine, but there are several things I'd like to add/change/modify to improve it. They are listed below:</p>
  
  <p>a) I only want a user to get xp from messages once a minute or once every 30 seconds to prevent spam levelling up.</p>
  
  <p>b) I want it to also put the server into the json file, so that way each user has a unique level for each server, because currently, their xp gets carried across all servers they and the bot share.</p>
  
  <p>c) Currently, the bot also gives the user xp if the user dm's the bot, which obviously I don't want. Is there a way to stop the user getting xp if it dm's the bot, I can't just make the bot ignore dm's because for some commands the user and the bot need to use them.</p>
  
  <p>Any help would be gratefully appreciated. Even if you can only answer 1 question, or give an idea or opinion, that would be great.</p>
  
  <p>Thanks</p>
  
  <p>ChiefJack</p>
  
  <p>EDIT: I have all of my questions answered now, here they are in code, hope this helps (please don't copy my code, thanks):</p>
  
  <pre><code>@bot.event
  async def on_message(message):
      with open("users.json", "r") as f:
          users = json.load(f)
  
          if message.author.bot:
              return
          if message.channel.is_private:
              return
          else:
              await update_data(users, message.author, message.server)
              number = random.randint(5,10)
              await add_experience(users, message.author, number, message.server)
              await level_up(users, message.author, message.channel, message.server)
  
          with open("users.json", "w") as f:
              json.dump(users, f)
      await bot.process_commands(message)
  
  async def update_data(users, user, server):
      if not user.id + "-" + server.id in users:
          users[user.id + "-" + server.id] = {}
          users[user.id + "-" + server.id]["experience"] = 0
          users[user.id + "-" + server.id]["level"] = 1
          users[user.id + "-" + server.id]["last_message"] = 0
  
  async def add_experience(users, user, exp, server):
      if time.time() - users[user.id + "-" + server.id]["last_message"] &gt; 30: 
          users[user.id + "-" + server.id]["experience"] += exp
          users[user.id + "-" + server.id]["last_message"] = time.time()
      else:
          return
  
  async def level_up(users, user, channel, server):
      experience = users[user.id + "-" + server.id]["experience"]
      lvl_start = users[user.id + "-" + server.id]["level"]
      lvl_end = int(experience ** (1/4))
  
      if lvl_start &lt; lvl_end:
          await bot.send_message(channel, f":tada: Congrats {user.mention}, you levelled up to level {lvl_end}!")
          users[user.id + "-" + server.id]["level"] = lvl_end
  </code></pre>