Search code examples
pythonherokudiscorddiscord.py

path can not be found for discord bot heroku


I've deployed a discord.py rewrite bot to Heroku and I've followed all the deployment steps and it worked. I tried to tweak my code into having changeable prefixes but I needed to use a JSON folder for it to work so I did and added it to PATH and I added it to my GitHub rep and everything seemed to work, It uploaded and deployed but the bot won't work now cause it cant find the prefix. The error apparently is that it can not find the path to the JSON file.

My path get code:

async def get_prefix(client, message):
    with open('storage\\pp.json', 'r') as f:
        prefixes = json.load(f)

    try:
        prs = prefixes[str(message.guild.id)]
    except KeyError:
        prs = "p."
    return prs

The Heroku error by using "Heroku logs --tail":

2021-08-24T17:31:59.796711+00:00 app[worker.1]: Ignoring exception in on_message
2021-08-24T17:31:59.796863+00:00 app[worker.1]: Traceback (most recent call last):
2021-08-24T17:31:59.796909+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
2021-08-24T17:31:59.796910+00:00 app[worker.1]: await coro(*args, **kwargs)
2021-08-24T17:31:59.796918+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 979, in on_message
2021-08-24T17:31:59.796919+00:00 app[worker.1]: await self.process_commands(message)
2021-08-24T17:31:59.796928+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 975, in process_commands
2021-08-24T17:31:59.796928+00:00 app[worker.1]: ctx = await self.get_context(message)
2021-08-24T17:31:59.796937+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 886, in get_context
2021-08-24T17:31:59.796937+00:00 app[worker.1]: prefix = await self.get_prefix(message)
2021-08-24T17:31:59.796947+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 831, in get_prefix
2021-08-24T17:31:59.796948+00:00 app[worker.1]: ret = await discord.utils.maybe_coroutine(prefix, self, message)
2021-08-24T17:31:59.796950+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/utils.py", line 343, in maybe_coroutine
2021-08-24T17:31:59.796951+00:00 app[worker.1]: return await value
2021-08-24T17:31:59.796961+00:00 app[worker.1]: File "/app/bot.py", line 97, in get_prefix
2021-08-24T17:31:59.796962+00:00 app[worker.1]: with open('storage\\pp.json', 'r') as f:
2021-08-24T17:31:59.796979+00:00 app[worker.1]: FileNotFoundError: [Errno 2] No such file or directory: 'storage\\pp.json'

The path is correct and it works on my pc. It just won't work on Heroku? Is it something wrong with Linux?


Solution

  • So apparently when you're pushing a bot to Heroku it's putting that bot on a Linux server aka a Linux machine that does not comprehend the same path as windows:

    So it should be:

    async def get_prefix(client, message):
        with open('storage\\pp.json', 'r') as f: #  It should be 'storage//pp.json'
            prefixes = json.load(f)
    
        try:
            prs = prefixes[str(message.guild.id)]
        except KeyError:
            prs = "p."
        return prs
    

    So basically your path should be forward slash 2 slashes