mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-19 18:16:34 +01:00
add xkcd command
This commit is contained in:
parent
d2e1b7fb60
commit
5a6947cc72
1 changed files with 37 additions and 0 deletions
37
cogs/other.py
Normal file
37
cogs/other.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import discord
|
||||
import aiohttp
|
||||
import random
|
||||
from datetime import datetime
|
||||
from discord.ext import commands
|
||||
|
||||
|
||||
class OtherCog(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
@commands.slash_command()
|
||||
async def xkcd(self, ctx: discord.ApplicationContext, *, number: int = None):
|
||||
"""Shows an XKCD comic"""
|
||||
async with aiohttp.ClientSession() as session:
|
||||
if number is None:
|
||||
async with session.get("https://c.xkcd.com/random/comic") as response:
|
||||
if response.status != 302:
|
||||
number = random.randint(100, 999)
|
||||
else:
|
||||
number = int(response['location'].split['/'][-2])
|
||||
|
||||
async with session.get("https://xkcd.com/{!s}/info.0.json".format(number)) as response:
|
||||
if response.status != 200:
|
||||
return await ctx.respond("Sorry, xkcd.com is unavailable at the moment.")
|
||||
data = await response.json()
|
||||
embed = discord.Embed(
|
||||
title=data["safe_title"],
|
||||
description=data['alt'],
|
||||
color=discord.Colour.embed_background()
|
||||
)
|
||||
embed.set_image(url=data['img'])
|
||||
return await ctx.respond(embed=embed)
|
||||
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(OtherCog(bot))
|
Loading…
Reference in a new issue