Forums

Task only partially running?

I have a file in my root that I am running on a daily task with the simple command (/home/username/filename.py). The task runs with exit code 0 and I see both my prints from the file in the task logs. But the discord post does not happen. Everything works as expected though when I run the file locally and from the bash console on python anywhere. Does anyone know why this wouldn't work from the task? Do I maybe need to add some sort of wait/timeout to keep the task running long enough for the API call to execute? Feels hacky but I'm at a loss. Here is the code:


from discordwebhook import Discord
from datetime import datetime, date
import requests
import json

def tenor_search(search_term):
    api_key = "{my tenor api key}"
    lmt = 1
    ckey = "bandonCowntdown"
    r = requests.get(
        "https://tenor.googleapis.com/v2/search?q=%s&key=%s&client_key=%s&limit=%s" % (search_term, api_key, ckey,  lmt))
    if r.status_code == 200:
        rule_gifs = json.loads(r.content)
        return rule_gifs["results"][0]["media_formats"]["gif"]["url"]
    else:
        return None

def post_to_discord():
    try:
        discord = Discord(url="https://discord.com/api/webhooks/{my hook key})
        heaven = date(2025, 11, 2)
        delta = heaven - datetime.now().date()
        search_term = f"rule{delta.days}"
        gif_url = tenor_search(search_term)
        print("bingo", gif_url)
        discord.post(
            embeds=[
                {
                    "title":f"{delta.days} days.",
                    "image": {"url": f"{gif_url}"}
                }
            ],
            username="Dan_bot",
            avatar_url="https://imgur.com/1M0Za0l.png"
        )
        print("all done")
        return
    except Exception as e:
        print(f'{e}')
        discord.post(
            embeds=[
                {
                    "title":f"{delta.days} days.",
                    "description": f"Dan bot is broken. Please help Dan bot!"
                }
            ],
            username="Dan_bot",
            avatar_url="https://imgur.com/1M0Za0l.png"
        )
        return

post_to_discord()

That sounds a bit strange -- most Discord bots don't work on PythonAnywhere in free accounts, because the normal discord library isn't proxy-aware, and free accounts have to access the public Internet through a proxy. But perhaps the discordwebhook library that you're using doesn't have that issue -- after all, if it works from a console, it must be somehow getting through the proxy!

The best suggestion I can think of is to see what the discord.post method that you're using returns. Perhaps there are some error cases where instead of raising an exception, it returns some kind of error code or something similar.

This was an exceptionally helpful suggestion. It returned a 429 error, which I'd never seen before, but is a rate limit response code. The content had a link to discord's documentation for their rate limits, so it seems like it's all working, but discord is grumpy with me, I didn't think I'd hit it that much but I guess we'll see if it works tomorrow. Thank you for the help!