bobby-b-bot / reddit

Reddit client for Bobby B Bot
MIT License
69 stars 11 forks source link

Could not get environment variable #4

Closed andrewcreed closed 5 years ago

andrewcreed commented 5 years ago

I'm fairly new to python, so I'm not educated with all the jargon, but I think I've almost managed to get my Reddit bot working, however, there is this one issue that I cannot for the life of me understand how to fix. The error is as follows:

ERROR - Could not get environment variables: {} Traceback (most recent call last): File "...\reddit\reddit_bot.py", line 54, in <module> raise Exception # invalid environment variable

andrewcreed commented 5 years ago

Yep. This is still happening after tweaking some things. Although now that error is on line 56.

File "...\reddit\reddit_bot.py", line 56, in <module> logger.info("Got subreddit names: bot running on {} subreddits".format(subreddits.count('+') + 1)) # count '+' sign plus one. AttributeError: 'NoneType' object has no attribute 'count'

fzanettini commented 5 years ago
  1. The first error probably happens because on Line 46 there is a check on whether the bot is running on a TEST or PROD environment:
        if environment == 'TEST':
            # test sub is in environment variables
            subreddits = get_env('TST_SUBS', __file__)
        elif environment == 'PROD':
            # productive subs are on a JSON file
            json_raw = json.load(subs)
            subreddits = '+'.join(json_raw)
        else:
            raise Exception # invalid environment variable

Therefore, the environment variable ENV has only two possible values 'TEST' or 'PROD' , and I assume the exception below is raised because the value for ENV variable in the ".env" file is different from the strings 'TEST' or 'PROD':

ERROR - Could not get environment variables: {} Traceback (most recent call last): File "...\reddit\reddit_bot.py", line 54, in <module> raise Exception # invalid environment variable

  1. The second error seems to be because on Line 56 there is a logging entry that counts to how many subreddits the bot is listening:

logger.info("Got subreddit names: bot running on {} subreddits".format(subreddits.count('+') + 1)) # count '+' sign plus one.

To count, it uses method "count()" in string "subreddits", considering the error below, it seems the "subreddits" was not assigned, which makes me believe the "subs.json" file is empty:

File "...\reddit\reddit_bot.py", line 56, in <module> logger.info("Got subreddit names: bot running on {} subreddits".format(subreddits.count('+') + 1)) # count '+' sign plus one. AttributeError: 'NoneType' object has no attribute 'count'

Make sure the "subs.json" file is somewhat similar to the example here and that the environment variable TST_SUBS is configured similarly to below example:

TST_SUBS="all+funny+news"

PS: these steps should be somewhat contained in the README. Feel free to propose changes to the documentation so it is easier to set up the bot 😄

andrewcreed commented 5 years ago

Hi, there! Thanks for the speedy reply!

My .env file's string is as follows:

ENV = 'TEST'

My subs.json file is not empty and is formatted identically to the one in your example.

andrewcreed commented 5 years ago

Just for good measure, I copied and pasted the exact subs.json file that you linked, and the same error is still showing.

fzanettini commented 5 years ago

Thank you for the details!

In that case, since the ENV is set to 'TEST', the subs will be fetched from TST_SUBS environment variable from .env file, the subs.json file is only used when the ENV is set to 'PROD'.

Can you share the value for TST_SUBS environment variable in the .env file?

andrewcreed commented 5 years ago

Ah, that might be my problem. There isn't a TST_SUBS variable in my file. I'll add one now, what should the value for it be?

fzanettini commented 5 years ago

That might be it. The TST_SUBS environment variable should be a string as "sub1+sub2". I recommended to create a subreddit to test your bot or use "testingground4bots".

andrewcreed commented 5 years ago

Okay. That's stopped the errors, but now nothing is showing up at all in the command prompt when I run 'reddit_bot.py' - should it?

And my bot isn't responding to any of the trigger words in my test server.

fzanettini commented 5 years ago

To separate each problem for future references, can you open a new issue to check this other behavior?