Fixing the reddit functionality. used to barf if you linked a reddit that didnt exist, now it just chceks for actual posts

This commit is contained in:
Luke Robles 2018-12-08 17:39:58 -08:00
parent a242ae99ed
commit 82fddef8a0

View File

@ -105,12 +105,11 @@ 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:
if (message.author != client.user) and '/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)
# 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