randomize profile pic on launch and create a get from reddit function

This commit is contained in:
Luke Robles 2017-08-20 16:50:17 +00:00
parent d5dcf5d51c
commit 726c8424bb
3 changed files with 70 additions and 38 deletions

View File

@ -6,6 +6,7 @@ then imported into the main bot
"""
import os
import requests
import decide
import define_word
@ -13,6 +14,7 @@ import discord
import docker
import eight_ball
import excuse
import get_from_reddit
import help_methods
import lewds
import role_check
@ -30,14 +32,35 @@ dragon_environment = os.getenv('DRAGON_ENV')
debug = dragon_environment == 'test'
@client.event
async def on_ready():
print("\n********************************")
print("\nDRAGON BOT RUNNING IN {} MODE".format(dragon_environment.upper()))
print("\n********************************")
# Update the bot's status
await client.change_presence(
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
print("\n********************************")
print("\nDRAGON BOT RUNNING IN {} MODE".format(dragon_environment.upper()))
print("\n********************************")
if debug:
print("\nPress control+c to exit the bot")
print("Followed by control+d or by typing")
@ -217,5 +240,4 @@ async def on_message(message):
"There arent {} lines of output yet".format(num_lines)
)
client.run(tokens[dragon_environment])

29
app/get_from_reddit.py Normal file
View File

@ -0,0 +1,29 @@
import random
import requests
def get_image(boards):
"""
get_image(boards)
Returns a URL to an image on reddit from a random board in the boards list
as long as it is hosted on one of the domains in the domains list
"""
if isinstance(boards, list):
boards = random.choice(boards)
domains = [
'my.mixtape.moe',
'i.imgur.com',
'imgur.com',
'i.redd.it',
'i.reddituploads.com',
]
response = requests.get(
"https://reddit.com/r/{}.json?limit=500".format(boards),
headers = {'User-agent':'discord dragon-bot'}
).json()['data']['children']
image_urls = list(filter(lambda x: x['data']['domain'] in domains, response))
return random.choice(image_urls)['data']['url'].replace('http://', 'https://')

View File

@ -1,12 +1,13 @@
import random
import requests
import get_from_reddit
from pybooru import Danbooru
def get_from_danbooru():
def get_from_danbooru(boards):
"""
get_from_danbooru()
get_from_danbooru(boards)
returns a URL to an image on danbooru that matches a random tag
defined in the tag_list list
@ -65,37 +66,17 @@ def get_from_danbooru():
)['large_file_url'])
def get_from_reddit():
"""
get_from_reddit()
returns a URL to an image on reddit from a random board in the boards list
as long as it is hosted on one of the domains in the domains list
"""
boards = [
'2Booty',
'bakunyuu_hentai',
'ecchi',
'hentai',
'rule34',
'WesternHentai',
]
domains = [
'my.mixtape.moe',
'i.imgur.com',
'imgur.com',
'i.redd.it',
]
response = requests.get(
"https://reddit.com/r/{}.json?limit=500".format(random.choice(boards)),
headers = {'User-agent':'discord dragon-bot'}
).json()['data']['children']
image_urls = list(filter(lambda x: x['data']['domain'] in domains, response))
return random.choice(image_urls)['data']['url'].replace('http://', 'https://')
def get_lewd(channel_name):
if 'nsfw' in channel_name:
return random.choice([get_from_reddit, get_from_danbooru])()
boards = [
'2Booty',
'bakunyuu_hentai',
'ecchi',
'hentai',
'rule34',
'WesternHentai',
]
# This is really bad practice but pass boards to get_from_reddit AND danbooru so it doesnt error
return random.choice([get_from_reddit.get_image, get_from_danbooru])(boards=boards)
return 'You can only use this command in NSFW channels'