How to create a bot that publishes the daily top post of a subreddit to a Discord channel (Docker).
1 - Create a new Dockerfile in your project directory:
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "bot.py" ]
This Dockerfile uses the official Python 3.9 image as a base, sets the working directory to /app
, copies the requirements.txt
file, installs the required packages, copies the rest of the files in the directory, and runs the bot.py
script when the container starts.
2 - Create a docker-compose.yml
file in your project directory:
version: '3'
services:
bot:
build: .
environment:
- DISCORD_TOKEN=<your-bot-token>
- REDDIT_CLIENT_ID=<your-reddit-client-id>
- REDDIT_CLIENT_SECRET=<your-reddit-client-secret>
- REDDIT_USER_AGENT=<your-reddit-user-agent>
- DISCORD_SERVER_ID=<your-discord-server-id>
- DISCORD_CHANNEL_ID=<your-discord-channel-id>
- SUBREDDIT=<your-subreddit-name>
restart: always
docker-compose.yml
file defines a service named bot
that builds the Docker image from the current directory, sets environment variables for the bot token, Reddit API credentials, Discord server and channel IDs, and subreddit name, and restarts the container if it crashes or stops.3 - Create a requirements.txt
file in your project directory with the following contents:
discord.py
praw
This file specifies the required Python packages for the bot to run.
4 - Modify your bot.py
script to use environment variables for the bot token, Reddit API credentials, Discord server and channel IDs, and subreddit name, like this:
import os
import discord
import praw
import datetime
# Discord bot token
TOKEN = os.environ["DISCORD_TOKEN"]
# Reddit API credentials
REDDIT_CLIENT_ID = os.environ["REDDIT_CLIENT_ID"]
REDDIT_CLIENT_SECRET = os.environ["REDDIT_CLIENT_SECRET"]
REDDIT_USER_AGENT = os.environ["REDDIT_USER_AGENT"]
reddit = praw.Reddit(
client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT
)
# Subreddit to retrieve top post from
SUBREDDIT = os.environ["SUBREDDIT"]
# Discord server and channel to post to
SERVER_ID = int(os.environ["DISCORD_SERVER_ID"])
CHANNEL_ID = int(os.environ["DISCORD_CHANNEL_ID"])
# Get today's date in the format "MM/DD/YYYY"
today = datetime.datetime.now().strftime("%m/%d/%Y")
# Get the top post of the day from the subreddit
top_post = reddit.subreddit(SUBREDDIT).top(time_filter="day", limit=1).__next__()
# Build the message to post to Discord
message = f"**Top post on r/{SUBREDDIT} for {today}:**\n\n{top_post.title}\n\n{top_post.url}"
# Authenticate the Discord bot using its token
client = discord.Client()
@client.event
async def on_ready():
# Get the Discord server and channel to post to
server = client.get_guild(SERVER_ID)
channel = server.get_channel(CHANNEL_ID)
# Post the message to the Discord channel
await channel.send(message)
# Run the Discord bot
client.run(TOKEN)
5 - Start the Docker
You can then use the docker build .
command to build a Docker image from the Dockerfile in your work directory.
You would be able to see your docker images by the below command:
docker images
Without a name, just using the ID:
docker run -i -t 8dbd9e392a96 /bin/bash
Creating a discord bot and getting a token.
How to get client_id and client_secret.
Discord Channel ID
No Comments