This repository was archived by the owner on Mar 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsnakes.py
More file actions
164 lines (131 loc) · 5.2 KB
/
Copy pathsnakes.py
File metadata and controls
164 lines (131 loc) · 5.2 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# coding=utf-8
import logging, aiohttp, random, wikipedia
from time import sleep
from bs4 import BeautifulSoup
from typing import Any, Dict
from discord.ext.commands import AutoShardedBot, Context, command
import asyncio
log = logging.getLogger(__name__)
class Snakes:
"""
Snake-related commands
"""
python_info = '''
Python (Programming Language)
\n
Python is powerful... and fast;\n
plays well with others;\n
runs everywhere;\n
is friendly & easy to learn;\n
is Open.
-------------------------------
Created by: Guido Van Rossum \n
Founded: 20th of February, 1991 \n
Official website: https://python.org
'''
def __init__(self, bot: AutoShardedBot):
self.inputs = []
self.bot = bot
async def get_snek(self, name: str = None) -> Dict[str, Any]:
"""
Go online and fetch information about a snake
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!
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!
: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
"""
name = str(name)
site = 'https://en.wikipedia.org/wiki/' + name
async with aiohttp.ClientSession() as session:
async with session.get(site) as resp:
text = await resp.text()
soup = BeautifulSoup(text, 'lxml')
if name.lower() == 'python':
name = self.python_info
return name
@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.
: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(BeautifulSoup(text, 'lxml').find("title"))
name = str(name)
site = 'https://en.wikipedia.org/wiki/' + name
async with aiohttp.ClientSession() as session:
async with session.get(site) as resp:
text = await resp.text()
soup = BeautifulSoup(text, 'lxml')
title = soup.find('h1').text
description = soup.find('table').text
em = discord.Embed(title=title, description=description)
if name.lower() == 'python':
await ctx.send(await self.get_snek(name))
else:
await ctx.send(embed=em)
# await ctx.send(name)
# Any additional commands can be placed here. Be creative, but keep it to a reasonable amount!
@command()
async def snake(self, ctx: Context, x=50, y=30):
board = """"""
running = True
snake = []
head = [x//2, y//2]
snake.append(head)
userID = ctx.author.id
facing = 0
board += "```\n " + "#" * x + "##"
for yAxis in range(y):
board += "\n #"
for xAxis in range(x):
if head == [xAxis, yAxis]:
board += "X"
else:
board += "0"
board += "#"
board += "\n " + "#" * x + "##```"
snakeBoard = await ctx.send(board)
while running:
for mess in self.inputs:
if mess.author.id == userID:
self.inputs = []
if mess.content == "a":
facing = (facing - 1) % 4
if mess.content == "d":
facing = (facing + 1) % 4
break
if facing == 0:
head[1] -= 1
elif facing == 1:
head[0] += 1
elif facing == 2:
head[1] += 1
else:
head[0] -= 1
snake.pop(-1)
snake.append(head)
board = """"""
board += "```\n " + "#" * x + "##"
for yAxis in range(y):
board += "\n #"
for xAxis in range(x):
if head == [xAxis, yAxis]:
board += "X"
else:
board += "0"
board += "#"
board += "\n " + "#" * x + "##```"
await snakeBoard.edit(content=board)
await asyncio.sleep(0.8)
async def on_message(self, message):
if message.content in ("w", "a", "s", "d"):
self.inputs.append(message)
await message.delete()
def setup(bot):
bot.add_cog(Snakes(bot))
log.info("Cog loaded: Snakes")