为什么 is_closed 在重写版本中停止工作?
Why has is_closed stopped working in the rewrite version?
我是 Discord 的新手 python API。只要客户端仍然打开,我就会一直尝试 运行 一种方法。我通过 Client.loop.create_task() 安排了方法,并且 运行ning 而不是 client.is_closed。我添加了调试打印语句,但是打印语句被调用了0次
我的代码基于此 post: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(),它应该 运行 只要客户端没有关闭。但是打印语句执行了0次
在版本 1.0.0 中,Client.is_closed
was changed from a property to a method. See Property Changes 在迁移指南中。
您需要添加括号才能调用该方法:
while not client.is_closed():
...
我是 Discord 的新手 python API。只要客户端仍然打开,我就会一直尝试 运行 一种方法。我通过 Client.loop.create_task() 安排了方法,并且 运行ning 而不是 client.is_closed。我添加了调试打印语句,但是打印语句被调用了0次
我的代码基于此 post: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(),它应该 运行 只要客户端没有关闭。但是打印语句执行了0次
在版本 1.0.0 中,Client.is_closed
was changed from a property to a method. See Property Changes 在迁移指南中。
您需要添加括号才能调用该方法:
while not client.is_closed():
...