Jasme
Jasme

Reputation: 87

Issue wit checking for time on check

Hi I'm currently having an issue getting the time allocated for the user which is saved on a postgres database. What I'm trying to achieve is when the users duration expires a role is removed I'm wanting to get the time from the database when the check runs but this doesn't seem to be working,

My console is not outputting an error but the check doesn't seem to be running.

Here is what I'm working with:

@tasks.loop(seconds=5.0)
async def check_mute(self):
    guild = self.bot.get_guild(750744359632121661)
    restricted = discord.utils.get(member.guild.roles, name="Restricted")
    members = discord.utils.get(member.guild.roles, name="Members")
    for member in list(guild.members):
        conn = psycopg2.connect(DATABASE_URL, sslmode='require')
        cursor = conn.cursor()
        cursor.execute("SELECT time FROM blacklist WHERE username=%s", (member.id, ))
        restricted_role = get(ctx.guild.roles, name="Restricted")
        muted_time = cursor.fetchone()
        current_time = dt.datetime.now()
        mute_elapsed_time = current_time - muted_time 
        
        if member.id:
            await member.add_roles(members)
            await member.remove_roles(restricted, reason=f'Restricted role removed by {author.name}')

Upvotes: 0

Views: 31

Answers (1)

stijndcl
stijndcl

Reputation: 5649

You're not getting errors because tasks don't throw any errors by default. In order to get some info out of them, you need to write a custom error handler for them.

With that said, I do see a few issues that might cause your loop to break.

First of all, the variables ctx and author are never defined anywhere in your code fragment, but you're using them. This will throw errors & interrupt the loop.

Are you starting your loop using check_mute.start()? Tasks need to be started before they run, and your code fragment doesn't have this.

the check doesn't seem to be running

I don't see you checking the time difference anywhere. You said the check didn't happen, but it just isn't there in the first place. You're defining mute_elapsed_time, but never using it to check if enough time has elapsed or not.

After fixing those, could you put some debug prints in your task to see where it gets & when it stops? If something else is wrong, that might help identify it.

PS. This is unrelated, but you're get'ing restricted_role in the loop for every member, while you can just do that once above the loop (and you already did do it above the loop, so it's really unnecessary). You're not even using it as far as I can see so consider removing it ^^. That's also the line where the ctx is (which doesn't exist) so removing it all together might be a good idea.

Upvotes: 1

Related Questions