Initial commit
This commit is contained in:
54
bot.py
Normal file
54
bot.py
Normal 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()
|
||||
Reference in New Issue
Block a user