· 2 years ago · Feb 19, 2023, 10:30 AM
1import firebase_admin, random, string, asyncio, os, socket, sys, http, interactions, datetime
2
3from firebase_admin import credentials, db
4from dateutil import parser as du_parser
5from datetime import datetime
6from dotenv import load_dotenv
7from twitchAPI.twitch import Twitch
8from twitchAPI.oauth import UserAuthenticator
9from twitchAPI.types import AuthScope
10from twitchAPI.oauth import refresh_access_token
11from twitchAPI.twitch import Twitch
12from twitchAPI.helper import first
13from twitchAPI.eventsub import EventSub
14
15
16load_dotenv()
17CLIENT_ID = os.getenv('CLIENT_ID')
18CLIENT_SECRET = os.getenv('CLIENT_SECRET')
19EVENTSUB_URL = "https://twitch.lawmixerscpf.tk"
20OAUTH_TOKEN = os.getenv('OAUTH_TOKEN')
21OAUTH_REFRESH_TOKEN = os.getenv('OAUTH_REFRESH_TOKEN')
22
23bot = interactions.Client(
24token=os.environ["DISCORD_TOKEN"],
25intents= interactions.Intents.ALL | interactions.Intents.GUILD_MESSAGE_CONTENT)
26
27bot.load("events.event")
28
29cred = credentials.Certificate("#")
30
31default_app = firebase_admin.initialize_app(cred, {
32 'databaseURL': "#"
33})
34
35
36async def stream_online(data: dict):
37 streamerName = data["event"]["broadcaster_user_login"]
38 started_at = data["event"]["started_at"]
39 timestamp = du_parser.isoparse(started_at).timestamp()
40
41 streamerId = data["event"]["broadcaster_user_id"]
42
43
44 channel_id = db.reference(f"/streamers/{streamerId}/channel_id").get()
45 guild_id = db.reference(f"/streamers/{streamerId}/guild_id").get()
46
47 if channel_id != None and guild_id != None:
48 channel = await interactions.get(bot, interactions.Channel, object_id=channel_id)
49
50 if data["subscription"]["status"] == "enabled" and data["subscription"]["type"] == "stream.online":
51 await channel.send(f"{streamerName} is live! @everyone")
52
53async def main():
54 twitch = await Twitch(CLIENT_ID, CLIENT_SECRET)
55
56 new_token, new_refresh_token = await refresh_access_token(OAUTH_REFRESH_TOKEN, CLIENT_ID, CLIENT_SECRET)
57
58 await twitch.set_user_authentication(new_token, [], new_refresh_token, validate=True)
59
60 event_sub = EventSub(EVENTSUB_URL, CLIENT_ID, 8080, twitch)
61
62 await event_sub.unsubscribe_all()
63
64 event_sub.start()
65
66 for streamer_id in db.reference("/streamers/").get():
67 await event_sub.listen_stream_online(str(streamer_id), stream_online)
68
69
70
71loop = asyncio.get_event_loop()
72
73task1 = loop.create_task(main())
74task2 = loop.create_task(bot._ready())
75
76gathered = asyncio.gather(task1, task2)
77loop.run_until_complete(gathered)