import discord
from discord.ext import commands, tasks
from datetime import datetime, timedelta
import aiosqlite
from datetime import datetime, timedelta, timezone


intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.guilds = True
intents.messages = True
intents.voice_states = True

bot = commands.Bot(command_prefix="!", intents=intents)
DB_PATH = "activity.db"

# --- DATABASE SETUP ---

async def setup_database():
    async with aiosqlite.connect(DB_PATH) as db:
        await db.execute("""
            CREATE TABLE IF NOT EXISTS activity (
                user_id INTEGER PRIMARY KEY,
                last_active TEXT
            )
        """)
        await db.commit()

async def update_activity(user_id):
    async with aiosqlite.connect(DB_PATH) as db:
        await db.execute("""
            INSERT INTO activity (user_id, last_active)
            VALUES (?, ?)
            ON CONFLICT(user_id) DO UPDATE SET last_active=excluded.last_active
        """, (user_id, datetime.utcnow().isoformat()))
        await db.commit()

async def get_last_active(user_id):
    async with aiosqlite.connect(DB_PATH) as db:
        async with db.execute("SELECT last_active FROM activity WHERE user_id = ?", (user_id,)) as cursor:
            row = await cursor.fetchone()
            if row:
                return datetime.fromisoformat(row[0])
            return None

# --- BOT EVENTS ---

@bot.event
async def on_ready():
    print(f'✅ Logged in as {bot.user}')
    await setup_database()
    check_inactive_members.start()

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    await update_activity(message.author.id)
    await bot.process_commands(message)

@bot.event
async def on_voice_state_update(member, before, after):
    if before.channel != after.channel:
        await update_activity(member.id)

# --- INACTIVITY CHECK LOOP ---

@tasks.loop(minutes=1)
async def check_inactive_members():
    for guild in bot.guilds:
        inactive_role = discord.utils.get(guild.roles, name="Inactive")
        if not inactive_role:
            inactive_role = await guild.create_role(name="Inactive", reason="For inactive users")

        now = datetime.now(timezone.utc)
        threshold = now - timedelta(hours=1)

        for member in guild.members:
            if member.bot:
                continue

            last_active = await get_last_active(member.id)

            # ✅ Fix: Only assign inactive if we have a known activity record
            if last_active is not None and last_active < threshold:
                if inactive_role not in member.roles:
                    try:
                        await member.add_roles(inactive_role, reason="Inactive for 30+ days")
                        print(f"🟡 Marked {member} as Inactive")
                    except discord.Forbidden:
                        print(f"⚠️ No permission to assign role to {member}")
            elif last_active is not None and last_active >= threshold:
                if inactive_role in member.roles:
                    try:
                        await member.remove_roles(inactive_role, reason="User is active again")
                        print(f"🟢 Removed 'Inactive' from {member}")
                    except discord.Forbidden:
                        print(f"⚠️ No permission to remove role from {member}")

# --- ADMIN COMMAND TO MARK ALL MEMBERS ACTIVE ---

@bot.command()
@commands.has_permissions(administrator=True)
async def mark_active(ctx):
    for member in ctx.guild.members:
        if not member.bot:
            await update_activity(member.id)
    await ctx.send("✅ All members have been marked as active.")

@bot.command()
async def ping(ctx):
    await ctx.send("Pong! Bot is working.")


# --- Start Bot ---
bot.run("TOKEN_HERE")


I have this code and its giving me this error

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\discord\ext\tasks\__init__.py", line 246, in _loop
    await self.coro(*args, **kwargs)
  File "D:\Discord bot Program\main.py", line 86, in check_inactive_members
    if last_active is not None and last_active < threshold:
                                   ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: can't compare offset-naive and offset-aware datetimes