From 2b3d6a9ace6140c7364e65097755ad0c74e9a0d3 Mon Sep 17 00:00:00 2001 From: Slfhstd Date: Wed, 4 Mar 2026 23:01:36 +0000 Subject: [PATCH] Initial commit --- Dockerfile | 7 ++++++ README.md | 24 +++++++++++++++++++++ bot.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ config.py | 14 ++++++++++++ docker-compose.yml | 6 ++++++ example.env | 8 +++++++ requirements.txt | 1 + 7 files changed, 114 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 bot.py create mode 100644 config.py create mode 100644 docker-compose.yml create mode 100644 example.env create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a7e3928 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +# Dockerfile for Reddit Test Posts Bot +FROM python:3.11-slim +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "bot.py"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..a37235f --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Reddit TestPostsBot + +## Usage + +1. Fill in your Reddit API credentials and subreddit name in `bot.py`. +2. Create a wiki page in your subreddit named `testpostsbot_config` with JSON like: + +``` +{ + "posts": [ + {"title": "Test Post 1", "body": "Body for post 1"}, + {"title": "Test Post 2", "body": "Body for post 2"} + ] +} +``` + +3. Build and run with Docker: + +``` +docker build -t testpostsbot . +docker run --env REDDIT_CLIENT_ID=... --env REDDIT_CLIENT_SECRET=... --env REDDIT_USERNAME=... --env REDDIT_PASSWORD=... --env SUBREDDIT=... testpostsbot +``` + +Or edit the variables directly in `bot.py` for quick testing. diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..ace2aef --- /dev/null +++ b/bot.py @@ -0,0 +1,54 @@ +# Reddit Test Posts Bot +# This script reads config from a subreddit wiki page and makes test posts if the bot is a moderator. +import praw +import json +import time +import os +from config import fetch_config_from_wiki + +REDDIT_CLIENT_ID = os.environ.get('REDDIT_CLIENT_ID') +REDDIT_CLIENT_SECRET = os.environ.get('REDDIT_CLIENT_SECRET') +REDDIT_USERNAME = os.environ.get('REDDIT_USERNAME') +REDDIT_PASSWORD = os.environ.get('REDDIT_PASSWORD') +REDDIT_USER_AGENT = os.environ.get('REDDIT_USER_AGENT', f'TestPostsBot/0.1 by {REDDIT_USERNAME}') + +SUBREDDIT = os.environ.get('SUBREDDIT') +WIKI_PAGE = os.environ.get('WIKI_PAGE', 'testpostsbot_config') + + +def is_moderator(reddit, subreddit_name): + subreddit = reddit.subreddit(subreddit_name) + mods = [str(mod) for mod in subreddit.moderator()] + return reddit.user.me().name in mods + + +def make_posts(reddit, subreddit_name, posts): + subreddit = reddit.subreddit(subreddit_name) + for post in posts: + title = post.get('title', 'Test Post') + body = post.get('body', '') + print(f"Posting: {title}") + subreddit.submit(title, selftext=body) + time.sleep(2) # avoid rate limits + + +def main(): + reddit = praw.Reddit( + client_id=REDDIT_CLIENT_ID, + client_secret=REDDIT_CLIENT_SECRET, + username=REDDIT_USERNAME, + password=REDDIT_PASSWORD, + user_agent=REDDIT_USER_AGENT + ) + + config = fetch_config_from_wiki(reddit, SUBREDDIT, WIKI_PAGE) + posts = config.get('posts', []) + + if is_moderator(reddit, SUBREDDIT): + make_posts(reddit, SUBREDDIT, posts) + else: + print(f"Bot is not a moderator of r/{SUBREDDIT}. No posts made.") + + +if __name__ == '__main__': + main() diff --git a/config.py b/config.py new file mode 100644 index 0000000..1d844a2 --- /dev/null +++ b/config.py @@ -0,0 +1,14 @@ +# config.py +# Fetches config from subreddit wiki page +import json + +def fetch_config_from_wiki(reddit, subreddit_name, wiki_page): + subreddit = reddit.subreddit(subreddit_name) + try: + wiki = subreddit.wiki[wiki_page] + config_text = wiki.content_md + config = json.loads(config_text) + return config + except Exception as e: + print(f"Error fetching config from wiki: {e}") + return {'posts': []} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9c31788 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + testpostbot: + image: slfhstd.uk/slfhstd/testpostbot:dev + env_file: + - .env + restart: unless-stopped diff --git a/example.env b/example.env new file mode 100644 index 0000000..e4788cd --- /dev/null +++ b/example.env @@ -0,0 +1,8 @@ +# Example environment variables for TestPostsBot +REDDIT_CLIENT_ID=your_client_id +REDDIT_CLIENT_SECRET=your_client_secret +REDDIT_USERNAME=your_username +REDDIT_PASSWORD=your_password +REDDIT_USER_AGENT=TestPostsBot/0.1 by your_username +SUBREDDIT=your_subreddit +WIKI_PAGE=testpostsbot_config diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6c81bc0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +praw