From 99cc4ed4d49f5b424a3a0401a185c77972c710f0 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sat, 10 Mar 2018 22:29:44 +0000 Subject: [PATCH] Creating a helper method for generating embed objects --- app/dragon-bot.py | 42 ++++++++++++++++++++++++++++++++++-------- app/wallpaper.py | 20 ++++++++++++++++---- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/app/dragon-bot.py b/app/dragon-bot.py index a6f3e526..eb4a98e3 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -75,12 +75,34 @@ async def on_message(message): ) ) + + def generate_embed(embed_url, embed_title=None, embed_description=None): + """ + generate_embed(embed_url, embed_title=None, embed_description=None) + + Generates a discord embed object based on the URL passed in + Optionally, you can set the title and description text for the embed object. + """ + image = embed_url + + if not embed_description: + embed_description="[Direct Link]({})".format(image) + + embed = discord.Embed( + title=embed_title, + description=embed_description, + type='rich' + ) + embed.set_image(url=image) + return embed + if message.content.startswith('!8ball'): await client.send_message( message.channel, eight_ball.check_8ball(message.content) ) + if message.content.startswith('!avatar'): profile = [message.author] if len(message.mentions): @@ -91,10 +113,10 @@ async def on_message(message): for user in profile: embed = discord.Embed( title="{}#{}".format(user.name, user.discriminator), - description="[Direct Link]({})".format(user.avatar_url), + description="[Direct Link]({})".format(user.avatar_url.replace('.webp', '.png')), type='rich' ) - embed.set_image(url=user.avatar_url) + embed.set_image(url=user.avatar_url.replace('.webp', '.png')) await client.send_message(message.channel, embed=embed) if message.content.startswith('!cleanup'): @@ -124,7 +146,7 @@ async def on_message(message): if message.content.startswith('!dog'): await client.send_message( message.channel, - doggos.get_dog() + embed=generate_embed(embed_url=doggos.get_dog()) ) if message.content.startswith('!excuse'): @@ -179,7 +201,10 @@ async def on_message(message): if message.content.startswith('!lewd'): await client.send_message( message.channel, - lewds.get_lewd(channel_name=message.channel.name) + embed=generate_embed( + embed_url=lewds.get_lewd(channel_name=message.channel.name), + embed_title="{} is being lewd".format(message.author.name) + ) ) if message.content.startswith('!weather'): @@ -197,13 +222,13 @@ async def on_message(message): if message.content.startswith('!pout'): await client.send_message( message.channel, - get_from_reddit.get_image('pouts') + embed=generate_embed(embed_url=get_from_reddit.get_image('pout')) ) if message.content.startswith('!smug'): await client.send_message( message.channel, - get_from_reddit.get_image('smuganimegirls') + embed=generate_embed(embed_url=get_from_reddit.get_image('smuganimegirls')) ) if message.content.startswith('!purge'): @@ -234,12 +259,13 @@ async def on_message(message): if message.content.startswith('!wallpaper'): await client.send_message( message.channel, - wallpaper.get_wall(message.content) + embed=generate_embed(embed_url=wallpaper.get_wall(message.content)) ) + if message.content.startswith('!homepage'): await client.send_message( message.channel, - wallpaper.get_picture() + embed=generate_embed(embed_url=wallpaper.fcking_homepage()) ) if message.content.startswith('!docker'): diff --git a/app/wallpaper.py b/app/wallpaper.py index 9663f807..bec0c9e9 100644 --- a/app/wallpaper.py +++ b/app/wallpaper.py @@ -1,10 +1,16 @@ -import requests -import requests from bs4 import BeautifulSoup -import urllib from urllib.parse import urlparse +import requests +import urllib def get_wall(message): + """ + get_wall(message) + + Grabs a random wallpaper from unsplash.com's random function + If any tags are passed in as the message, it will search for images + matching those tags + """ unsplash_url = "https://source.unsplash.com/3840x2160/?" if len(message.split()) > 2: @@ -30,9 +36,15 @@ def get_wall(message): else: return response -def get_picture(): +def fcking_homepage(): + """ + fcking_homepage() + + Retrives the picture of the day from fuckinghomepage.com + """ url = requests.get("http://fuckinghomepage.com") soup = BeautifulSoup(url.content) + for pic in soup.find_all("p"): if 'SWEET-ASS PICTURE' in ''.join(pic.findAll(text=True)): link = pic.find_next_sibling('p')