-
-
Notifications
You must be signed in to change notification settings - Fork 16
Team 20 #16
base: master
Are you sure you want to change the base?
Team 20 #16
Changes from 10 commits
ece1140
54fbeb0
105b415
fd9db0d
63ccb69
3763038
47e03d6
9b4a683
73ab47b
2cbbf34
0ddc2a4
e766154
8d89ccf
002dcfa
163d4d7
916a7ac
9338eeb
a626e56
aa43444
532995e
992de22
305c3fe
d419d1c
108e84d
971d098
2c6af66
80e01f6
8bb749d
fc5dddb
3330e52
1b5e682
2344edc
94bfad1
a93cc3b
9f77c25
626b8b6
d31e269
78ad193
e395e6b
93d1d8e
b9ee9c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,33 @@ | |
import logging | ||
from typing import Any, Dict | ||
|
||
from googleapiclient.discovery import build | ||
from discord.ext.commands import AutoShardedBot, Context, command | ||
from discord import Embed | ||
|
||
import aiohttp | ||
import json | ||
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. these imports aren't gonna pass linting. wrong import orders and groupings. you should fix them so they are PEP8 compliant. Why aren't you guys using flake8? |
||
import async_timeout | ||
import random | ||
import difflib | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
# Probably should move these somewhere | ||
BASEURL = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts|pageimages&exintro=&explaintext=&titles={}&redirects=1" | ||
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 line is 140 characters. it's not gonna pass linting and it's just not okay to do even if it did. |
||
PYTHON = { | ||
"name": "Python", | ||
"info": """Python is a species of programming language, \ | ||
commonly used by coding beginners and experts alike. It was first discovered \ | ||
in 1989 by Guido van Rossum in the Netherlands, and was released to the wild \ | ||
two years later. Its use of dynamic typing is one of many distinct features, \ | ||
alongside significant whitespace, heavy emphasis on readability, and, above all, \ | ||
absolutely pain-free software distribution... *sigh*""", | ||
"image": "https://www.python.org/static/community_logos/python-logo-master-v3-TM-flattened.png" | ||
} | ||
with open("bot/snakes.txt") as file: | ||
SNAKES = list(map(lambda ln: ln.strip('\n'), file.readlines())) | ||
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. I don't even know where to begin with this line.
|
||
|
||
|
||
class Snakes: | ||
""" | ||
|
@@ -15,31 +38,67 @@ class Snakes: | |
def __init__(self, bot: AutoShardedBot): | ||
self.bot = bot | ||
|
||
@staticmethod | ||
def snake_url(name) -> str: | ||
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. type hint 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.
|
||
"""Get the URL of a snake""" | ||
|
||
def encode_url(text): | ||
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. The standard lib already provides a tool for this: |
||
"""Encode a string to URL-friendly format""" | ||
return BASEURL.format(text.replace(' ', '%20').replace("'", '%27')) | ||
|
||
# Check if the snake name is known | ||
if name.upper() in list(map(lambda n: n.upper(), SNAKES)): | ||
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. again, please use a list comp. |
||
return encode_url(name) | ||
|
||
# Get a list of similar names if a match wasn't found | ||
matches = difflib.get_close_matches(name, SNAKES, n=1) | ||
return encode_url(matches[0] if matches != [] else random.choice(SNAKES)) | ||
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. needless empty list comparison. |
||
|
||
@staticmethod | ||
async def fetch(session, url): | ||
"""Fetch the contents of a URL as a json""" | ||
async with async_timeout.timeout(10): | ||
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. Why not just |
||
async with session.get(url) as response: | ||
return await response.json() | ||
|
||
async def get_snek(self, name: str = None) -> Dict[str, Any]: | ||
""" | ||
Go online and fetch information about a snake | ||
"""Get a snake with a given name, or otherwise randomly""" | ||
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. Poorly written docstring. "otherwise randomly".
|
||
if name is None: | ||
name = random.choice([x.strip('\n') for x in SNAKES]) | ||
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. didn't you already strip out the newlines when you created |
||
|
||
elif name.upper() == "PYTHON": | ||
return PYTHON | ||
|
||
The information includes the name of the snake, a picture of the snake, and various other pieces of info. | ||
What information you get for the snake is up to you. Be creative! | ||
# Get snake information | ||
async with aiohttp.ClientSession() as session: | ||
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. You used a timeout above, but not here. Maybe you meant to have one here? |
||
url = self.snake_url(name) | ||
|
||
If "python" is given as the snake name, you should return information about the programming language, but with | ||
all the information you'd provide for a real snake. Try to have some fun with this! | ||
# Get the | ||
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. Get the what? |
||
response = await self.fetch(session, url) | ||
page = response["query"]["pages"] | ||
content = next(iter(page.values())) | ||
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. Surely there's a simpler way to do that. |
||
|
||
:param name: Optional, the name of the snake to get information for - omit for a random snake | ||
:return: A dict containing information on a snake | ||
""" | ||
# Parse the full-res image from the thumbnail | ||
thumb = content.get("thumbnail", {}).get("source", "http://i.imgur.com/HtIPyLy.png/beep") | ||
image = "/".join(thumb.replace("thumb/", "").split("/")[:-1]) | ||
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.
|
||
|
||
# WHY WOULD YOU USE DICTIONARY LITERAL IN A RETURN STATEMENT but okay lol | ||
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 seems fine from the way you've done this - not sure I have any qualms with it but maybe consider making a class for this return object... 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. returning a dictionary literal seems fine. this comment, however, does not. |
||
return { | ||
"name": content["title"], | ||
"info": content.get("extract", "") or "I don't know about that snake!", | ||
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. don't do this. the second param of "info": content.get("extract", "I don't know about that snake!"), |
||
"image": image | ||
} | ||
|
||
@command() | ||
async def get(self, ctx: Context, name: str = None): | ||
""" | ||
Go online and fetch information about a snake | ||
|
||
This should make use of your `get_snek` method, using it to get information about a snake. This information | ||
should be sent back to Discord in an embed. | ||
content = await self.get_snek(name) | ||
# Just a temporary thing to make sure it's working | ||
em = Embed() | ||
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. there's no two letter maxlimit on variable names. what's wrong with |
||
em.title = content["name"] | ||
em.description = content["info"][:1970] + "\n\nPS. If the image is a fucking map, blame wikipedia. -Somejuan" | ||
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. other teams seem to have found a way not to return maps. :P 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. Indeed they have, but we aren't other teams :D |
||
em.set_image(url=content["image"]) | ||
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. some of this can be more concise: embed=discord.Embed(
title=content["name"],
description=content["info"][:1970] + "\n\nPS. If the image is a fucking map, blame wikipedia. -Somejuan"
) |
||
|
||
: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 | ||
""" | ||
await ctx.send(embed=em) | ||
|
||
# Any additional commands can be placed here. Be creative, but keep it to a reasonable amount! | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,8 @@ | |
# Commands, etc | ||
bot.load_extension("bot.cogs.snakes") | ||
|
||
bot.run(os.environ.get("BOT_TOKEN")) | ||
#bot.run(os.environ.get("BOT_TOKEN")) | ||
# I SWEAR TO GOD, HONESTLY WATCH ME LEAVE THIS TOKEN IN LMAO | ||
bot.run('BOT TOKEN') | ||
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. How does this run properly? |
||
|
||
bot.http_session.close() # Close the aiohttp session when the bot finishes running |
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.
any particular reason this was done? discord.py doesn't like the latest aiohttp, we locked the version for a reason.