refactoring and adding additional checks

main
lza_menace 4 years ago
parent 46e538dde1
commit 9bc7d462b2

@ -29,34 +29,25 @@ class IRCBot(pydle.Client):
timestamp=submission.created_utc timestamp=submission.created_utc
) )
s.save() s.save()
print(f"Added Reddit post {submission.id} as record #{s.id}") print(f"Added Reddit post {submission.id} as record {s.id}")
new_requests.append(s) new_requests.append(s)
# Only post the totals into IRC # Only post the totals into IRC
if new_requests: if new_requests:
await self.message(room, f"Found {len(new_requests)} new Reddit posts in /r/monerosupport. Use `!list` to see the list and `!request x` to see each one.") await self.message(room, f"Found {len(new_requests)} new Reddit posts in /r/monerosupport. Use `.list` to see the list and `.request x` to see each one.")
# Check stored posts to see if they're solved # Check stored posts to see if they're solved
posts = SupportRequest.select().where(SupportRequest.solved==False) posts = SupportRequest.select().where(SupportRequest.solved==False)
for post in posts: for post in posts:
p = r.reddit.submission(post.post_id) p = r.reddit.submission(post.post_id)
if p.link_flair_text: if p.link_flair_text:
print(f"Marking post #{post.id} ({post.post_id}) as solved") print(f"Marking post {post.id} ({post.post_id}) as solved")
post.solved = True post.solved = True
post.save() post.save()
await asyncio.sleep(3) await asyncio.sleep(3)
await asyncio.sleep(30) await asyncio.sleep(30)
# solved = BooleanField(default=False)
# assigned = BooleanField(default=False)
# assignee = ForeignKey(IRCSupportOperator, backref='assignee')
#
# class IRCSupportOperator(BaseModel):
# irc_nick = CharField()
# is_a_regular = BooleanField()
# is_support_admin = BooleanField()
async def on_message(self, target, source, message): async def on_message(self, target, source, message):
if source == self.nickname: if source == self.nickname:
return return
@ -66,16 +57,18 @@ class IRCBot(pydle.Client):
if self.nickname in message: if self.nickname in message:
await self.message(target, f"Sup. I'm not very helpful yet, but getting there.") await self.message(target, f"Sup. I'm not very helpful yet, but getting there.")
if message in ["!list"]: # list command to show series of unsolved/active support requests
if message in [".list"]:
s = [] s = []
reqs = SupportRequest.select().where( reqs = SupportRequest.select().where(
SupportRequest.solved==False SupportRequest.solved==False
).order_by(SupportRequest.timestamp) ).order_by(SupportRequest.timestamp)
for req in reqs: for req in reqs:
s.append(f"#{req.id}") s.append(f"{req.id}")
await self.message(target, ", ".join(s)) await self.message(target, ", ".join(s))
if message.startswith("!request ") or message in ["!request"]: # request command to view meta about a specific support request
if message.startswith(".request ") or message in [".request"]:
msg = message.split() msg = message.split()
if not len(msg) > 1: if not len(msg) > 1:
await self.message(target, "Invalid arguments") await self.message(target, "Invalid arguments")
@ -92,19 +85,63 @@ class IRCBot(pydle.Client):
).first() ).first()
if req: if req:
if req.assigned: if req.assigned:
_a = "ASSIGNED" _a = f"ASSIGNED ({req.assignee.irc_nick})"
else: else:
_a = "UNASSIGNED" _a = "UNASSIGNED"
if req.solved: if req.solved:
_s = "SOLVED" _s = "SOLVED"
else: else:
_s = "UNSOLVED" _s = "UNSOLVED"
await self.message(target, f"#{req.id} - {req.title} - {arrow.get(req.timestamp).humanize()} - https://reddit.com{req.permalink} - {_a} - {_s}") await self.message(target, f"{req.id}: {req.title} - {arrow.get(req.timestamp).humanize()} - https://reddit.com{req.permalink} - {_a} - {_s}")
else: else:
await self.message(target, "No record with that ID") await self.message(target, "No record with that ID")
if message in ["!help"]: # claim support ticket - you become the assignee/helpers
await self.message(target, "not ready") if message.startswith(".claim ") or message in [".claim"]:
msg = message.split()
if not len(msg) > 1:
await self.message(target, "Invalid arguments")
return
try:
post_id = int(msg[1])
except:
await self.message(target, "Invalid arguments")
return
req = SupportRequest.select().where(
SupportRequest.id==post_id
).first()
if req:
if not await self.is_registered(source):
i = IRCSupportOperator(irc_nick=source)
i.save()
else:
i = IRCSupportOperator.select().where(IRCSupportOperator.irc_nick==source)
req.assigned = True
req.assignee = i
req.save()
await self.message(target, f"Support request {req.id} claimed by {source}")
else:
await self.message(target, "No record with that ID")
# queue command only shows things assigned to you
if message in [".queue"]:
req = SupportRequest.select().where(
IRCSupportOperator.irc_nick==source,
IRCSupportOperator.solved==False
).first()
if req:
reqs = []
for i in req:
reqs.append(i)
await self.message(target, ", ".join(reqs))
else:
await self.message(target, "No support requests assigned to you")
# help command shows available commands
if message in [".help"]:
await self.message(target, "`.list`: Show open support tickets on Reddit and synchronized to the database. | `.request x`: Show metadata of support request \"x\". | `.claim x`: Claim support request \"x\" | `.queue`: Show support requests assigned to you")
async def is_admin(self, nickname): async def is_admin(self, nickname):
admin = False admin = False
@ -114,6 +151,15 @@ class IRCBot(pydle.Client):
print("info: ", info) print("info: ", info)
return admin return admin
async def is_registered(self, nickname):
irc_nick = IRCSupportOperator.select().where(
IRCSupportOperator.irc_nick==nickname
).first()
if irc_nick:
return True
else:
return False
def run_bot(): def run_bot():
try: try:
print(f"[+] Starting IRC bot connecting to {config.IRC_HOST}...\n") print(f"[+] Starting IRC bot connecting to {config.IRC_HOST}...\n")

@ -11,8 +11,8 @@ class BaseModel(Model):
class IRCSupportOperator(BaseModel): class IRCSupportOperator(BaseModel):
irc_nick = CharField() irc_nick = CharField()
is_a_regular = BooleanField() is_a_regular = BooleanField(default=False)
is_support_admin = BooleanField() is_support_admin = BooleanField(default=False)
class SupportRequest(BaseModel): class SupportRequest(BaseModel):
post_id = CharField() post_id = CharField()

Loading…
Cancel
Save