Skip to content

Commit

Permalink
Added support for timers for youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaserlang committed Oct 29, 2024
1 parent b34634e commit 07436ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
49 changes: 31 additions & 18 deletions tbot/twitch_bot/tasks/timers.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
import asyncio, random
from tbot.twitch_bot.bot_base import bot
from tbot import utils, config
import asyncio
import random
from datetime import datetime, timedelta

from tbot import config, utils
from tbot.twitch_bot import var_filler
from tbot.twitch_bot.bot_base import bot

_started = False


@bot.on('AFTER_CONNECTED')
async def connected(**kwargs):
global _started
if not _started:
_started = True
bot.loop.create_task(runner())


async def runner():
while True:
await asyncio.sleep(config.data.twitch.check_timers_every)
bot.loop.create_task(check_timers())


async def check_timers():
timers = await bot.db.fetchall(
'SELECT *, c.name as channel_name FROM twitch_timers t, twitch_channels c WHERE t.enabled=1 AND t.next_run<=%s AND t.channel_id=c.channel_id',
(datetime.utcnow(),)
(datetime.utcnow(),),
)
for t in timers:
bot.loop.create_task(handle_timer(t))


async def handle_timer(t):
await bot.db.execute(
'UPDATE twitch_timers SET next_run=%s WHERE id=%s',(
datetime.utcnow()+timedelta(minutes=t['interval']),
t['id']
))
if t['enabled_status'] > 0 and \
t['enabled_status'] != get_enabled_status(t['channel_id']):
'UPDATE twitch_timers SET next_run=%s WHERE id=%s',
(datetime.utcnow() + timedelta(minutes=t['interval']), t['id']),
)
if t['enabled_status'] > 0 and t['enabled_status'] != get_enabled_status(
t['channel_id']
):
return
messages = utils.json_loads(t['messages'])

Expand All @@ -51,10 +57,8 @@ async def handle_timer(t):
pos = 1

await bot.db.execute(
'UPDATE twitch_timers SET last_sent_message=%s WHERE id=%s',(
pos,
t['id']
))
'UPDATE twitch_timers SET last_sent_message=%s WHERE id=%s', (pos, t['id'])
)
try:
data = {
'bot': bot,
Expand All @@ -68,14 +72,23 @@ async def handle_timer(t):
'emotes': '',
'cmd': '',
}
msg = await var_filler.fill_message(messages[pos-1], **data)
bot.send("PRIVMSG", target='#'+t['channel_name'], message=msg)
msg = await var_filler.fill_message(messages[pos - 1], **data)
bot.send('PRIVMSG', target='#' + t['channel_name'], message=msg)
if bot.channels_check[t['channel_id']]['youtube_live_chat_id']:
from .youtube_chat import send_youtube_chat
await send_youtube_chat(
config.data.youtube.twitch_bot_channel_id or t['channel_id'],
bot.channels_check[t['channel_id']]['youtube_live_chat_id'],
msg,
)

except var_filler.Send_error as e:
bot.send("PRIVMSG", target='#'+t['channel_name'], message=str(e))
bot.send('PRIVMSG', target='#' + t['channel_name'], message=str(e))
except var_filler.Send_break:
pass


def get_enabled_status(channel_id):
if bot.channels_check[channel_id]['is_streaming']:
return 1
return 2
return 2
4 changes: 2 additions & 2 deletions tbot/twitch_bot/tasks/youtube_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ async def parse_chatmessages(channel_id: str, live_chat_id: str, chat: dict):
)


async def send_youtube_chat(channel_id: str, live_chat_id: str, message: str):
async def send_youtube_chat(sender_channel_id: str, live_chat_id: str, message: str):
await youtube_request(
channel_id,
sender_channel_id,
url='https://www.googleapis.com/youtube/v3/liveChat/messages',
method='POST',
params={
Expand Down

0 comments on commit 07436ec

Please sign in to comment.