Creating ExistentialRickBot, probably the silliest Redditbot yet

So I'd written SchmeckleBot, a simple bot that converts currency from schmeckles to USD. But I decided to go even simpler, still in the theme of Rick and Morty (if you loved Futurama watch this!).

In the pilot episode, when Morty sees something during a chase scene that causes an existential crisis, Rick quickly snaps him out of it with the pithy quote 'Don't think about it!'.

It should be immediately obvious that a reddit bot needs to exist and reply with that quote whenever an existential question is asked on the r/rickandmorty subreddit. And thus was born ExistentialRickBot.

The core logic is dead simple, if an existential question is posted, respond with an existential answer.

questions = ['why', 'happen', 'think'] # Match if any of these are found in message
def isExistentialQuestion(message):
  return '?' in message and any([q in message.lower() for q in questions])

def getAnswerToExistentialQuestion():
  return "The answer is don't think about it."

You'd think something as simple as this would have less of an impact on a community than a more complex bot like Tensorflow chessbot or SchmeckleBot, but no. ExistentialRickBot has gained the most karma points the quickest by far. Here is one of its responses


ExistentialRickBot360 points 
The answer is don't think about it.

I am a bot | what is my purpose


We can dive a little deeper into the reddit python API (PRAW) logic. We have a main loop running that checks submissions in real time (praw helpfully provides a yielding stream). For each submission we check if the title is an existential question, and if so, we generation a response and reply.

while running:
  # get submission stream
  try:
    submissions = submission_stream(r, subreddit, limit=submission_read_limit)
    # for each submission
    for submission in submissions:
      count += 1
      # print out some debug info
      logInfoPerSubmission(submission, count, count_actual)

      # Skip if already processed
      if submission.id in already_processed:
        continue
      
      # check if submission title is a question
      if isExistentialQuestion(submission.title):
        # generate response
        msg = "%s%s" % (getAnswerToExistentialQuestion(), getResponseFooter())
        # respond, keep trying till success
        while True:
          try:
            print("> %s - Adding comment to %s: %s" % (datetime.now(), submission.id, submission))
            submission.add_comment(msg)
            # update & save list
            already_processed.add(submission.id)
            saveProcessed(already_processed)
            count_actual += 1
            # Wait after submitting to not overload
            waitWithComments(300)
            break
          except praw.errors.AlreadySubmitted as e:
            print("> %s - Already submitted skipping..." % datetime.now())
            break
          except praw.errors.RateLimitExceeded as e:
            print("> {} - Rate Limit Error for commenting on {}, sleeping for {} before retrying...".format(datetime.now(), submission.id, e.sleep_time))
            waitWithComments(e.sleep_time)

We keep track of submissions we've previously responded to in a set, stored in a text file (we can regenerate easily enough by doing a search for all responses by the bot using praw as well).

Check out some of it's most recent posts from its user page: ExistentialRickBot

Source code on Github

Popular posts from this blog

Strawberry DNA extraction and viewing with a cheap USB Microscope

Visualizing TLE Orbital Elements with Python and matplotlib

Relearning web development by writing a clicking game using React, Npm and Browserify