Advertisement
AdditionalThinking

ILMM Bot

Jun 14th, 2017
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. #/u/I_Like_Monster_Math Python Reddit Bot code
  2. #written by reddit user AdditionalThinking
  3. #not the same person as /u/I_Hate_Monster_Math
  4.  
  5. import praw, time
  6. def postlog(): #posts the most recent replies, 'trying too often' failures and
  7.                #timeout errors to a debug thread.
  8.                #none of these subroutines are necessary for the bot to work, but
  9.                #they are integrated into the rest of the code, so not easily disabled
  10.     temp="Last 10 Replies"
  11.     for log in replylogs:
  12.         temp=temp+"\n\n"+log
  13.     temp=temp+"\n\n"+"Last 10 Fails"
  14.     for log in faillogs:
  15.         temp=temp+"\n\n"+log
  16.     temp=temp+"\n\n"+"Last 10 Timeouts"
  17.     for log in timeoutlogs:
  18.         temp=temp+"\n\n"+log
  19.     print("POSTED")
  20.     reddit.submission(id="").edit(temp)   #id is the post id for a debug thread
  21. def update(logs,log):
  22.     for i in range(0,9):
  23.         logs[i]=logs[i+1]
  24.     logs[9]=log
  25.     return logs
  26. def formattedtime():
  27.     return time.strftime("%d/%m %H:%M:%S")
  28. timeoutlogs=["Null","Null","Null","Null","Null","Null","Null","Null","Null","Null",]
  29. replylogs=["Null","Null","Null","Null","Null","Null","Null","Null","Null","Null",]
  30. faillogs=["Null","Null","Null","Null","Null","Null","Null","Null","Null","Null",]
  31.  
  32. #start of the actual bot
  33.  
  34. reddit=praw.Reddit("bot1") #follow tutorial at http://pythonforengineers.com/build-a-reddit-bot-part-1/
  35. subreddit=reddit.subreddit("all")
  36. while True:
  37.     try:
  38.         for comment in subreddit.stream.comments():
  39.             if comment.body=="ShutdownILMMB": #emergency code. comment this anywhere on reddit and the bot will stop
  40.                 quit()
  41.                 while True:
  42.                     print("Error")
  43.             if comment.author == "I_Hate_Monster_Math":
  44.                 if comment.body=="/r/dontfuckingsayit":
  45.                     if comment.created_utc>(time.time()-100): #when the comment stream starts, it will pick up the first
  46.                                                               #100 comments on reddit. this makes sure it ignores those.
  47.                                                               #Not technically necessary for this specific bot.
  48.                         comment.reply("/r/TheyDidTheMonsterMath")
  49.                         print("Replied to ", comment.id)
  50.                         replylogs=update(replylogs,comment.id+" at "+formattedtime()+" UTC Type1")
  51.                         postlog()
  52.                     else:
  53.                         print("Found", comment.id, "\n",comment.created,"<",time.time()-100,sep="")
  54.                 elif comment.body=="DID I STUTTER":
  55.                     if comment.created_utc>(time.time()-100):
  56.                         comment.reply("/r/ItWasAGraveyardGraph")
  57.                         print("Replied to ", comment.id)
  58.                         replylogs=update(replylogs,comment.id+" at "+formattedtime()+" UTC Type2")
  59.                         postlog()
  60.     except Exception as exception: #this will catch timeout errors every hour and 7 seconds
  61.         print("Timeout",exception,formattedtime())
  62.         timeoutlogs=update(timeoutlogs,formattedtime()+" UTC")
  63.         try:
  64.             postlog()
  65.         except Exception:
  66.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement