Move the avatar stuff into its own module to keep things clean

This commit is contained in:
Luke Robles 2017-08-20 14:40:41 -05:00
parent c9ce94c73e
commit 560f8a503a
2 changed files with 21 additions and 19 deletions

18
app/avatar.py Normal file
View File

@ -0,0 +1,18 @@
import requests
import get_from_reddit
def change_avatar():
image = None
while not image:
image = get_from_reddit.get_image(boards='smuganimegirls')
extension = image.split('.')[-1]
local_smug = "/tmp/smug.{}".format(extension)
# save an image locally
if extension.lower() in ['png', 'jpg']:
with open(local_smug, 'wb') as f:
f.write(requests.get(image).content)
return open(local_smug, 'rb').read()
else:
image = None

View File

@ -8,6 +8,7 @@ then imported into the main bot
import os
import requests
import avatar
import decide
import define_word
import discord
@ -37,25 +38,8 @@ async def on_ready():
game=discord.Game(name='Type !help to see what I can do')
)
# Keep trying for a different smug anime girl until we get a jpg or png
image = None
while not image and not debug:
image = get_from_reddit.get_image(boards='smuganimegirls')
extension = image.split('.')[-1]
local_smug = "/tmp/smug.{}".format(extension)
if extension.lower() in ['png', 'jpg']:
with open(local_smug, 'wb') as f:
f.write(requests.get(image).content)
# Set the profile picture
try:
await client.edit_profile(avatar=open(local_smug, 'rb').read())
except discord.errors.HTTPException:
print('Failed setting profile image. retrying')
image = None
else:
image = None
if not debug:
await client.edit_profile(avatar=avatar.change_avatar())
print("\n********************************")
print("\nDRAGON BOT RUNNING IN {} MODE".format(dragon_environment.upper()))