From 50373bd9c7295d9c680880248d2b613338d0242d Mon Sep 17 00:00:00 2001
From: dnns01 <git@dnns01.de>
Date: Wed, 22 Dec 2021 10:58:14 +0100
Subject: [PATCH] Log whispers for Haugebot

---
 haugebot_twitch/haugebot.py             |  2 ++
 haugebot_twitch/whispers.py             | 24 ++++++++++++++++++++++++
 haugebot_web/migrations/0007_whisper.py | 24 ++++++++++++++++++++++++
 haugebot_web/models.py                  |  7 +++++++
 4 files changed, 57 insertions(+)
 create mode 100644 haugebot_twitch/whispers.py
 create mode 100644 haugebot_web/migrations/0007_whisper.py

diff --git a/haugebot_twitch/haugebot.py b/haugebot_twitch/haugebot.py
index ce9d161..a13dd5e 100644
--- a/haugebot_twitch/haugebot.py
+++ b/haugebot_twitch/haugebot.py
@@ -8,6 +8,7 @@ from twitchio.ext.commands import Context, Bot
 from vote_cog import VoteCog
 from wusstest_du_schon import WusstestDuSchon
 from wordcloud import Wordcloud
+from whispers import Whispers
 
 
 class HaugeBot(Bot, ABC):
@@ -24,6 +25,7 @@ class HaugeBot(Bot, ABC):
         self.add_cog(VoteCog(self))
         self.add_cog(WusstestDuSchon(self))
         self.add_cog(Wordcloud(self))
+        self.add_cog(Whispers(self))
 
     @staticmethod
     async def send_me(ctx, content):
diff --git a/haugebot_twitch/whispers.py b/haugebot_twitch/whispers.py
new file mode 100644
index 0000000..a7f1bbe
--- /dev/null
+++ b/haugebot_twitch/whispers.py
@@ -0,0 +1,24 @@
+import sqlite3
+from datetime import datetime
+
+from twitchio.ext import commands
+
+
+class Whispers(commands.Cog):
+    def __init__(self, bot):
+        self.bot = bot
+
+    @commands.Cog.event()
+    async def event_message(self, message):
+        # make sure the bot ignores itself and nightbot
+        if not message.author or message.author.name.lower() in [self.bot.NICK.lower(),
+                                                                 'nightbot'] or message.channel is not None:
+            return
+
+        conn = sqlite3.connect("db.sqlite3")
+        c = conn.cursor()
+        c.execute(
+            "INSERT INTO haugebot_web_whisper(author, content, received_at) VALUES (?, ?, ?)",
+            (message.author.name, message.content, datetime.now()))
+        conn.commit()
+        conn.close()
diff --git a/haugebot_web/migrations/0007_whisper.py b/haugebot_web/migrations/0007_whisper.py
new file mode 100644
index 0000000..b88acf1
--- /dev/null
+++ b/haugebot_web/migrations/0007_whisper.py
@@ -0,0 +1,24 @@
+# Generated by Django 3.2.10 on 2021-12-22 09:49
+
+import datetime
+from django.db import migrations, models
+import django
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('haugebot_web', '0006_auto_20211220_0048'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Whisper',
+            fields=[
+                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('author', models.TextField(max_length=50)),
+                ('content', models.TextField(max_length=500)),
+                ('received_at', models.DateTimeField(default=django.utils.timezone.now)),
+            ],
+        ),
+    ]
diff --git a/haugebot_web/models.py b/haugebot_web/models.py
index ec339d7..ec18009 100644
--- a/haugebot_web/models.py
+++ b/haugebot_web/models.py
@@ -1,6 +1,7 @@
 import os
 
 from django.db import models
+from django.utils import timezone
 
 from haugebot_web import twitch_api
 from .managers import TwitchUserManager
@@ -62,3 +63,9 @@ class TwitchUser(models.Model):
             return twitch_api.is_mod(self, broadcaster)
         except TwitchUser.DoesNotExist:
             return False
+
+
+class Whisper(models.Model):
+    author = models.TextField(max_length=50)
+    content = models.TextField(max_length=500)
+    received_at = models.DateTimeField(default=timezone.now)
-- 
GitLab