-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtommy_bot.py
41 lines (30 loc) · 1.21 KB
/
tommy_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import logging
import re
import random
import praw
from dotenv import load_dotenv
load_dotenv()
LOG_FILE = os.getenv('LOG_FILE')
SUBS = os.getenv('SUBS')
TOMMY_CHANCE = int(os.getenv('CHANCE'))
logging.basicConfig(filename = LOG_FILE,
level=logging.INFO,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
logging.info('Starting the tommy bot...')
reddit = praw.Reddit('tommybot')
subreddit = reddit.subreddit(SUBS)
pattern = re.compile('[A-Za-z\']+[!|?]+')
for comment in subreddit.stream.comments(skip_existing = True):
lastWord = comment.body.split(' ')[-1]
if pattern.match(lastWord) and random.randint(1, 100) <= TOMMY_CHANCE:
tommyReply = '...' + re.sub('[^A-Za-z\']+', '', lastWord) + re.sub('[^!|?]+', '', lastWord)
botReply = tommyReply + '''
^Beep ^boop, ^I ^am ^a ^bot. ^Downvote ^me ^if ^I'm ^being ^annoying!'''
comment.reply(botReply)
logging.info('Last words of comment: ...' + ' '.join(comment.body.split(' ')[-5:]))
# log this comment
with open('comments_replied_to.log', 'a') as f:
f.write(comment.id + '\n')
logging.info('Comment id [%s] written to file', comment.id)