Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/u/GoldenSights
- import praw # simple interface to the reddit API, also handles rate limiting of requests
- import time
- import os
- import sys
- import sqlite3
- '''USER CONFIGURATION'''
- USERNAME = ""
- #This is the bot's Username. In order to send mail, he must have some amount of Karma.
- PASSWORD = ""
- #This is the bot's Password.
- USERAGENT = ""
- #This is a short description of what the bot does. For example "/u/GoldenSights' Newsletter bot"
- SUBRESTRICT = ["GoldTesting"]
- #This is the subreddit where the bot is allowed to post.
- #If a user inputs a permalink that does not lead to this subreddit, the post will fail.
- #To allow any, delete everything between the brackets.
- COMHEADER = "This is at the top of the comment\n\n"
- #Comment Header
- COMFOOTER = "\n\nThis is at the bottom of the comment"
- #Comment Footer
- PMHEADER = "This is at the top of the return PM\n\n"
- #PM Header
- PMFOOTER = "\n\nThis is at the bottom of the return PM"
- #PM Footer
- PMSUCCESS = "Your comment has successfully been created: _permalink_"
- #This will be sent to the user when his post succeeds
- #_permalink_ will be replaced by the successful comment's permalink. You may move this around as you please.
- PMFAILURE = "Your comment has been rejected for the following reason(s):\n\n"
- #This will be sent to the user when his post fails. Error messages will be displayed
- ERRBANNED = "- You have been banned from using this service"
- #If an admin has placed this user on the banlist, return this error
- ERRFETCH = "- Failed to fetch comment object given that permalink. You should use the permalink exactly as it appears when you cut / copy from your address bar. The last 7 characters are the comment's id number."
- #If praw fails in fetching the object from ID, return this error
- ERRNOPERMA = "- The first line of your PM must be a permalink to a comment."
- #If the first line does not contain any visible permalink, return this error
- BANCOMMAND = "banuser"
- UNBANCOMMAND = "unbanuser"
- #The ADMIN may use these commands to ban / unban a username
- DISTINGUISHCOMMENT = False
- #If your bot is going to be operating in a sub where it is a moderator, you may choose to distinguish the comment
- #Use True or False (With Capitals! No quotation marks!)
- WAIT = 30
- #This is how many seconds you will wait between cycles. The bot is completely inactive during this time.
- ADMIN = "GoldenSights"
- '''All done!'''
- try:
- import bot #This is a file in my python library which contains my Bot's username and password. I can push code to Git without showing credentials
- USERNAME = bot.getuG()
- PASSWORD = bot.getpG()
- USERAGENT = bot.getaG()
- except ImportError:
- pass
- WAITS = str(WAIT)
- banlist = []
- sql = sqlite3.connect('sql.db')
- print('Loaded SQL Database')
- cur = sql.cursor()
- cur.execute('CREATE TABLE IF NOT EXISTS oldposts(ID TEXT)')
- print('Loaded old comments')
- cur.execute('CREATE TABLE IF NOT EXISTS banned(name TEXT)')
- print('Loaded banned users')
- sql.commit()
- r = praw.Reddit(USERAGENT)
- r.login(USERNAME, PASSWORD)
- def scanPM():
- cur.execute('SELECT * FROM banned')
- fetched = cur.fetchall()
- for m in fetched:
- banlist.append(m[0])
- print(str(len(banlist)) + ' banned users.\n')
- print('Searhing Inbox.')
- pms = r.get_unread(unset_has_mail=True, update_user=True)
- for pm in pms:
- result = []
- failresult = []
- failoverride = False
- cobj = ''
- print(pm.id)
- try:
- author = pm.author.name
- if author.lower() == ADMIN.lower():
- line = pm.body.lower().split()[0]
- if BANCOMMAND.lower() in line:
- user = line[-1]
- print('\t[ ] ADMIN has banned ' + user)
- cur.execute('INSERT INTO banned VALUES(?)', [user])
- failoverride = True
- if UNBANCOMMAND.lower() in line:
- user = line[-1]
- print('\t[ ] ADMIN has unbanned ' + user)
- cur.execute('DELETE FROM banned WHERE name=?', [user])
- failoverride = True
- bodysplit = pm.body.lower().split()
- if '://reddit.com/r/' in bodysplit[0]:
- for word in bodysplit[0].split():
- if '://reddit.com/r/' in word
- link = word
- if '/' == link[-8]:
- link = link[-7:]
- elif = '/' == link[-1] and '/' == link[-9]:
- link = link[-8:-1]
- try:
- cobj = r.get_info(thing_id='t1_' + link,fetch=True)
- print('\t[ ] Found comment object')
- if SUBRESTRICT == [] or any(cobj.subreddit.display_name.lower() == sub.lower() for sub in SUBRESTRICT):
- print('\t[ ] Passed sub restriction')
- except:
- print('\t[ERR] Fetching comment object failed')
- failresult.append(ERRFETCH)
- else:
- print('\t[ERR] No permalink on first line')
- failresult.append(ERRNOPERMA)
- except AttributeError:
- print('\t[ERR] Author unavailable.')
- sql.commit()
- while True:
- try:
- scanPM()
- except Exception as e:
- print('ERROR: ' + str(e))
- print('Running again in ' + WAITS + ' seconds \n_________\n')
- sql.commit()
- time.sleep(WAIT)
Add Comment
Please, Sign In to add comment