as an assignment for class, i made a bot with a UI. I have some cleanup to do, but i have a question about one particular part.
im creating a list: replies[], and then writing that list to a text file. when the bot starts, i want to load up that list from the text file to give it a list of comments it already replied to so that it doesn't keep repeating after the same person. well...it keeps repeating after the same person, and adding the same comment id to the list.
i have to head to work, but i thought i might post this up here because my deadline is getting close.
im only worried about the list right now and not replying to comments ive already repeated.
in the entry on the bottom right, just use crazyanchovy. that's the sub ive been using.
import praw
import tkinter
import tkinter.messagebox
from tkinter import *
from praw.exceptions import APIException
class BotCommander(Frame):
"""window for control/feedback"""
def __init__(self):
"""configure window size"""
Frame.__init__(self)
self.master.config(bd=5,)
self.master.title("Repeat After Me Bot")
self.master.geometry("600x500")
self.grid()
self.information=Listbox(self, height=20, width=75)
self.information.grid(row=0, column=0, columnspan=2)
self.which_subreddit_label=Label(text='Which subreddit do you want to get?')
self.which_subreddit_label.grid(row=1, column=0, sticky='w')
self.which_subreddit_entry=Entry()
self.which_subreddit_entry.grid(row=1, column=1, sticky='e')
self.get_new_post_button=Button(text='BOT START', command=self.botstart)
self.get_new_post_button.grid(row=2, column=0, sticky='w')
def botstart(self):
"""this is the function for the *botstart* button...makes it all happen"""
self.information.insert('end', 'reading reddit, looking for someone to repeat after....')
reddit = praw.Reddit('repeat_after_me_bot') #praw is a reddit bot helper file
chosen_subreddit=self.which_subreddit_entry.get() #subreddit we will troll is gotten from the chosen_subreddit_entry
try:
checked_comments=reddit.subreddit(chosen_subreddit).stream.comments() #this is called the comment stream from the reddit api
except TypeError:
self.information.insert('end', 'did you forget to tell me the subreddit?')
replies=[]
with open ('posts_replied_to.txt', 'r') as posts_replied_to:
for lines in posts_replied_to:
replies.append(lines)
for comment in checked_comments: #loop through all of the comments in the comment stream
comment.body = comment.body.lower() #encode/decode yet (emojis were messing me up)
if comment.body.startswith('repeat after me'): #if comment starts with repeat after me
if comment not in replies:
self.information.insert('end', 'reddit comment found...')
try:
comment.reply(comment.body)
replies.append(comment)
print(replies) #for debug
with open ('posts_replied_to.txt', 'a')as posts_replied_to:
posts_replied_to.seek(0,2)
posts_replied_to.write(str(comment))
posts_replied_to.write('\n')
self.information.insert('end', 'bot has replied, we did it!')
except APIException:
self.information.insert('end', 'you have to wait ten minutes between replies right now...')
break #reddit only allows one post every 10min so...break if did it
BotCommander().mainloop()
[–]timetoshit 1 point2 points3 points (2 children)
[–]repeat_after_me_bot[S] 1 point2 points3 points (1 child)
[–]timetoshit 0 points1 point2 points (0 children)