Skip to content

Commit

Permalink
Merge pull request #1 from JeffP07/main
Browse files Browse the repository at this point in the history
Update Bot
  • Loading branch information
JeffP07 committed Mar 2, 2024
2 parents df63c54 + 0252c18 commit 6bd34db
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CLIENT_ID='' # Reddit application client ID
CLIENT_SECRET='' # Reddit application client secret
USERNAME='' # Bot's Reddit username
PASSWORD='' # Bot's Reddit password
WHITELIST='' # List of Reddit usernames to sticky posts. Format: ["Username1", "Username2"]
SUBREDDIT='' # Subreddit to watch for posts. Do not include the /r/
52 changes: 52 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Docker Image CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: replace_envs
uses: franzbischoff/replace_envs@v2
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.API_KEY }}
USERNAME: ${{ secrets.USERNAME }}
PASSWORD: ${{ secrets.PASSWORD }}
WHITELIST: ${{ secrets.WHITELIST }}
SUBREDDIT: ${{ secrets.SUBREDDIT }}
- name: Docker Metadata action
id: meta
uses: docker/metadata-action@v5.5.1
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.11.8-alpine

# Copy requirements and install into image
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade -r requirements.txt

# Copy project into image
COPY . /app
WORKDIR /app

# Run bot.py
CMD ["python", "-u", "bot.py"]
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Environment Variables

### Set these environment variables by either editing the .env file before building the Docker image or adding them in the Docker container's settings

- CLIENT_ID `# Reddit application client ID`
- CLIENT_SECRET `# Reddit application client secret`
- USERNAME `# Bot's Reddit username`
- PASSWORD `# Bot's Reddit password`
- WHITELIST `# List of Reddit usernames that will have their posts stickied. Format: ["Username1", "Username2"]`
- SUBREDDIT `# Subreddit to watch for posts. Do not include the r/`
25 changes: 17 additions & 8 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import os
import ast
from dotenv import load_dotenv
import praw

# Load environment variables from .env file
# Will not overwrite existing environment variables
load_dotenv()

# Reddit API credentials
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
user_agent = 'bot by /u/YOUR_USERNAME'
username = 'YOUR_USERNAME'
password = 'YOUR_PASSWORD'
client_id = os.environ["CLIENT_ID"]
client_secret = os.environ["CLIENT_SECRET"]
user_agent = 'Reddit Sticky Bot'
username = os.environ["USERNAME"]
password = os.environ["PASSWORD"]

# Whitelisted users
whitelisted_users = ['User1', 'User2', 'DeveloperUsername', 'CommunityManager']
whitelisted_users = ast.literal_eval(os.environ["WHITELIST"])
whitelisted_users_lower = [name.lower() for name in whitelisted_users]

# Initialize PRAW with your credentials
reddit = praw.Reddit(client_id=client_id,
Expand All @@ -18,7 +26,7 @@
password=password)

# Subreddit to monitor
subreddit_name = 'YOUR_SUBREDDIT'
subreddit_name = os.environ["SUBREDDIT"]

# The text for the sticky comment
sticky_comment_text = """
Expand All @@ -27,9 +35,10 @@
"""

def sticky_comment_on_whitelisted_user_post():
print(f"Bot started and listening in r/{subreddit_name}")
subreddit = reddit.subreddit(subreddit_name)
for submission in subreddit.stream.submissions():
if submission.author and submission.author.name in whitelisted_users:
if submission.author and (submission.author.name.lower() in whitelisted_users_lower):
print(f"Found a post by whitelisted user: {submission.author.name}")
# Post the comment
comment = submission.reply(sticky_comment_text)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-dotenv
praw

0 comments on commit 6bd34db

Please sign in to comment.