Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/u/I_Like_Monster_Math Python Reddit Bot code
- #written by reddit user AdditionalThinking
- #not the same person as /u/I_Hate_Monster_Math
- import praw, time
- def postlog(): #posts the most recent replies, 'trying too often' failures and
- #timeout errors to a debug thread.
- #none of these subroutines are necessary for the bot to work, but
- #they are integrated into the rest of the code, so not easily disabled
- temp="Last 10 Replies"
- for log in replylogs:
- temp=temp+"\n\n"+log
- temp=temp+"\n\n"+"Last 10 Fails"
- for log in faillogs:
- temp=temp+"\n\n"+log
- temp=temp+"\n\n"+"Last 10 Timeouts"
- for log in timeoutlogs:
- temp=temp+"\n\n"+log
- print("POSTED")
- reddit.submission(id="").edit(temp) #id is the post id for a debug thread
- def update(logs,log):
- for i in range(0,9):
- logs[i]=logs[i+1]
- logs[9]=log
- return logs
- def formattedtime():
- return time.strftime("%d/%m %H:%M:%S")
- timeoutlogs=["Null","Null","Null","Null","Null","Null","Null","Null","Null","Null",]
- replylogs=["Null","Null","Null","Null","Null","Null","Null","Null","Null","Null",]
- faillogs=["Null","Null","Null","Null","Null","Null","Null","Null","Null","Null",]
- #start of the actual bot
- reddit=praw.Reddit("bot1") #follow tutorial at http://pythonforengineers.com/build-a-reddit-bot-part-1/
- subreddit=reddit.subreddit("all")
- while True:
- try:
- for comment in subreddit.stream.comments():
- if comment.body=="ShutdownILMMB": #emergency code. comment this anywhere on reddit and the bot will stop
- quit()
- while True:
- print("Error")
- if comment.author == "I_Hate_Monster_Math":
- if comment.body=="/r/dontfuckingsayit":
- if comment.created_utc>(time.time()-100): #when the comment stream starts, it will pick up the first
- #100 comments on reddit. this makes sure it ignores those.
- #Not technically necessary for this specific bot.
- comment.reply("/r/TheyDidTheMonsterMath")
- print("Replied to ", comment.id)
- replylogs=update(replylogs,comment.id+" at "+formattedtime()+" UTC Type1")
- postlog()
- else:
- print("Found", comment.id, "\n",comment.created,"<",time.time()-100,sep="")
- elif comment.body=="DID I STUTTER":
- if comment.created_utc>(time.time()-100):
- comment.reply("/r/ItWasAGraveyardGraph")
- print("Replied to ", comment.id)
- replylogs=update(replylogs,comment.id+" at "+formattedtime()+" UTC Type2")
- postlog()
- except Exception as exception: #this will catch timeout errors every hour and 7 seconds
- print("Timeout",exception,formattedtime())
- timeoutlogs=update(timeoutlogs,formattedtime()+" UTC")
- try:
- postlog()
- except Exception:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement