0

The bot manages tables: users and bans. Users and bans have a single user entry (test). The bot writes the new commenter's id to users and checks the commenter's id in the bans table. If id is in bans, the bot should remove any comment. Otherwise, it checks for forbidden words in the comment. The first function works correctly, an entry is added to users, but then control passes to the third function and it also works correctly. However, the second function does not work, user comments from bans are ignored. When run in PyCharm, the code runs without errors. What exactly needs to be corrected in the code, what is the error?

@dp.message_handler()
# add id in users
async def check_database(message: types.Message):
    if not db.user_exists(message.from_user.id):
        db.add_user(message.from_user.id, message.from_user.first_name)
        await check_bans(message)
        await mess_handler(message)

async def check_bans(message: types.Message):
# Check user id in bans, delete his comment
    if db.user_bans(message.from_user.id):
        await message.delete()
        await mess_handler(message)

async def mess_handler(message: types.Message):
# Delete comments if there are forbidden words
    text = message.text.lower()
    if str(message.from_user.id) != cfg.ADMIN_ID:
        for word in cfg.WORDS:
            if word in text:
                await message.delete()

db.py file

class Database:
    def __init__(self, db_file):
        self.connection = sqlite3.connect(db_file)
        self.cursor = self.connection.cursor()

    def user_exists(self, user_id):
        with self.connection:
            result = self.cursor.execute("SELECT * FROM 'users' WHERE 'user_id' = ?", (user_id,)).fetchall()
            return bool(len(result))

    def user_bans(self, user_id):
        with self.connection:
            result1 = self.cursor.execute("SELECT * FROM 'bans' WHERE 'user_id' = ?", (user_id,)).fetchall()
            return bool(len(result1))
Andrey
  • 1

1 Answers1

0

Why in checking bans to check the message if it is deleted? It turns out that you call the comment check twice:

async def check_bans(message: types.Message):
# Check user id in bans, delete his comment
    if db.user_bans(message.from_user.id):
        await message.delete()
    ////await mess_handler(message)////
Shawn
  • 1,232
  • 1
  • 14
  • 44
KirtVik
  • 1
  • 1