blob: 6a88d4c8f0b43990680d87e01d5ea821de5c80ee (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import discord
from discord.ext import commands
from pydis_core.utils.logging import get_logger
from bot.bot import Bot
from bot.constants import Colours, Tokens
log = get_logger(__name__)
API_URL = "https://api.unsplash.com/photos/random"
class EarthPhotos(commands.Cog):
"""The earth photos cog."""
def __init__(self, bot: 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(
API_URL,
params={"query": "planet_earth", "client_id": Tokens.unsplash.get_secret_value()}
) 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.get_secret_value()}
) 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}) "
f"on [Unsplash](https://unsplash.com{rf})."
)
)
await ctx.send(embed=embed)
async def setup(bot: Bot) -> None:
"""Load the Earth Photos cog."""
if not Tokens.unsplash:
log.warning("No Unsplash access key found. Cog not loading.")
return
await bot.add_cog(EarthPhotos(bot))
|