-
-
Notifications
You must be signed in to change notification settings - Fork 16
Team 17 #8
base: master
Are you sure you want to change the base?
Team 17 #8
Changes from 19 commits
b312786
31fe5d8
f92cf5e
d04759c
91cfb2d
6dd7aef
c865044
e830c95
80273f6
36ace30
9816692
2d743c8
b77b7fe
49f5251
f915d90
a552543
3234d57
3f56b6c
dfd3175
0ff0f42
ddc6e4e
9edf8c9
97e0e9d
adafa48
7033463
cca57c4
3f91775
b712a24
5850ddb
256d4e1
0854cb1
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,4 +1,4 @@ | ||
| # Byte-compiled / optimized / DLL files | ||
| Byte-compiled / optimized / DLL files | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
|
|
||
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| # coding=utf-8 | ||
| import logging | ||
|
|
||
| from aiohttp import ClientSession | ||
|
|
||
| from discord.ext.commands import AutoShardedBot | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
@@ -15,8 +17,26 @@ def __init__(self, bot: AutoShardedBot): | |
| self.bot = bot | ||
|
|
||
| async def on_ready(self): | ||
| log.info('Signed in as:') | ||
| log.info('--------------') | ||
| log.info(f'Username: {self.bot.user.name}') | ||
| log.info(f'User ID: {self.bot.user.id}') | ||
| log.info('--------------') | ||
| log.info('Serving Team 17 in Code Jam 1!') | ||
| log.info('--------------') | ||
| log.info("Bot connected!") | ||
|
|
||
| self.bot.session = ClientSession(loop=self.bot.loop) | ||
|
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. Don't do that, this should be done in the cog itself and set on |
||
| self.bot.info_url = 'https://snake-facts.weebly.com/' | ||
| log.info('Session created!') | ||
|
|
||
| with open('./snakes.txt', encoding='utf-8') as f: | ||
| self.bot.sneks = f.read().split('\n') | ||
| for i, snek in enumerate(self.bot.sneks): | ||
| self.bot.sneks[i] = snek.replace('\u200b', '').replace('\ufeff', '') | ||
|
|
||
| log.info('Snakes loaded.') | ||
|
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. This is the logging cog. If you need to do stuff
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. you can make two
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. You sure can!
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. what order will the bot perform the actions in?
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. No idea. You shouldn't rely on that. |
||
|
|
||
|
|
||
| def setup(bot): | ||
| bot.add_cog(Logging(bot)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,34 @@ | ||
| # coding=utf-8 | ||
| import logging | ||
| from difflib import get_close_matches | ||
| from random import choice | ||
| from typing import Any, Dict | ||
|
|
||
| from bs4 import BeautifulSoup | ||
|
|
||
| import discord | ||
| from discord.ext.commands import AutoShardedBot, Context, command | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| # sometimes it's the 2nd div after 2nd td, sometimes it's the 3rd div. | ||
| # either way, it's returning `None`, so then info url + None = info url | ||
| # which is what the thumbnail url is currently. | ||
| SNEK_MAP_SELECTOR = ( | ||
| "#wsite-content > div:nth-of-type(2) > div > div > table > " | ||
| "tbody > tr > td:nth-of-type(2) > div:nth-of-type(3) > div > a > img" | ||
| ) | ||
|
|
||
| SCIENTIFIC_NAME_SELECTOR = ( | ||
| "#wsite-content > div:nth-of-type(1) > div > div > " | ||
| "table > tbody > tr > td:nth-of-type(1) > div:nth-of-type(2)" | ||
| ) | ||
|
|
||
| DID_YOU_KNOW_SELECTOR = ( | ||
| '#wsite-content > div:nth-of-type(2) > div > div > table > ' | ||
| 'tbody > tr > td:nth-of-type(2) > div:nth-of-type(1)' | ||
| ) | ||
|
|
||
|
|
||
| class Snakes: | ||
| """ | ||
|
|
@@ -15,6 +38,38 @@ class Snakes: | |
| def __init__(self, bot: AutoShardedBot): | ||
| self.bot = bot | ||
|
|
||
| def no_sneks_found(self, name): | ||
| '''Helper function if the snake was not found in the directory.''' | ||
| em = discord.Embed( | ||
| title='No snake found.', | ||
| color=discord.Color.green() | ||
| ) | ||
|
|
||
| snakes = get_close_matches(name, self.bot.sneks) | ||
|
|
||
| if snakes: | ||
| em.description = 'Did you mean...\n' | ||
| em.description += '\n'.join(f'`{x}`' for x in snakes) | ||
| else: | ||
| snakes = 'https://github.com/SharpBit/code-jam-1/blob/master/snakes.txt' | ||
| em.description = f'No close matches found. Click [here]({snakes}) for the list of available snakes.' | ||
|
|
||
| return em | ||
|
|
||
| def format_info(self, data): | ||
| '''Formats the info with the given data.''' | ||
|
Member
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.
Member
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. Not required, but some documentation for the parameters would be nice, maybe some type annotations at the least to say what |
||
| em = discord.Embed( | ||
| title=f"{data['name']} ({data['scientific-name']})", | ||
| description=data['description'], | ||
| color=discord.Color.green(), | ||
| url=data['url'] | ||
| ) | ||
|
|
||
| em.set_image(url=data['image-url']) | ||
| # em.set_thumbnail(url=data['map-url']) | ||
|
|
||
| return em | ||
|
|
||
| async def get_snek(self, name: str = None) -> Dict[str, Any]: | ||
| """ | ||
| Go online and fetch information about a snake | ||
|
|
@@ -28,9 +83,56 @@ async def get_snek(self, name: str = None) -> Dict[str, Any]: | |
| :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 | ||
| """ | ||
| if name: | ||
| if name not in self.bot.sneks: | ||
|
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. This, again, should be an attribute on your cog, not the bot. |
||
| return self.no_sneks_found(name) | ||
| else: | ||
| name = choice(self.bot.sneks) | ||
|
|
||
| @command() | ||
| async def get(self, ctx: Context, name: str = None): | ||
| snake = name.lower().replace(' ', '-').replace("'", '') | ||
| url = f'{self.bot.info_url}{snake}.html' | ||
|
|
||
| async with self.bot.session.get(url) as resp: | ||
| info = await resp.read() | ||
| soup = BeautifulSoup(info, 'lxml') | ||
|
|
||
| img = soup.find(attrs={'property': {'og:image'}})['content'] | ||
| names = soup.find('td', class_='wsite-multicol-col') | ||
| sci_name = soup.select(SCIENTIFIC_NAME_SELECTOR)[0].text.strip() | ||
| # location_map = soup.select(SNEK_MAP_SELECTOR)[0]['src'] | ||
| description_tag = soup.find(attrs={'property': {'og:description'}}) | ||
|
|
||
| info = { | ||
| 'name': names.h1.string, | ||
| 'scientific-name': sci_name, | ||
| 'image-url': img, | ||
| # 'map-url': f'{self.bot.info_url}{location_map}', | ||
| 'description': description_tag['content'], | ||
| 'url': url | ||
| } | ||
|
|
||
| return info | ||
|
|
||
| async def get_snek_fact(self): | ||
| '''Helper function to get a snake fact.''' | ||
| page = choice(self.bot.sneks).replace(' ', '-').replace("'", '') | ||
| url = f'{self.bot.info_url}{page}.html' | ||
|
|
||
| async with self.bot.session.get(url) as resp: | ||
| response = await resp.read() | ||
| soup = BeautifulSoup(response, 'lxml') | ||
| fact = soup.select(DID_YOU_KNOW_SELECTOR)[0].text | ||
|
|
||
| em = discord.Embed( | ||
| title='Did you know?', | ||
| description=fact[13:], | ||
| color=discord.Color.green() | ||
| ) | ||
|
|
||
| return em | ||
|
|
||
| @command(aliases=['snakes.get', 'snakes.get()', 'get()']) | ||
| async def get(self, ctx: Context, *, name: str = None): | ||
| """ | ||
| Go online and fetch information about a snake | ||
|
|
||
|
|
@@ -40,8 +142,36 @@ 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 | ||
| """ | ||
| # Sends info about the programming language | ||
| if name.lower() == 'python': | ||
| # Python language info. | ||
| em = discord.Embed( | ||
| title='Python', | ||
| description='Python is an interpreted high-level programming language for general-purpose programming. ' | ||
| 'Created by Guido van Rossum and first released in 1991, ' | ||
| 'Python has a design philosophy that emphasizes code readability, ' | ||
| 'notably using significant whitespace.', | ||
| color=discord.Color.blurple() | ||
| ) | ||
|
|
||
| em.set_thumbnail(url='https://ih0.redbubble.net/image.80621508.8934/flat,800x800,075,t.u1.jpg') | ||
| em.set_image(url='https://www.python.org/static/community_logos/python-logo-master-v3-TM.png') | ||
| return await ctx.send(embed=em) | ||
| data = await self.get_snek(name) | ||
| # if the snake is not found | ||
| if isinstance(data, discord.Embed): | ||
| return await ctx.send(embed=data) | ||
| # format the given data | ||
| em = self.format_info(data) | ||
| await ctx.send(embed=em) | ||
|
|
||
| # Any additional commands can be placed here. Be creative, but keep it to a reasonable amount! | ||
| @command(aliases=['getsnekfact', 'snekfact()', 'get_snek_fact()']) | ||
| async def snekfact(self, ctx: Context): | ||
| ''' | ||
|
Member
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, |
||
| Gets a randomsnek fact from the "Did you know?" cards | ||
| that the website has on the right hand side. | ||
| ''' | ||
| await ctx.send(embed=await self.get_snek_fact()) | ||
|
|
||
|
|
||
| def setup(bot): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # coding=utf-8 | ||
| from discord import Embed | ||
|
|
||
| # Channels, servers and roles | ||
| PYTHON_GUILD = 267624335836053506 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| ">>> ", ">> ", "> ", | ||
| ">>>", ">>", ">" | ||
| ), # Order matters (and so do commas) | ||
| activity=Game(name="Help: bot.help()"), | ||
| activity=Game(name="with snekky sneks"), | ||
|
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. Probably a bit user-unfriendly, but it's fine in this case. :P
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. still mentionable /shrug |
||
| help_attrs={"aliases": ["help()"]}, | ||
| formatter=Formatter() | ||
| ) | ||
|
|
@@ -35,6 +35,6 @@ | |
| # Commands, etc | ||
| bot.load_extension("bot.cogs.snakes") | ||
|
|
||
| bot.run(os.environ.get("BOT_TOKEN")) | ||
| bot.run(os.environ.get('BOT_TOKEN')) | ||
|
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. Haha, come on, no need to change the quote marks.
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. i use single quotes, just that i accidentally did the config.json stuff before so when i changed it back i naturally used single quotes |
||
|
|
||
| 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.
No, this should be a comment.