From 86d900dfe9fdfd81a61888ebe71bae9eb508b165 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Feb 2017 21:00:09 -0500 Subject: [PATCH] Added command to post xkcd comics --- helpcommand.txt | 5 ++++- pocketbot.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/helpcommand.txt b/helpcommand.txt index 067406c..110ab0e 100644 --- a/helpcommand.txt +++ b/helpcommand.txt @@ -8,4 +8,7 @@ **!wtf**: Don't use unless you want a wall of copypasta **!ups**: Print the status of the main UPS. Updated once a minute. **!tohru**: Posts a random pic of the one and only Tohru! -**!setplaying**: Sets the bot's Now Playing status to whatever you say (eg !setplaying with Tohru) \ No newline at end of file +**!setplaying**: Sets the bot's Now Playing status to whatever you say (eg !setplaying with Tohru) +**!xkcd**: Posts a random xkcd comic +**!xkcd latest**: Posts the latest xkcd comic +**!xkcd x**: Posts xkcd comic number x (if it exists) \ No newline at end of file diff --git a/pocketbot.py b/pocketbot.py index 8c35453..7653eec 100644 --- a/pocketbot.py +++ b/pocketbot.py @@ -4,6 +4,7 @@ import asyncio import configparser import datetime import random +import xkcd #Import config file config = configparser.ConfigParser() @@ -67,6 +68,31 @@ def on_message(message): elif message.content.lower().startswith('!setplaying'): yield from client.change_presence(game=discord.Game(name=message.content.partition(' ')[2])) + #xkcd comic fetcher + elif message.content.lower().startswith('!xkcd'): + args = message.content.partition(' ')[2] + if (args is ''): + comic = xkcd.getRandomComic() + comicnum = str (comic.number) + xkmessage = 'xkcd #' + comicnum + ': ' + comic.getTitle() + '\n' + comic.getImageLink() + '\n' + 'Alt Text: '+comic.getAltText() + yield from client.send_message(message.channel, xkmessage) + else: + if ('latest' in args): + comic = xkcd.getLatestComic() + comicnum = str (comic.number) + xkmessage = 'The most recent xkcd is #' + comicnum + ': ' + comic.getTitle() + '\n' + comic.getImageLink() + '\n' + 'Alt Text: '+comic.getAltText() + yield from client.send_message(message.channel, xkmessage) + elif (args.isdigit()): + comic = xkcd.getComic(args) + if (comic.number == -1): + yield from client.send_message(message.channel, 'There is no xkcd comic #' + args + '. Please try again.') + else: + comicnum = str (comic.number) + xkmessage = 'xkcd is #' + comicnum + ': ' + comic.getTitle() + '\n' + comic.getImageLink() + '\n' + 'Alt Text: '+comic.getAltText() + yield from client.send_message(message.channel, xkmessage) + else: + yield from client.send_message(message.channel, '`'+args+'`' + 'is not a valid option. Please leave blank for a random comic, say "latest" for the latest comic or put the number of the comic you want.' ) + client.loop.create_task(ups_check()) client.run(token) \ No newline at end of file