0
votes

I am new to the Discord python API. I am trying to run a method forever as long as the client is still open. I scheduled the method through Client.loop.create_task(), and am running while not client.is_closed. I added a debugging print statement, but the print statement is called 0 times.

I based the code on this 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)

It should execute task(), which should run as long as the client is not closed. However, the print statement is executed 0 times.

1
Are you creating a loop of a task that has another loop (that will run until the client closes) on it?ivan
this post: stackoverflow.com/questions/53662781/discord-py-schedule did the same thing, which worked also, as far as I can tell, Client.loop.create_task() will only run a method onceluart

1 Answers

0
votes

In version 1.0.0, Client.is_closed was changed from a property to a method. See Property Changes in the Migration guide.

You'll need to add parentheses to call the method:

while not client.is_closed():
    ...