title: How To Prevent Leveling Up From Spam For A Python Discord Bot
body: <p>So, recently I've been working on a discord bot in python that levels you up while you talk in a discord server, many popular bots like Carl Bot and MEE6 both have functions like this which I find to be quite interesting; so I wanted to try and recreate it.  The bot works perfectly fine, but it does have a few caveats which I would like to try and have fixed, for one there is no way to check your level or get a leader board for the top 10 people in the server.  But, the issue I want to find and resolve here is the fact that you can spam messages; messages that make no sense and many messages that only need to be one or two.  So, how do I do this, how can I prevent spam in my system for creating this.  Any help would be appreciated!</p>
  <pre><code>from discord.ext import commands
  import json
  import discord
  import os
  import random
  import time
  
  client = commands.Bot(command_prefix = &quot;!&quot;)
  
  #On Ready Event.
  @client.event
  async def on_ready():
    print(&quot;Bot Ready.&quot;)
    time.sleep(1)
    print(&quot;Logged In As:  {0.user}&quot;.format(client))
  
  #Function For Getting A Users Information And Level Them Up.
  @client.event
  async def on_member_join(member):
    with open(&quot;users.json&quot;, &quot;r&quot;) as f:
      users = json.load(f)
  
    await update_data(users, member)
  
    with open(&quot;users.json&quot;, &quot;w&quot;) as f:
      json.dump(users, f)
  
  #Function For Sending Messages.
  @client.event
  async def on_message(message):
    with open(&quot;users.json&quot;, &quot;r&quot;) as f:
      users = json.load(f)
  
    await update_data(users, message.author)
    await add_experience(users, message.author, 5)
    await level_up(users, message.author, message.channel)
  
    with open(&quot;users.json&quot;, &quot;w&quot;) as f:
      json.dump(users, f)
  
  async def update_data(users, user):
    if not str(user.id) in users:
      users[str(user.id)] = {}
      users[str(user.id)][&quot;experience&quot;] = 0
      users[str(user.id)][&quot;level&quot;] = 1
  
  async def add_experience(users, user, exprience):
    users[str(user.id)][&quot;experience&quot;] += exprience
  
  async def level_up(users, user, channel):
    experience = users[str(user.id)][&quot;experience&quot;]
    level_start = users[str(user.id)][&quot;level&quot;]
    level_end = int(experience ** (1/4))
  
    if level_start &lt; level_end:
      await channel.send(f&quot;{user.mention} Has Leveled Up!  They Have Leveled Up To Level {level_end}!&quot;)
      users[str(user.id)][&quot;level&quot;] = level_end
  
  client.run(os.getenv(&quot;Token&quot;))
  client.run(os.environ[&quot;Token&quot;])
  </code></pre>