This task worked perfectly fine when it was in the main file, but I wanted to organise it a little better. All my other cogs work perfectly fine. No errors show in log when I run the bot, but the task doesn’t show. The task should send a line of text from the text file to a specific channel every 24 hours.
I’m new to cogs, any help is appreciated. 🙂
import os import asyncio import random import discord from discord.ext import tasks from discord.ext import commands from discord.utils import get class DQuote(commands.Cog): def __init__(self, client): self.client = client self.dquote.start() @tasks.loop(hours = 24) async def dquote(self): channel = self.client.get_channel(id = 884425493309423646) # send here with open('../messages/quo.txt', 'r', encoding = 'utf-8'): # open txt txt = file.read() spl = list(map(str, txt.splitlines())) await channel.send('<@&880059988016504882> ' + random.choice(spl)) # get random quote @dquote.before_loop async def before_dquote(self): await self.client.wait_until_ready() def setup(client): client.add_cog(DQuote(client))
You’re not starting it.
dquote.start() is placed in the
before_loop method, but
before_loop is only called before it starts (as the name & documentation suggest). As a result it’s never actually called because the loop was never started, so
dquote.start() is never called either.
Put it in the
__init__ method of your cog instead, as the
__init__ is always gonna be called when the Cog is initialized.
def __init__(self, client): self.client = client self.dquote.start() # Put it here ... @dquote.before_loop async def before_dquote(self): await self.client.wait_until_ready() # And remove it down here
For more examples about
Tasks refer to the docs.