import discord
from discord.ext import commands
import requests
import random
import io
bot = commands.Bot(command_prefix="!")
def get_total_comics():
response = requests.get("https://xkcd.com/info.0.json").json()
print(response["num"])
return int(response["num"])
def get_url(total):
url = "https://xkcd.com/" + str(random.randrange(1, total)) + "/info.0.json"
return url
def get_img(url):
response = requests.get(url).json()
img_url = response["img"]
img_content = requests.get(img_url).content
img = io.BytesIO(img_content)
return img
@bot.command()
async def xkcd(ctx):
total = get_total_comics()
url = get_url(total)
img = get_img(url)
await ctx.send(file=discord.File(img, 'comic.png'))
bot.run("INSERT TOKEN HERE")
pip install discord requests
python bot.py
python3 -m venv .venv
source .venv/bin/activate
pip install discord requests
python bot.py