0

I am a beginner programmer and I am having an issue with my code. Here is the main code:

def user_status_check(chat_member):
    return chat_member["status"] != "left"

@dp.message_handler(commands=["mute"])
async def mute(message: types.Message) -> None:
    if str(message.from_user.id) == ADMIN:
        if not message.reply_to_message:
            await message.reply("This command must be a reply to a message!")
            return
        mute_sec = int(message.text[6:])
        db.add_mute(message.reply_to_message.from_user.id, mute_sec)
        await message.bot.delete_message(chat_id=chat_ID, message_id=message.message_id)
        await message.reply_to_message.reply(f"The user has been muted for {mute_sec} seconds!")

@dp.message_handler()
async def forbidden_message(message: types.Message) -> None:
    if not db.user_exists(user_id=message.from_user.id):
        db.add_user(user_id=message.from_user.id)
    if not db.mute_user(user_id=message.from_user.id):
        if user_status_check(await bot.get_chat_member(chat_id=chanel_ID, user_id=message.from_user.id)):
            global n
            text = message.text.lower()
            for word in WORDS:
                if word in text:
                    if n > 1:
                        await message.delete()
                        await message.answer(f"Profanity, flooding, and other expressions are prohibited!\n"
                                             f"Be more careful from now on! You have {n} attempts left.")
                        n -= 1
                        await asyncio.sleep(random.randint(5, 7))
                        await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id+1)
                    elif n == 1:
                        await message.delete()
                        await message.answer(f"Profanity, flooding, and other expressions are prohibited!\n"
                                             f"Be more careful from now on! You have {n} attempt left.")
                        n -= 1
                        await asyncio.sleep(random.randint(5, 7))
                        await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id+1)
                    elif n == 0:
                        await message.delete()
                        await message.answer(f"How unfortunate!\n"
                                            f"You have {n} attempts left. We have to say goodbye to you!")
                        await bot.kick_chat_member(chat_id=chat_ID, user_id=message.from_user.id)
                        await asyncio.sleep(random.randint(5, 7))
                        await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id+1)
                        n = 3
        else:
            await message.delete()
            await message.answer(f"Hello again!\n"
                                 f"To send messages again, please subscribe to the channel.", reply_markup=subscribe_menu)
            await asyncio.sleep(random.randint(3, 5))
            await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id+1)
    else:
        await message.delete()

@dp.chat_join_request_handler()
async def start_message(message: types.ChatJoinRequest, link: types.Message) -> None:
    await message.approve()
    await bot.send_message(chat_id=message.chat.id,
                           text=f"<b>Welcome! </b>\n"
                                f"<em>You are now part of our male community. "
                                f"Remember this moment well, it will turn your whole life upside down! </em>\n"
                                f"<em>Subscribe to our channel "
                                f"so that you can send messages and learn a lot of new things for yourself.</em>",
                           parse_mode="HTML")
    await asyncio.sleep(random.randint(3, 5))
    await bot.delete_message(chat_id=message.chat.id, message_id=link.message_id)

However, the different code of the connected database is returning the "'NoneType' object is not subscriptable" error when I try to run it:

class DataBase:

    def __init__(self, db_file):
        self.connection = sq.connect("datas.db")
        self.cursor = self.connection.cursor()

        self.cursor.execute("CREATE TABLE IF NOT EXISTS 'profile'(id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    "user_id INTEGER UNIQUE NOT NULL, "
                    "mute_time INTEGER DEFAULT (0) NOT NULL)")

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

    def add_user(self, user_id):
        with self.connection:
            return self.connection.execute("REPLACE INTO 'profile' ('user_id') VALUES (?)", (user_id,))

    def mute_user(self, user_id):
        with self.connection:
            user = self.cursor.execute("SELECT * FROM 'profile' WHERE 'user_id' = ?", (user_id,)).fetchone()
            return int(user[2]) >= int(time.time())

    def add_mute(self, user_id, mute_time):
        with self.connection:
            return self.connection.execute("UPDATE 'profile' SET 'mute_time' = ? WHERE 'user_id' = ?", (int(time.time()) + mute_time, user_id))

I got the code from another person on YouTube. In the video, the code is working, but my code is finding an error in the line:

return int(user[2]) >= int(time.time())

This line seems illogical to me because the SQL query selects data from another column. According to the blogger, it should compare information on the 'mute_time' column. For this reason, he chose index 2. Please help me with the best way to check the time so that it returns True or False.

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Check what is `user` at the moment. Looks like the query returned nothing and it is None. – Serg Jun 16 '23 at 10:25

0 Answers0