From 82fddef8a0a038689a17cce6bf97487c32d0fb62 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sat, 8 Dec 2018 17:39:58 -0800 Subject: [PATCH] Fixing the reddit functionality. used to barf if you linked a reddit that didnt exist, now it just chceks for actual posts --- app/dragon-bot.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/dragon-bot.py b/app/dragon-bot.py index 3ceb6a83..701862f5 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -105,15 +105,14 @@ async def on_message(message): # Read messages and look for mentions of subreddits for word in message.content.split(): - if message.author != client.user: - if '/r/' in word or 'r/' in word: - try: - subreddit = word.split('/')[-1] - # Try and request the page to check that it actually exists - get_from_reddit.get_image(boards=subreddit, nsfw=True) + if (message.author != client.user) and '/r/' in word: + try: + subreddit = word.split('/')[-1] + # check if that subreddit exists by querying the length of this json blob + if len(requests.get("https://reddit.com/r/{}.json".format(subreddit), headers={'User-agent':'dragon-bot'}).json()['data']['children']): await client.send_message(message.channel, "https://reddit.com/r/{}".format(subreddit)) - except: - pass + except: + pass if message.content.startswith('!8ball'): await client.send_message(