Skip to content

Commit

Permalink
Log sub message
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaserlang committed Feb 27, 2024
1 parent 54943eb commit d91c5d2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
16 changes: 16 additions & 0 deletions tbot/migrations/20240227_01_tVr5L.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
"""

from yoyo import step

__depends__ = {'20240226_03_eKhC6'}

steps = [
step('''
ALTER TABLE `twitch_sub_log`
CHANGE `plan_name` `message` varchar(2000) NULL DEFAULT NULL,
ADD COLUMN `user` VARCHAR(200) COMMENT '',
ADD COLUMN `gifter_user` VARCHAR(200) COMMENT '';
''')
]
64 changes: 57 additions & 7 deletions tbot/twitch_bot/modlog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio, websockets, random, json, aiohttp
from datetime import datetime
from datetime import datetime, timezone
from tbot import config, utils, logger

class Pubsub():
Expand Down Expand Up @@ -104,19 +104,69 @@ def created_by_user_id():

async def log_sub(self, data):
try:
user_id = data.get('user_id')
user = data.get('user_name', 'anonsubgift')
gifter_id = None
gifter_user = None
if data['is_gift']:
gifter_id = user_id
gifter_user = user
user_id = data['recipient_id']
user = data['recipient_user_name']

await self.db.execute('''
INSERT INTO twitch_sub_log (channel_id, created_at, user_id, plan_name, tier, gifter_id, is_gift, total) VALUES
(%s, %s, %s, %s, %s, %s, %s, %s)
INSERT INTO twitch_sub_log (
channel_id,
created_at,
user_id,
user,
message,
tier,
gifter_id,
gifter,
is_gift,
total
) VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
''', (
data['channel_id'],
datetime.utcnow(),
data.get('recipient_id', data.get('user_id')),
data['sub_plan_name'],
datetime.now(tz=timezone.utc),
user_id,
user,
data['sub_message'].get('message'),
data['sub_plan'],
data.get('user_id', data.get('recipient_id')),
gifter_id,
gifter_user,
data['is_gift'],
data.get('cumulative_months'),
))

message = data['sub_message'].get('message')
tiers = {
'1000': 'Tier 1 Sub',
'2000': 'Tier 2 Sub',
'3000': 'Tier 3 Sub',
'Prime': 'Prime Sub',
}

if not data['is_gift']:
if message:
message = f': {message}'
message = f'{user} subscribed with a {tiers.get(data["sub_plan"], "unknown")} ({data["cumulative_months"]}){message}'
else:
message = f'{gifter_user} gifted a {tiers.get(data["sub_plan"], "unknown")} to {user}'

await self.db.execute('''
INSERT INTO twitch_chatlog (type, created_at, channel_id, user, user_id, message) VALUES
(%s, %s, %s, %s, %s, %s)
''', (
2,
datetime.now(tz=timezone.utc),
data['channel_id'],
gifter_user or user,
gifter_id or user_id,
message,
))
except Exception as e:
logger.exception(e)

Expand Down

0 comments on commit d91c5d2

Please sign in to comment.