【问题标题】:Why has is_closed stopped working in the rewrite version?为什么 is_closed 在重写版本中停止工作?
【发布时间】:2019-05-23 15:52:54
【问题描述】:

我是 Discord python API 的新手。只要客户端仍然打开,我就会尝试永远运行一个方法。我通过 Client.loop.create_task() 安排了该方法,并且在没有 client.is_closed 时正在运行。我添加了一个调试打印语句,但是打印语句被调用了0次。

我基于这篇文章的代码:Discord.py Schedule

import discord
from datetime import datetime
from datetime import timedelta

token = "TOKEN"

s_users = {}
timezon_offset = 5
inac_days = 0
inac_hours = 0
inac_minutes = 1
active_role = "Active"
inac_role = "Inactive"  

client = discord.Client()

async def monitor():

    for guild in client.guilds:
        for user in guild.members:
            s_users[user] = datetime(1,1,1)

@client.event
async def on_ready():

        print('logged on as {0}'.format(client.user))

@client.event
async def on_message(message):

        print('message logged from {0.author}: {0.content} at {0.created_at}'.format(message))
        s_users[message.author] = message.created_at - timedelta(hours=timezon_offset)

async def task():
    await client.wait_until_ready()
    active = None
    inactive = None
    for guild in client.guilds:
            for role in guild.roles:
                if (role.name == active_role):
                    active = role
                elif (role.name == inac_role):
                    inactive = role
    while not client.is_closed:
            print("here")
            for user in s_users:

                if datetime.now() - s_users[user] > timedelta(days=inac_days, hours=inac_hours, minutes=inac_minutes):

                    if not(inac_role in [role.name for role in user.roles]):

                        await user.remove_roles(active)
                        await user.add_roles(inactive)

                        print("gave user: " + user.name + " " + inac_role + " role")

                if datetime.now() - s_users[user] <= timedelta(days=inac_days, hours=inac_hours, minutes=inac_minutes):

                    if not(active_role in [role.name for role in user.roles]):

                        await user.remove_roles(inactive)
                        await user.add_roles(active)

                        print("gave user: " + user.name + " " + active_role + " role")


client.loop.create_task(task())
client.run(token)

它应该执行task(),只要客户端没有关闭它就应该运行。但是 print 语句执行了 0 次。

【问题讨论】:

  • 您是否正在创建一个任务循环,其中包含另一个循环(将一直运行到客户端关闭)?
  • 这篇文章:stackoverflow.com/questions/53662781/discord-py-schedule 做了同样的事情,这也有效,据我所知,Client.loop.create_task() 只会运行一次方法

标签: python-3.x discord.py discord.py-rewrite


【解决方案1】:

在 1.0.0 版中,Client.is_closed 从属性更改为方法。请参阅迁移指南中的 Property Changes

您需要添加括号来调用该方法:

while not client.is_closed():
    ...

【讨论】:

  • 谢谢,完全错过了!
猜你喜欢
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 2020-12-05
相关资源
最近更新 更多