Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Excerpt from redditfetch.py """
- import praw
- import random
- import os
- r = praw.Reddit(client_id=os.getenv("REDDITTOKEN"), client_secret=os.getenv('REDDITSECRET'), user_agent='snekbot')
- def random_post(subreddit, num=40):
- sub = r.subreddit(subreddit)
- 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()]
- post = random.choice(posts)
- return post
- def random_from_several(subreddits, num=40):
- allposts = list()
- for s in subreddits:
- sub = r.subreddit(s)
- 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()]
- allposts = allposts + posts
- post = random.choice(allposts)
- return post
- """ Excerpt from bot.py """
- token = os.getenv("DISCORDBOT_TOKEN")
- client = commands.Bot(command_prefix="!")
- @client.command(pass_context=True)
- @commands.check(is_pets_channel)
- async def bunme(ctx, *args):
- """ Fetch a random post from r/bunnies. Restricted to the furryfriends channel """
- try:
- post = redditfetch.random_post("bunnies")
- except Exception as e:
- await client.send_message(ctx.message.channel, "Exception occurred!")
- return
- await client.send_message(ctx.message.channel, "__Title__: {}\n{}".format(post.title, post.url))
- client.run(token)
Add Comment
Please, Sign In to add comment