简体   繁体   English

发出机智检查检查时间

[英]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.嗨,我目前在获取为保存在 postgres 数据库中的用户分配的时间时遇到问题。 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}')

You're not getting errors because tasks don't throw any errors by default.您不会收到错误,因为默认情况下tasks不会引发任何错误。 In order to get some info out of them, you need to write a custom error handler for them.为了从中获取一些信息,您需要为它们编写自定义error handler

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.首先,变量ctxauthor从未在您的代码片段中的任何地方定义,但您正在使用它们。 This will throw errors & interrupt the loop.引发错误并中断循环。

Are you starting your loop using check_mute.start() ?您是否使用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.您正在定义mute_elapsed_time ,但从未使用它来检查是否已经过去了足够的时间

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).这是不相关的,但你是get “荷兰国际集团restricted_role在回路的每一个成员,而你可以做,一旦上述循环(和你已经没有做到上述循环,所以真的没有必要)。 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.这也是ctx所在的行(不存在),因此将其全部删除可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM