AreTillery

Discord Bun Buns

May 15th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. """ Excerpt from redditfetch.py """
  2. import praw
  3. import random
  4. import os
  5.  
  6. r = praw.Reddit(client_id=os.getenv("REDDITTOKEN"), client_secret=os.getenv('REDDITSECRET'), user_agent='snekbot')
  7.  
  8. def random_post(subreddit, num=40):
  9.     sub = r.subreddit(subreddit)
  10.     posts = [post for post in sub.hot(limit=num) if not "reddit.com/" in post.url.lower() and "v.redd.it" not in post.url.lower() and "https://youtu.be" not in post.url.lower()]
  11.     post = random.choice(posts)
  12.     return post
  13.  
  14. def random_from_several(subreddits, num=40):
  15.     allposts = list()
  16.     for s in subreddits:
  17.         sub = r.subreddit(s)
  18.         posts = [post for post in sub.hot(limit=num) if not "reddit.com/" in post.url.lower() and "v.redd.it" not in post.url.lower() and "https://youtu.be" not in post.url.lower()]
  19.         allposts = allposts + posts
  20.     post = random.choice(allposts)
  21.     return post
  22.  
  23.  
  24. """ Excerpt from bot.py """
  25. token = os.getenv("DISCORDBOT_TOKEN")
  26. client = commands.Bot(command_prefix="!")
  27.  
  28. @client.command(pass_context=True)
  29. @commands.check(is_pets_channel)
  30. async def bunme(ctx, *args):
  31.     """ Fetch a random post from r/bunnies. Restricted to the furryfriends channel """
  32.     try:
  33.         post = redditfetch.random_post("bunnies")
  34.     except Exception as e:
  35.         await client.send_message(ctx.message.channel, "Exception occurred!")
  36.         return
  37.     await client.send_message(ctx.message.channel, "__Title__: {}\n{}".format(post.title, post.url))
  38.  
  39. client.run(token)
Add Comment
Please, Sign In to add comment