Emir Sürmen
Emir Sürmen

Reputation: 950

'Context' object has no attribute 'reddit' Discord.PY

I'm trying to add a .reddit command to my bot. This is my code:

 @client.command(name="random", aliases=["reddit"])
 async def _random(ctx, subreddit: str = ""):
    reddit = None
    if reddit_app_id and reddit_app_secret:
        reddit = praw.Reddit(client_id=reddit_app_id,client_secret=reddit_app_secret,user_agent="MASTERBOT:%s1.0" % reddit_app_id)
    if reddit:
        submissions = reddit.subreddit(subreddit).hot()
        submission = next(x for x in submissions if not x.stickied)
        await ctx.send(submissions.url)

I have everything imported, everything seemed fine until I got this error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Command' object has no attribute 'randint'

As I understood, the program has no idea what a randint is. I checked if I've made a typo, but no. Everything seemed fine. I was getting an antoher error on the same command but I managed to fix it. But this one got me and I need your help.

These are the new errors:

AttributeError: 'coroutine' object has no attribute 'url'

.

RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Upvotes: 2

Views: 183

Answers (1)

Diggy.
Diggy.

Reputation: 6944

Do you have the command in a Cog (class)?

If you don't, then you should remove self, as it'll assume that that's the name of the context object.

@client.command(name="random", aliases=["reddit"])
async def _random( ctx, subreddit: str = ""):
    reddit = None
    if reddit_app_id and reddit_app_secret:
        reddit = praw.Reddit(client_id=reddit_app_id,client_secret=reddit_app_secret,user_agent="MASTERBOT:%s1.0" % reddit_app_id)
    if reddit:
        chosen_subreddit = reddit_enabled_meme_subreddits[0]
        if subreddit:
            if subreddit in reddit_enabled_meme_subreddits:
                chosen_subreddit = subreddit
        submissions = reddit.subreddit(chosen_subreddit).hot()
        post_to_pick = random.randint(1, 10)
        for i in range(0, post_to_pick):
            submission = next(x for x in submissions if not x.stickied)
        await ctx.send(submission.url)
    else:
        await ctx.send("This is not working")

The issue is with the name of the command, random, as this is polluting the namespace of the random module. You're able to bypass this by renaming the command.

The async def random(.... is clashing with import random at the top of your code. You're able to set the name of the command with the name= keyword argument in the decorator. That's the name that people will be typing into discord.


Tried using your method of getting a random submission (minus the superfluous code, just the same logic), and it worked for me:

reddit = praw.Reddit(client_id="...", client_secret="...", user_agent="...")

@bot.command(name="reddit")
async def _reddit(ctx, subreddit: str = ""):
    submissions = reddit.subreddit(subreddit).hot()
    submission = next(x for x in submissions if not x.stickied)
    await ctx.send(submission.url)

The only thing I can think of is making sure that you have the most up-to-date version of praw, and also if there's anything else in the command that you might be leaving out of the question, then that might be affecting it, although that's just speculation.

I'd say try making the command from the ground-up. Start off simple with something that works, and add to it line by line until something breaks. Then you'll know what's causing the RuntimeWarning and such.

Sorry for not having a clear-cut answer for this.

Upvotes: 2

Related Questions