DEV Community

loading...

A XKCD Discord bot with a little personality

itscamlong profile image Cam Long ใƒป2 min read

This is Bobby Tables.

The idea started, as many do, on Reddit. I found a thread in the XKCD subreddit where a user had created a bot that would fetch a comic based on a number that was provided by a commenter. He named it the "BobbyTablesBot" after the famous quip of one such comic. This was in comparison to the many other bots out there that either expanded information about an already existing comic, or only allowed fetching a random comic.

I'm one of the moderators of a Discord community for computer science students at my school. We have just over 1000 members with several hundred of them active at any given time. Needless to say, as active coders we're referencing XKCD comics all the time. The opportunity to create something for this community became immediately clear.

So I got to work, and wrote my own BobbyTablesBot. It takes a number and immediately returns a link to the relevant comic (Discord's wonderful embed functionality does the rest). But my community wanted more - so I expanded the functionality to allow for a text search, as well as that random comic. I even added the ability to find the ExplainXKCD for those off days we all have.

Here he is in action:
example

Open source, because I love you

The bot is registered with Discord, but not currently public. If you're interested in running it on your own server, contact me!

GitHub logo itscamlong / BobbyTablesBot

A XKCD Discord bot

BobbyTablesBot

A XKCD Discord bot

A simple Python Discord bot that takes in a XKCD comic number and returns a URL, relying on embeds to provide the actual comic image and alt-text. It also allows the user to input a string and get a guess at a relevant XKCD. Finally, no argument returns the latest XKCD.

New! Because life is complicated, you can now request the ExplainXKCD page of a particular comic.

Credit for name goes to BobbyTablesBot on Reddit.

The search is entirely based on adtac's Relevant XKCD search API. It wouldn't be possible without him, so many thanks there!




How I built it

The bot itself is pretty simple under the hood. I used Python as the main language (it's my daily/favorite) and thus used the discord.py API to implement all bot functionality. This was my first experience with async programming and it taught me a lot.

To achieve text search, I found an API that already had done this. I poked around until I found a endpoint and used it to retrieve a number based on text input.

The most complex part of the code, ironically enough, is retrieving the latest comic. That always lives at the base URL (xckd.com/) and the number is only mentioned at the bottom of the page... as text. Not very helpful. I ended up using BeautifulSoup to scrape the homepage and retrieve that number for me. Maybe not the cleanest solution, but it works!

Conclusions

There are a few things about this bot I might change in the future. A few bugs have presented themselves in use (I'm lucky to have such... enthusiastic... testers). The search API also returns some weird results - at some point in the future I might write my own. Overall, I'm super happy with how this turned out. The end product might not be all that much, but my community loves it, and so do I.

Discussion (0)

Forem Open with the Forem app