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 04d7686242
commit 235bb8b2e0

View File

@ -105,15 +105,14 @@ async def on_message(message):
# Read messages and look for mentions of subreddits # Read messages and look for mentions of subreddits
for word in message.content.split(): for word in message.content.split():
if message.author != client.user: if (message.author != client.user) and '/r/' in word:
if '/r/' in word or 'r/' in word: try:
try: subreddit = word.split('/')[-1]
subreddit = word.split('/')[-1] # check if that subreddit exists by querying the length of this json blob
# Try and request the page to check that it actually exists if len(requests.get("https://reddit.com/r/{}.json".format(subreddit), headers={'User-agent':'dragon-bot'}).json()['data']['children']):
get_from_reddit.get_image(boards=subreddit, nsfw=True)
await client.send_message(message.channel, "https://reddit.com/r/{}".format(subreddit)) await client.send_message(message.channel, "https://reddit.com/r/{}".format(subreddit))
except: except:
pass pass
if message.content.startswith('!8ball'): if message.content.startswith('!8ball'):
await client.send_message( await client.send_message(