Discordpy Tasks not working as expected. No returning

こ雲淡風輕ζ 提交于 2021-01-28 12:30:28

问题


@tasks.loop(seconds = 5.0)
async def remind420(self):
    print("YES")
    print(datetime.now().strftime("%H:%M"))
    if datetime.now().strftime("%H:%M") == "16:55" or datetime.now().strftime("%H:%M") == "04:20":
        await client.get_channel(499245707081940995).send("420!!")

@remind420.before_loop
async def remind420_before():
    await client.wait_until_ready()

remind420.start()

There is no output. Absolutely no output. I expect the time to returned or atleast a yes.


回答1:


Make sure to have all your imports and self in the before_loop function parameters. If it is inside a cog, make sure to include self before the client. Adding the hours, minutes and count + the self in the before bit seemed to fix the issue.

import discord
from discord.ext import commands, tasks
from datetime import date, datetime

class StackOverflow(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.remind420.start()

    @tasks.loop(seconds = 5.0, minutes=0, hours=0 count=None)
    async def remind420(self):
        print("YES")
        print(datetime.now().strftime("%H:%M"))
        if datetime.now().strftime("%H:%M") == "16:55" or datetime.now().strftime("%H:%M") == "04:20":
            await self.client.get_channel(632933507399942164).send("420!!")


    @remind420.before_loop
    async def remind420_before(self):
        await self.client.wait_until_ready()


来源:https://stackoverflow.com/questions/59801751/discordpy-tasks-not-working-as-expected-no-returning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!