Meme

This Cog enables the bot’s meme-sending capabilities.

Hint

To enable this cog, set the ENABLE_MEME variable.

Packages

asyncpraw, the Asynchronous Python Reddit API Wrapper, is used to get the images from Reddit.

env variables

Name

Description

REDDIT_USERNAME

The username of your Reddit bot

REDDIT_PASSWORD

Its password

REDDIT_CLIENT_ID

Its client ID

REDDIT_CLIENT_SECRET

Its secret key

REDDIT_APP_VERSION

This is an arbitrary value, see Creating a Reddit bot

MEME_DOWNLOAD

Whether to download the memes or not

MEME_DIR

Where to download them if MEME_DOWNLOAD is enabled

MEME_ITEM_LIMIT

The maximum number of items to fetch, see How it works

Creating a Reddit bot

The variables starting with REDDIT correspond to a Reddit bot’s credentials. According to its docs, to use asyncpraw we need:

  • A Reddit account: to create a new account, head to reddit.com. You can create an account without providing an email address (just omit it), but be aware this means there’s no way to recover the account if the password is lost.

  • A Client ID and Secret: see this guide.

  • A User Agent: this Cog already generates one following the format provided by Reddit’s API guide. Thus, I recommend letting the bot take care of that with the information provided by .env. Note that the APP_VERSION is set arbitrarily; I use the same version as the bot’s.

New in version 2.0.0: The bot can send memes without downloading them (MEME_DOWNLOAD).

New in version 3.0.0: The MEME_DIR directory is created by the configuration script.

How it works

List files

These are the files created when the command is used for the first time in a server or DM. They contain a list of the names of all the files sent to that server/DM. This allows the bot to check whether a meme was already sent to that context, and thus avoid sending duplicates. These list files are saved in the directory pointed by MEME_DIR.

Asyncpraw

When starting, the bot creates an asyncpraw.Reddit instance. This instance fetches the top MEME_ITEM_LIMIT posts from the Hot category of a subreddit.

If the post is actually an image (not a mod post, nor a video, nor a text-only post), it checks if it already sent it to that context (server or DM). If it did, it skips that post and checks the next one fetched, so it can eventually run out of posts to check.

If you use the meme command frequently with the same subreddit, you should increase the item limit so the bot can send more new memes before reaching this cap.

Downloading memes

Since version 2.0.0, you can choose skip the download of the memes to send. This uses the fact that discord.Embed can use a URL to include an image directly.

You can still enable the download of the images, which are then used to send the messages. These are saved in the directory set in MEME_DIR.

A meme’s filename is derived from its i.redd.it URL. For example, from i.redd.it/thisisnotameme.jpg we get thisisnotameme.jpg.