diff options
author | 2021-03-05 01:19:30 +0300 | |
---|---|---|
committer | 2021-03-05 01:19:30 +0300 | |
commit | 0bc3cc53563816d6d9bd02e7b939ad767c7984a3 (patch) | |
tree | c5c0b0482ded0648a4e9d99280df53e54b926165 | |
parent | Merge pull request #607 from Shivansh-007/fix/startup-channels (diff) | |
parent | Update earth_photos.py (diff) |
Merge pull request #609 from Kronifer/earth_photos
Earth photos
-rw-r--r-- | bot/constants.py | 2 | ||||
-rw-r--r-- | bot/exts/easter/earth_photos.py | 61 |
2 files changed, 63 insertions, 0 deletions
diff --git a/bot/constants.py b/bot/constants.py index db34b55a..721defc8 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -158,6 +158,7 @@ class Colours: soft_orange = 0xf9cb54 soft_red = 0xcd6d6d yellow = 0xf9f586 + grass_green = 0x66ff00 class Emojis: @@ -271,6 +272,7 @@ class Tokens(NamedTuple): igdb_client_id = environ.get("IGDB_CLIENT_ID") igdb_client_secret = environ.get("IGDB_CLIENT_SECRET") github = environ.get("GITHUB_TOKEN") + unsplash_access_key = environ.get("UNSPLASH_KEY") class Wolfram(NamedTuple): diff --git a/bot/exts/easter/earth_photos.py b/bot/exts/easter/earth_photos.py new file mode 100644 index 00000000..60e34b15 --- /dev/null +++ b/bot/exts/easter/earth_photos.py @@ -0,0 +1,61 @@ +import logging + +import discord +from discord.ext import commands + +from bot.constants import Colours +from bot.constants import Tokens + +log = logging.getLogger(__name__) + + +class EarthPhotos(commands.Cog): + """This cog contains the command for earth photos.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.command(aliases=["earth"]) + async def earth_photos(self, ctx: commands.Context) -> None: + """Returns a random photo of earth, sourced from Unsplash.""" + async with ctx.typing(): + async with self.bot.http_session.get( + 'https://api.unsplash.com/photos/random', + params={"query": "planet_earth", "client_id": Tokens.unsplash_access_key} + ) as r: + jsondata = await r.json() + linksdata = jsondata.get("urls") + embedlink = linksdata.get("regular") + downloadlinksdata = jsondata.get("links") + userdata = jsondata.get("user") + username = userdata.get("name") + userlinks = userdata.get("links") + profile = userlinks.get("html") + # Referral flags + rf = "?utm_source=Sir%20Lancebot&utm_medium=referral" + async with self.bot.http_session.get( + downloadlinksdata.get("download_location"), + params={"client_id": Tokens.unsplash_access_key} + ) as _: + pass + + embed = discord.Embed( + title="Earth Photo", + description="A photo of Earth 🌎 from Unsplash.", + color=Colours.grass_green + ) + embed.set_image(url=embedlink) + embed.add_field( + name="Author", + value=f"Photo by [{username}]({profile}{rf}) \ + on [Unsplash](https://unsplash.com{rf})." + ) + await ctx.send(embed=embed) + + +def setup(bot: commands.Bot) -> None: + """Load the Earth Photos cog.""" + if not Tokens.unsplash_access_key: + log.warning("No Unsplash access key found. Cog not loading.") + return + bot.add_cog(EarthPhotos(bot)) |