Added fix for quickly deleted comments

This commit is contained in:
Collin R
2020-02-11 09:08:03 -08:00
parent 582aebaf0f
commit 3f9aa9b677
2 changed files with 59 additions and 15 deletions

View File

@@ -38,7 +38,7 @@ def run():
is_mod = bool(subreddit.moderator(redditor=reddit.user.me())) is_mod = bool(subreddit.moderator(redditor=reddit.user.me()))
print_level(1, f'Is mod? {is_mod}') print_level(1, f'Is mod? {is_mod}')
monitor_comments(subreddit, db) monitor_comments(subreddit, db, levels)
# Ignoring other potential exceptions for now, since we may not be able # Ignoring other potential exceptions for now, since we may not be able
# to recover from them as well as from this one # to recover from them as well as from this one
except prawcore.exceptions.RequestException as e: except prawcore.exceptions.RequestException as e:
@@ -47,9 +47,8 @@ def run():
print('Lost connection to Reddit; attempting to reconnect....') print('Lost connection to Reddit; attempting to reconnect....')
def monitor_comments(subreddit, db): def monitor_comments(subreddit, db, levels):
"""Monitor new comments in the subreddit, looking for confirmed solutions. """Monitor new comments in the subreddit, looking for confirmed solutions."""
"""
# Passing pause_after=0 will bypass the internal exponential delay, but have # Passing pause_after=0 will bypass the internal exponential delay, but have
# to check if any comments are returned with each query # to check if any comments are returned with each query
for comm in subreddit.stream.comments(skip_existing=True, pause_after=0): for comm in subreddit.stream.comments(skip_existing=True, pause_after=0):
@@ -83,8 +82,16 @@ def monitor_comments(subreddit, db):
# Reply to the comment marking the submission as solved # Reply to the comment marking the submission as solved
reply_body = reply.make(solver, points, level_info) reply_body = reply.make(solver, points, level_info)
comm.reply(reply_body) try:
print_level(1, f'Replied to comment with: "{reply_body}"') comm.reply(reply_body)
print_level(1, f'Replied to comment with: "{reply_body}"')
except praw.exceptions.APIException as e:
print_level(1, 'Unable to reply to comment')
print_level(2, f'{e}')
db.remove_point(solver)
print_level(1, f'Removed point awarded to {solver.name}')
print_level(1, 'Skipping comment')
continue
# Check if (non-mod) user flair should be updated to new level # Check if (non-mod) user flair should be updated to new level
lvl = level_info.current lvl = level_info.current
@@ -105,9 +112,9 @@ def monitor_comments(subreddit, db):
def marks_as_solved(comment): def marks_as_solved(comment):
'''Return True if the comment meets the criteria for marking the submission """Return True if the comment meets the criteria for marking the submission
as solved, False otherwise. as solved, False otherwise.
''' """
op_resp_to_solver = (not comment.is_root op_resp_to_solver = (not comment.is_root
and comment.is_submitter and comment.is_submitter
and not comment.parent().is_submitter and not comment.parent().is_submitter
@@ -126,7 +133,7 @@ def is_mod_comment(comment):
def is_first_solution(solved_comment): def is_first_solution(solved_comment):
'''Return True if this solved comment is the first, False otherwise.''' """Return True if this solved comment is the first, False otherwise."""
# Retrieve any comments hidden by "more comments" by passing limit=0 # Retrieve any comments hidden by "more comments" by passing limit=0
submission = solved_comment.submission submission = solved_comment.submission
submission.comments.replace_more(limit=0) submission.comments.replace_more(limit=0)
@@ -143,6 +150,7 @@ def is_first_solution(solved_comment):
def find_solver(solved_comment): def find_solver(solved_comment):
"""Determine the redditor responsible for solving the question."""
return solved_comment.parent().author return solved_comment.parent().author

View File

@@ -40,7 +40,7 @@ class Database:
name TEXT UNIQUE NOT NULL, name TEXT UNIQUE NOT NULL,
points INTEGER DEFAULT 0 points INTEGER DEFAULT 0
) )
''' '''
def __init__(self, dbpath): def __init__(self, dbpath):
self.path = dbpath self.path = dbpath
@@ -59,19 +59,55 @@ class Database:
insert_stmt = ''' insert_stmt = '''
INSERT OR IGNORE INTO redditor_points (id, name) INSERT OR IGNORE INTO redditor_points (id, name)
VALUES (:id, :name) VALUES (:id, :name)
''' '''
self.cursor.execute(insert_stmt, {'id': redditor.id, 'name': redditor.name}) self.cursor.execute(insert_stmt, {'id': redditor.id, 'name': redditor.name})
return self.cursor.rowcount return self.cursor.rowcount
@transaction # @transaction
# def add_point(self, redditor):
# points = self.get_points(redditor, add_if_none=True)
# params = {'id': redditor.id, 'name': redditor.name, 'points': points + 1}
# update_stmt = '''
# UPDATE redditor_points
# SET points = :points
# WHERE id = :id AND name = :name
# '''
# self.cursor.execute(update_stmt, params)
# return self.cursor.rowcount
# @transaction
# def remove_point(self, redditor):
# points = self.get_points(redditor, add_if_none=True)
# params = {'id': redditor.id, 'name': redditor.name, 'points': points - 1}
# update_stmt = '''
# UPDATE redditor_points
# SET points = :points
# WHERE id = :id AND name = :name
# '''
# self.cursor.execute(update_stmt, params)
# return self.cursor.rowcount
def add_point(self, redditor): def add_point(self, redditor):
return self._update_points(redditor, 1)
def remove_point(self, redditor):
return self._update_points(redditor, -1)
@transaction
def _update_points(self, redditor, points_modifier):
"""points_modifier is positive to add points, negative to subtract."""
points = self.get_points(redditor, add_if_none=True) points = self.get_points(redditor, add_if_none=True)
params = {'id': redditor.id, 'name': redditor.name, 'points': points + 1} # params = {'id': redditor.id, 'name': redditor.name, 'points': points - 1}
params = {
'id': redditor.id,
'name': redditor.name,
'points': points + points_modifier,
}
update_stmt = ''' update_stmt = '''
UPDATE redditor_points UPDATE redditor_points
SET points = :points SET points = :points
WHERE id = :id AND name = :name WHERE id = :id AND name = :name
''' '''
self.cursor.execute(update_stmt, params) self.cursor.execute(update_stmt, params)
return self.cursor.rowcount return self.cursor.rowcount
@@ -82,7 +118,7 @@ class Database:
SELECT points SELECT points
FROM redditor_points FROM redditor_points
WHERE id = :id AND name = :name WHERE id = :id AND name = :name
''' '''
self.cursor.execute(select_stmt, params) self.cursor.execute(select_stmt, params)
row = self.cursor.fetchone() # TODO check if more than one row row = self.cursor.fetchone() # TODO check if more than one row
if row: if row: