From d4dec84576dcec9f7f0e6c044e84a09dbedd87f6 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 26 Oct 2020 01:24:05 -0700 Subject: [PATCH] add everything thus far, reddit scaping, storing to db, basic retrieval --- requirements.txt | 3 +- supportbot/bot.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++- supportbot/db.py | 18 ++++++++++-- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2a69344..828133e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ praw peewee -pure-sasl pydle -timeloop +arrow diff --git a/supportbot/bot.py b/supportbot/bot.py index b8ac129..8c940b0 100644 --- a/supportbot/bot.py +++ b/supportbot/bot.py @@ -1,13 +1,46 @@ import sys, os import pydle +import asyncio +import arrow +from supportbot.reddit import Reddit +from supportbot.db import SupportRequest, IRCSupportOperator from supportbot import config -from supportbot import db class IRCBot(pydle.Client): + async def on_connect(self): for room in config.ROOMS: await self.join(room) + while True: + print("Checking reddit for new posts") + r = Reddit() + new_requests = [] + subreddit = r.reddit.subreddit(r.subreddit) + for submission in subreddit.new(): + if not SupportRequest.select().where(SupportRequest.post_id == submission.id): + s = SupportRequest( + post_id=submission.id, + author=submission.author, + title=submission.title, + permalink=submission.permalink, + timestamp=submission.created_utc + ) + s.save() + print(f"Added Reddit post {submission.id} as record #{s.id}") + new_requests.append(s) + if new_requests: + await self.message(room, f"Found {len(new_requests)} new Reddit posts in /r/monerosupport. Use `!l` or `!list` to see them.") + 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): if source == self.nickname: @@ -18,6 +51,42 @@ class IRCBot(pydle.Client): if self.nickname in message: await self.message(target, f"Sup. I'm not very helpful yet, but getting there.") + if message in ["!list"]: + s = [] + reqs = SupportRequest.select().where( + SupportRequest.solved==False + ).order_by(SupportRequest.timestamp.desc()) + for req in reqs: + s.append(f"{req.id} - {req.author} - {arrow.get(req.timestamp).humanize()}") + await self.message(target, ", ".join(s[0:9])) + + if message.startswith("!request ") or message in ["!request"]: + 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 req.assigned: + _a = "ASSIGNED" + else: + _a = "UNASSIGNED" + await self.message(target, f"{req.id} - {req.title} - https://reddit.com/{req.permalink} - {_a}") + else: + await self.message(target, "No record with that ID") + + if message in ["!help"]: + await self.message(target, "not ready") + async def is_admin(self, nickname): admin = False if nickname in config.ADMIN_NICKNAMES: diff --git a/supportbot/db.py b/supportbot/db.py index 1cb6e5b..92a112d 100644 --- a/supportbot/db.py +++ b/supportbot/db.py @@ -1,4 +1,4 @@ -import datetime +from datetime import datetime from peewee import * from supportbot import config @@ -9,7 +9,19 @@ class BaseModel(Model): class Meta: database = db +class IRCSupportOperator(BaseModel): + irc_nick = CharField() + is_a_regular = BooleanField() + is_support_admin = BooleanField() + class SupportRequest(BaseModel): - reddit_user = CharField() + post_id = CharField() + timestamp = DateTimeField(default=datetime.now) + author = CharField() + title = CharField() + permalink = CharField() solved = BooleanField(default=False) - # todo... + assigned = BooleanField(default=False) + assignee = ForeignKeyField(IRCSupportOperator, backref='assignee', null=True) + +db.create_tables([SupportRequest, IRCSupportOperator])