0

I need antispam function on my discord server. Please help me. I tried this:

    import datetime
    import time

    time_window_milliseconds = 5000
    max_msg_per_window = 5
    author_msg_times = {}

    @client.event 
async def on_ready():
  print('logged in as {0.user}'.format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing,name="stack overflow"))

@client.event 
async def on_message(message):
    global author_msg_counts
    
    ctx = await client.get_context(message)

      author_id = ctx.author.id
      # Get current epoch time in milliseconds
      curr_time = datetime.datetime.now().timestamp() * 1000
    
      # Make empty list for author id, if it does not exist
      if not author_msg_times.get(author_id, False):
          author_msg_times[author_id] = []
    
      # Append the time of this message to the users list of message times
      author_msg_times[author_id].append(curr_time)
    
      # Find the beginning of our time window.
      expr_time = curr_time - time_window_milliseconds
    
      # Find message times which occurred before the start of our window
      expired_msgs = [
          msg_time for msg_time in author_msg_times[author_id]
          if msg_time < expr_time
      ]
    
      # Remove all the expired messages times from our list
      for msg_time in expired_msgs:
          author_msg_times[author_id].remove(msg_time)
      # ^ note: we probably need to use a mutex here. Multiple threads
      # might be trying to update this at the same time. Not sure though.
    
      if len(author_msg_times[author_id]) > max_msg_per_window:
          await ctx.send("Stop Spamming")

ping()
client.run(os.getenv('token'))

And it doesn't seem to work when I type the same message over and over again. Can you guys please help me? I need the good antispam function which will work inside on_message

Dweller
  • 15
  • 1
  • 9
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour), read [what's on-topic here](https://stackoverflow.com/help/on-topic), [How to Ask](https://stackoverflow.com/help/how-to-ask), and the [question checklist](https://meta.stackoverflow.com/q/260648/843953), and provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Ceres Apr 24 '21 at 13:28
  • "Implement this feature for me" or "how do I do this" is off-topic for this site. You have to make an honest attempt, and then ask a **specific question** about your algorithm or technique. If you have no idea where to start, you need to look up a tutorial or talk to a tutor. Stack Overflow is the wrong place to ask for general advice – Ceres Apr 24 '21 at 13:28
  • thank you for help. I gonna do it myself then – Dweller Apr 24 '21 at 13:45
  • help me please 1 – Dweller Apr 24 '21 at 14:33
  • I'm really confused. Why did you edit the whole question and changed it to another instead of making a new question? – RiveN Apr 24 '21 at 14:49
  • because i need to wait for couple of days to ask new question. Thanks for help! – Dweller Apr 24 '21 at 15:38

1 Answers1

1

I think the best thing you can do is to make an event on_member_join, which will be called every time user joins. Then in this event, you can make a list instead of variables that will save user id, and their current currency. users_currency = ["user's id", "5$", "another user's id", "7$"] and so on. Next, I would recommend saving it to a text file.

Example code

global users_currency
users_currrency = []

@client.event
global users_currency
async def on_member_join(member): #on_member_join event
    user = str(member.id) #gets user's id and changes it to string
    users_currency.append(user) #adds user's id to your list
    users_currency.append("0") #sets the currency to 0

Now if someone will join their id will appear in list and change their currency to 0.

How can you use assigned values in list

If you keep the code close to example higher then on users_currrency[0], users_currrency[2], [...]. You will get users' ids and on users_currrency[1], users_currrency[3], etc. You will get their currency. Then you can use on_message event or @client.command to make command that will look for user's id in list and change next value - their currency.

Saving it to a text file

You have to save it in a text file (Writing a list to a file with Python) and then make a function that will run at the start of the bot and read everything from the file and assign it inside your list.
Example code:

with open("users_currency.txt") as f:
    rd=f.read()
    changed_to_a_list=rd.split()
    users_currency = changed_to_a_list
RiveN
  • 2,595
  • 11
  • 13
  • 26