Aditya Tomar
Aditya Tomar

Reputation: 1639

Why is my tasks.loop not working on Heroku?

I have a cog named wednesday that prints a certain image from an images folder, as the name suggests, every Wednesday. For testing purposes, however, I've made the loop run every 30 seconds, and I've made it so that it runs today (Monday) instead of Wednesday. I've tried running this code locally on my computer, and it works with no issues. However, after deploying to Heroku, the loop doesn't work at all. Keep in mind that I have another cog that has a tasks.loop as well, which changes the status of my bot every few seconds. That cog, however, works without any issues on Heroku. As I said before, this wednesday cog works locally on my machine, which means that I'm properly adding and loading the cog for my bot, and doing other things that are required for the code to run. So, why is my code not working on Heroku?

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

with open('config.json') as f:
    config = json.load(f)

class wednesday(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @tasks.loop(seconds=30)
    async def time_checker(self):
        self.time = datetime.datetime.now
        if self.time().hour == 19:
            if datetime.datetime.today().weekday() == 0: # 0 because I'm testing today which is a Monday
                self.channel = await self.bot.fetch_channel(config['id'])
                await self.channel.send(file=discord.File('images/wednesday_pic.png'))
    
    @commands.Cog.listener()
    async def on_ready(self):
        await self.bot.wait_until_ready()
        self.time_checker.start()

def setup(bot):
    bot.add_cog(wednesday(bot))

I'm not sure why this is happening: I think it might have something to do with the loop. I have other images in the images folder that work fine for other commands that I have, as the bot sends them when I call the command. Any help would be appreciated.

Upvotes: 0

Views: 382

Answers (2)

Aditya Tomar
Aditya Tomar

Reputation: 1639

I've figured out what was causing the error. The Heroku servers run on the UTC time zone, and I am on the PST/PDT time zone. So, I used a UTC to PDT converter to get the appropriate time that I was looking for the loop to print out the image. Here is the final code:

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

with open('config.json') as f:
    config = json.load(f)

class wednesday(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @tasks.loop(hours=1)
    async def time_checker(self):
        self.channel = await self.bot.fetch_channel(config['channel'])
        self.time = datetime.datetime.now
        if self.time().hour == 12:
            if datetime.datetime.today().weekday() == 3:
                await self.channel.send(file=discord.File('images/wednesday_pic.png'))
    
    @commands.Cog.listener()
    async def on_ready(self):
        await self.bot.wait_until_ready()
        self.time_checker.start()

def setup(bot):
    bot.add_cog(wednesday(bot))

Upvotes: 0

Tin Nguyen
Tin Nguyen

Reputation: 5330

We need more debugging details. You may find out what the mistake is when doing the debugging.

  1. Check requirements.txt and ensure the versions are locked. (Likely the case if you generated it through pip freeze. If not: Make sure the versions on your local development environment is the same as on Heroku.
  2. Add more logs. Logs when your cog is loaded. Logs when your task is repeatedly executed along with the values of your variables. Logs that print out your current working directory (import os; print(os.getcwd()))
  3. Check how far your bot reaches. Is it failing at the very beginning and it does not come online? Is the cog not being loaded? Is it the task routine? Is it the path resolution?
  4. Provide the logs. Don't cut off the logs. Provide the entire thing. They may not seem relevant to your but other readers may spot something.
  5. You provided there a relative path for your image. Provide information of your project folder structure. What's the command used to execute your project (Procfile, can also be seen in the logs.)

Upvotes: 1

Related Questions