Initial commit

This commit is contained in:
2026-03-04 23:01:36 +00:00
commit 2b3d6a9ace
7 changed files with 114 additions and 0 deletions

7
Dockerfile Normal file
View File

@@ -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"]

24
README.md Normal file
View File

@@ -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.

54
bot.py Normal file
View File

@@ -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()

14
config.py Normal file
View File

@@ -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': []}

6
docker-compose.yml Normal file
View File

@@ -0,0 +1,6 @@
services:
testpostbot:
image: slfhstd.uk/slfhstd/testpostbot:dev
env_file:
- .env
restart: unless-stopped

8
example.env Normal file
View File

@@ -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

1
requirements.txt Normal file
View File

@@ -0,0 +1 @@
praw