From 726c8424bbc5a298b88143e58db84012c9df41c9 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sun, 20 Aug 2017 16:50:17 +0000 Subject: [PATCH] randomize profile pic on launch and create a get from reddit function --- app/dragon-bot.py | 32 +++++++++++++++++++++++----- app/get_from_reddit.py | 29 ++++++++++++++++++++++++++ app/lewds.py | 47 +++++++++++++----------------------------- 3 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 app/get_from_reddit.py diff --git a/app/dragon-bot.py b/app/dragon-bot.py index efdcb834..f76871b1 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -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]) diff --git a/app/get_from_reddit.py b/app/get_from_reddit.py new file mode 100644 index 00000000..84a231ea --- /dev/null +++ b/app/get_from_reddit.py @@ -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://') \ No newline at end of file diff --git a/app/lewds.py b/app/lewds.py index 0aac3c2b..07bc057f 100644 --- a/app/lewds.py +++ b/app/lewds.py @@ -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'