finished xkcd

This commit is contained in:
DasMoorhuhn 2023-02-04 14:09:04 +01:00
parent a632d1b76c
commit 67b39a42a9
2 changed files with 24 additions and 13 deletions

View File

@ -22,16 +22,24 @@ class XKCD:
def getRandomComic(self): def getRandomComic(self):
response = requests.get(url=f"https://c.xkcd.com/random/comic") response = requests.get(url=f"https://c.xkcd.com/random/comic")
with open(file="data/log/comic.html", mode="w") as file:
file.write(response.text)
file.close()
for line in response.text: for line in response.text.split("\n"):
if '<meta property="og:url"' in line: if '<meta property="og:url"' in line:
with open(file="data/log/test.txt", mode="a") as file: line = line.split('"')
file.write(line) for cell in line:
file.close() if "https" in cell:
return self.getSpecificComic(url=cell)
def getLastComic(self): def getLastComic(self):
response = requests.api.get(f"{self.url}/{self.__api}") response = requests.api.get(f"{self.url}/{self.__api}")
return Comic(json.loads(response.text)) return Comic(json.loads(response.text))
def getSpecificComic(self, num=None, url=None):
if num != None:
response = requests.api.get(url=f"{self.url}/{num}/{self.__api}")
return Comic(json.loads(response.text))
if url != None:
response = requests.api.get(url=f"{url}/{self.__api}")
return Comic(json.loads(response.text))

View File

@ -97,8 +97,11 @@ async def slash(interaction: Interaction):
@tree.command(name='get-random-comic', description='Get a random comic from XKCD') @tree.command(name='get-random-comic', description='Get a random comic from XKCD')
async def slash(interaction: Interaction): async def slash(interaction: Interaction):
xkcd.getRandomComic() comic = xkcd.getRandomComic()
await interaction.response.send_message("hi")
embed = discord.Embed(title=comic.title, color=Color.blue(), url=f"{xkcd.url}/{comic.num}")
embed.set_image(url=comic.img)
await interaction.response.send_message(embed=embed)
@tree.command(name="info", description="get info about this server") @tree.command(name="info", description="get info about this server")
async def slash(interaction: Interaction): async def slash(interaction: Interaction):
@ -130,9 +133,9 @@ async def slash(interaction: Interaction):
logger.info("Command: help") logger.info("Command: help")
commandListString = \ commandListString = \
"""\ """\
`/info`: Get infos about the server and the Bot `/info`: Get infos about the server and the Bot\n
`/get-random-comic`: Not working yet `/get-random-comic`: Not working yet\n
`/get-latest-comic`: Get latest comic from XKCD `/get-latest-comic`: Get latest comic from XKCD\n
`/help`: Get this view `/help`: Get this view
""" """
embed = discord.Embed(title=f"Help", description="List of commands", color=Color.blue()) embed = discord.Embed(title=f"Help", description="List of commands", color=Color.blue())