Dr. Cyclops
Dr. Cyclops

Reputation: 27

Working on anti spam bot using discord.py this code runs but doesnt do what its supposed to

Working on an anti spam bot that records author ids to a .txt file ( clearing every 10 seconds) this code runs but I dont see any author names populating in my txt file.

Any help would be appreciated.

import asyncio
import os
import json
import time
import random
import discord
import datetime

from discord.ext import commands
from discord.ext.commands import bot

    ## On Logon ##
    @client.event
    async def on_ready():
        print("Ready")
        while True:
            print("cleared")
            await asyncio.sleep(10)
            with open("spam-bank.txt", "r+") as file:
                file.truncate(0)

    async def on_message(message):
    counter = 0
    with open("spam-bank.txt", "r+") as file:
        for lines in file:
            if lines.strip("\n") == str(message.author.id):
                counter+=1
        
        file.writelines(f"{str(message.author.id)}\n")
        if counter > 5:
            await message.guild.ban(message.author, reason="Caught by bot Spamming")
            await asyncio.sleep(1)
            await message.guild.unban(message.author)
            print("Flagged by bot for spamming chat..")

Upvotes: 1

Views: 475

Answers (1)

Dr. Cyclops
Dr. Cyclops

Reputation: 27

Turns out I was missing @client.event before the async def on_message

Upvotes: 1

Related Questions