A while back, I noticed that a lot of the memes sent in the #memes channel were from Reddit, specifically r/ProgrammerHumor. Since I was bored, I wanted to make a bot that takes the top posts from that subreddit and post them in the Discord channel.

Basics of a Discord bot

A few days ago, I started with this guide but for some reason it wouldn’t find the file on my Mac. I forgot about the idea for a few days, but then remembered it today and got to work, this time on my PC. It somehow worked, and I’ll have to figure out what the problem is. I followed along the guide until I got to the section “Responding to Events,” and then I took a look at “Connecting a Bot.”

# bot.py
import os
import random

from discord.ext import commands
from dotenv import load_dotenv

TOKEN = os.getenv('DISCORD_TOKEN')

bot = commands.Bot(command_prefix='!')

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx):
    brooklyn_99_quotes = [
        'I\'m the human form of the 💯 emoji.',
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'

    response = random.choice(brooklyn_99_quotes)
    await ctx.send(response)



I took this chunk of code and modified it to take the top 3 posts and send them in the channel. To do this, I used PRAW: The Python Reddit API Wrapper. I had to make a new Reddit account for OAuth, and everything else was in the documentation. With that, here is the actual new code:

@bot.command(name='daily', help='Top daily posts of r/ProgrammerHumor')
async def daily(num_posts):
    for submission in reddit.subreddit("programmerhumor").top("day", limit=3):
        # print(submission.url)
        title = submission.title
        image = submission.url
        await num_posts.send(title + "\n" + image)

It was simple enough - once I typed


in a channel, the bot replied with the titles and images:


And that’s it! Special thanks to Jake Cover from ACM for giving me resources to start on this.

xkcd 1646