-
-
Notifications
You must be signed in to change notification settings - Fork 16
Team 13 #10
base: master
Are you sure you want to change the base?
Team 13 #10
Changes from 6 commits
0aa95cb
c2505ce
8c25449
e3f0c70
611501f
85bb282
243b51f
b4e6468
cea4cf3
3a37252
6602d02
62401e5
7f63f4f
0137051
1e710db
5844b02
3656c01
48f8de4
bf2f9ea
d8a26ba
9c8a9f4
697f557
0951f92
544bed5
a2255e7
9b2f8d0
f30abab
48f2584
e5901b6
5770c79
259c5a6
2d45b2f
6776264
03f9b61
a27582a
e2d556b
003ace9
2366c43
ea31798
a9f8863
01c701b
9d28fa3
c98bc6e
d2482b2
f1b7a2c
ff361b8
13937c1
3279319
e0d2bf2
361cc45
ac33379
375f4bc
71a0f44
99f00f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| # coding=utf-8 | ||
| import json | ||
| import logging | ||
| import os | ||
| import random | ||
| from typing import Any, Dict | ||
|
|
||
| import aiohttp | ||
|
|
||
| import discord | ||
| from discord.ext.commands import AutoShardedBot, Context, command | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
@@ -29,6 +35,29 @@ async def get_snek(self, name: str = None) -> Dict[str, Any]: | |
| :return: A dict containing information on a snake | ||
| """ | ||
|
|
||
| async def get_snek_qwant_json(self, snake_name): | ||
| """ | ||
| Gets the json from Unsplash for a given snake query | ||
| :param snake_name: name of the snake | ||
| :return: the full JSON from the search API | ||
| """ | ||
| url = "https://api.unsplash.com/search/photos?client_id" \ | ||
| "={0}&query={1}+snake".format(os.environ.get("UNSPLASH_CLIENT_ID"), snake_name) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this string is a bit messy. prefer implicit string joins inside paranthesis and f-strings to \ line continuation and formats. client_id = os.environ.get("UNSPLASH_CLIENT_ID")
url = (
"https://api.unsplash.com/search/photos?client_id"
f"={client_id}&query={snake_name}+snake"
) |
||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get(url) as response: | ||
| response = await response.read() | ||
| return json.loads(response.decode("utf-8")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't you just use |
||
|
|
||
| async def get_snek_image(self, name): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| """ | ||
| Gets the URL of a snake image | ||
| :param name: name of snake | ||
| :return: image url | ||
| """ | ||
| json_response = await self.get_snek_qwant_json(name) | ||
| rand = random.randint(0, 9) # prevents returning the same image every time | ||
| return json_response['results'][rand]['urls']['small'] | ||
|
|
||
| @command() | ||
| async def get(self, ctx: Context, name: str = None): | ||
| """ | ||
|
|
@@ -40,6 +69,28 @@ async def get(self, ctx: Context, name: str = None): | |
| :param ctx: Context object passed from discord.py | ||
| :param name: Optional, the name of the snake to get information for - omit for a random snake | ||
| """ | ||
| embed = discord.Embed(color=0x3E885B) | ||
| if name == "python": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be case insensitive.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks - so when I change line 73 to ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if name and name.lower() == "python":would work for me.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep - looks good, thanks |
||
| # handle Python special case | ||
| embed.add_field( | ||
| name="Python (programming language)", | ||
| value="*Guido van Rossum*\n\n" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. multiline implicit string joins should be wrapped inside paranthesis, so that it's clear that they're one thing. This is a bit too implicit.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain this? I'm not super clear as to what you mean... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value=(
"*Guido van Rossum*\n\n"
"This language is neither dangerous nor venomous and be found in software globally"
),this is one way. or if you prefer it, this is acceptable too value=("*Guido van Rossum*\n\n"
"This language is neither dangerous nor venomous and be found in software globally"), |
||
| "This language is neither dangerous nor venomous and be found in software globally", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless you're a pirate, this should be "and can be found". |
||
| inline=False | ||
| ) | ||
| embed.set_image(url=await self.get_snek_image("python programming language")) | ||
| else: | ||
| url = await self.get_snek_image(name) # not limited to snakes - user can search anything they like | ||
| embed.add_field( | ||
| name="common name", | ||
| value="*sci name*\n\nThis snake is (**not**) venomous and can be found in (...)", | ||
| inline=False | ||
| ) | ||
| embed.set_image(url=url) | ||
| await ctx.channel.send( | ||
| content=ctx.message.author.mention + " Here's your snek!", | ||
| embed=embed | ||
| ) | ||
|
|
||
| # Any additional commands can be placed here. Be creative, but keep it to a reasonable amount! | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snake_nameshould be type hinted, as should the return type.