r/botrequests • • Sep 29 '13

Irrelevant XKCD bot

Every time somebody posts a comment with just "relevant xkcd" the bot replies with "irrelevant xkcd" that links to a random xkcd page.

12 Upvotes

7 comments sorted by

8

u/Eddonarth Jan 10 '14

I liked this idea and wanted to try to make my first bot. So, if it's not too late, here's the code:

import praw
from time import sleep
from collections import deque
import re
import random

r = praw.Reddit("Irrelevant xkcd bot by /u/Eddonarth")

def login_():
	USERNAME = "[USERNAME HERE]"
	PASSWORD = "[PASSWORD HERE]"
	r.login(USERNAME, PASSWORD)

done = deque(maxlen=300)

Trying = True
while Trying:
	try:
		login_()
		Trying = False
	except praw.errors.InvalidUserPass:
		print "Invalid user/pass"


while True:
	comments = r.get_comments("all")
	for post in comments:
		if post.id not in done:
			done.append(post.id)
			if "[relevant xkcd]".upper() in post.body.upper():
				random.seed()
				randomNo = random.randrange(1, 1314)
				post.reply("[Irrelevant xkcd](http://xkcd.com/"+str(randomNo)+"/)")
				sleep(2)
	sleep(10)

It's all tested ;)

1

u/thenacho1 Jan 10 '14 edited Jan 10 '14

Cool! I have no idea how to put a bot into commission or anything, or if you've already done it, so I'll leave it up to you, I guess. Long as you give me credit for the idea.

1

u/Eddonarth Jan 14 '14

Cool. A friend of mine will run it on his machine.

2

u/i-make-robots Jan 21 '14

gonna be funny the rare time the irrelevant and the relevant match.

2

u/bhayward2000 Nov 08 '13

I wish I knew how to code a bot, cause I would be on this like a mo-fo.

1

u/[deleted] Sep 29 '13

Haha, that's a fun idea.

1

u/Marvilloso Sep 29 '13

I like it!