0

When I try to run my script that collacts memes from reddit, I get this error:

Traceback (most recent call last):
  File "C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
  File "C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Reddit' object is not callable

And my code that trigerrs .reddit command is this:

@client.command(description="This command is not working right now")
async def reddit(self,ctx,subreddit: str =""):
    self.reddit = praw.Reddit(client_id=reddit_app_id, 
client_secret=reddit_app_secret,user_agent="MASTERBOT:½s:1.0")
    if self.reddit():
        chosen_subreddit = reddit_enabled_meme_subreddits[0]
        if subreddit:
            if subreddit in reddit_enabled_meme_subreddits:
                chosen_subreddit = subreddit
            submissions = self.reddit.subreddits(chosen_subreddit).hot()
        post_to_pick = random.randint(1,10)
        for i in range(0, post_to_pick):
            submissions = next(x for x in submissions if not x.stickied)
        await ctx.send(submissions.url)
    else:
        await ctx.send("This is not working")

And these are my id,secret and subreddits:

reddit_enabled_meme_subreddits = ["memes","dankmemes"]
reddit_app_secret = "SECRET"
reddit_app_id = "ID"

I have everything imported, everything seems fine when I look into others but mine just doesn't work!

Emir Sürmen
  • 884
  • 3
  • 14
  • 33

2 Answers2

1

I think your issue is at the line:

if self.reddit():

You're trying to call what praw.Reddit() is returning.
Perhaps try removing the parentheses.

Diggy.
  • 6,744
  • 3
  • 19
  • 38
  • It seemed to work but now I'm getting the error that says: "local variable 'submissions' referenced before assignment". Even though I tried to change the location of "submissions", that didn't work either – Emir Sürmen Jun 01 '20 at 19:41
  • It sounds like the code isn't reaching inside all of the if statements. Would you be able to add print statements to see where it gets up to? – Diggy. Jun 01 '20 at 19:46
  • what do you mean by "printing statements"? do you mean "print(self.reddit)"? – Emir Sürmen Jun 01 '20 at 20:09
  • Inside each of the `if` statements, just print out anything, so you can see which if statements are true, and then from that you can narrow down where the issue is coming from, and if the variables contain what you think they do. – Diggy. Jun 01 '20 at 20:13
  • it only prints the one inside "self.reddit". the others don't show up – Emir Sürmen Jun 02 '20 at 07:05
  • and it prints the one under "post_to_pick" which is also in "self.reddit". – Emir Sürmen Jun 02 '20 at 07:13
0

I think you should not use praw inside an asynchronous function. I am also working on Reddit API Requests on my Discord Bot and I am using aiohttp as well as asyncpraw. Also I do not recommend using ctx since it has a known vulnerability.

Here is my snippet how I get a random post from the subreddit "gifs" and send the URL in Discord. I think this is what you are also looking for.

Discord.py: Reddit API Request takes a long time

I only have the problem that my response takes quite some time.

Popeye
  • 11
  • 2