-3

I am buiding a discord bot with dicord.py and praw and I want to exclude all video and gallery posts since they don't embed properly in discord. I tried using a in statement and telling it to search for a new post again but it still does not work. (this is my first python project so be nice)

heres what i tried:

elif user_message.lower() == '!dog':
            sub = reddit.subreddit('rarepuppers')
            posts = [post for post in sub.hot(limit=250)]
            random_post_number = random.randint(0, 250)
            random_post = posts[random_post_number]
            strForm = "{}".format(random_post.url)
            if 'v.redd.it' or 'gallery' in random_post.url:
              random_post_number = random.randint(0, 250)
              random_post = posts[random_post_number]
              strForm = "{}".format(random_post.url)
              await message.channel.send(random_post.url)
              return
            await message.channel.send(random_post.url)
            return
NeoFox
  • 13
  • 4
  • Not familiar with the praw package or the context of a lot of the variables in relation to discords objects. But it looks like it's your if. you want to exclude gallery photos so you should do "not in" instead of "in". Also, the or compares two booleans, so you aren't checking if either string is in the url, but rather if 'gallery' is in the url. because the statement {if "any string"} is always true, your if is basically saying {if True or 'gallery' in url} – smcrowley Jul 07 '22 at 00:08

1 Answers1

0

Check for video

if random_post.is_video:
   # Do what you want if there is a video
   random_post = random.choice(posts)

Check for gallery

if 'gallery_data' in random_post.__dict__:
   # Do what you want if the post is a gallery
   random_post = random.choice(posts)

I recommend that you use random.choice instead of random.radint and then using the index.

You can also use the subreddit.random() instead of using the random module.

Sandy
  • 247
  • 3
  • 12