Last active
August 24, 2016 17:11
-
-
Save myrkvi/868b44dd0fbba871bacfe0176df91bf5 to your computer and use it in GitHub Desktop.
Feel free to use this however you want, but if code from this is published, please give credit :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/env python3 | |
# Dependencies: | |
# discord.py https://github.com/Rapptz/discord.py | |
# requests http://docs.python-requests.org/en/master/ | |
import discord | |
import asyncio | |
import requests | |
import json | |
from io import BytesIO | |
client = discord.Client() | |
@client.event | |
async def on_ready(): | |
await client.change_status(discord.Game(name="¡xkcd or ¡xkcd <num>", url="http://github.com/myrkvi/", type=1)) | |
print("Logged in as:", client.user.name, client.user.id) | |
print("------------------------------") | |
@client.event | |
async def on_message(message): | |
if message.content.startswith("¡xkcd") or message.content.startswith("ixkcd"): | |
msg = message.content.split(" ") | |
await client.send_typing(message.channel) | |
if len(msg) > 1: | |
num = msg[1] | |
r = requests.get("http://xkcd.com/" + num +"/info.0.json") | |
if r.status_code != 200: | |
await client.send_message(message.channel, "`{}` is an invalid argument!".format(num)) | |
return | |
else: | |
r = requests.get("http://xkcd.com/info.0.json") | |
rJson = r.json() | |
date = "{}-{}-{}".format(rJson["year"], rJson["month"], rJson["day"]) | |
title = rJson["title"] | |
image = rJson["img"] | |
alt = rJson["alt"] | |
num = rJson["num"] | |
await client.send_file(message.channel, BytesIO(requests.get(image).content), filename="{}.png".format(num)) | |
await client.send_message(message.channel, "({}) **{}** {}\n\"{}\"".format(num, title, date, alt)) | |
client.run("<TOKEN>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment