mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-06 15:15:37 +01:00
Add twitch
This commit is contained in:
@@ -19,5 +19,7 @@ class Config(BaseSettings):
|
||||
TWITCH_CLIENT_SECRET: str
|
||||
TWITCH_CHANNEL_ID: str
|
||||
|
||||
SECRETS_FILE_PATH: str
|
||||
|
||||
|
||||
config = Config() # type: ignore
|
||||
|
||||
10
src/main.py
10
src/main.py
@@ -1,5 +1,11 @@
|
||||
from services.discord import start_discord_sevice
|
||||
from asyncio import gather
|
||||
|
||||
from services.discord import start_discord_sevice
|
||||
from services.twitch import start_twitch_service
|
||||
|
||||
async def main():
|
||||
await start_discord_sevice()
|
||||
print("Starting services...")
|
||||
await gather(
|
||||
start_discord_sevice(),
|
||||
start_twitch_service()
|
||||
)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
from asyncio import Lock
|
||||
import json
|
||||
|
||||
from twitchAPI.twitch import Twitch
|
||||
from twitchAPI.type import AuthScope
|
||||
|
||||
import aiofiles
|
||||
|
||||
from config import config
|
||||
|
||||
|
||||
class State:
|
||||
pass
|
||||
|
||||
|
||||
class TokenStorage:
|
||||
lock = Lock()
|
||||
|
||||
@staticmethod
|
||||
async def save(acceess_token: str, refresh_token: str):
|
||||
data = json.dumps({"access_token": acceess_token, "refresh_token": refresh_token})
|
||||
|
||||
async with TokenStorage.lock:
|
||||
async with aiofiles.open(config.SECRETS_FILE_PATH, "w") as f:
|
||||
await f.write(data)
|
||||
|
||||
@staticmethod
|
||||
async def get() -> tuple[str, str]:
|
||||
async with TokenStorage.lock:
|
||||
async with aiofiles.open(config.SECRETS_FILE_PATH, "r") as f:
|
||||
data_str = await f.read()
|
||||
|
||||
data = json.loads(data_str)
|
||||
return data["access_token"], data["refresh_token"]
|
||||
|
||||
|
||||
class TwitchService:
|
||||
SCOPES = [AuthScope.CHANNEL_BOT]
|
||||
|
||||
def __init__(self, twitch: Twitch):
|
||||
self.twitch = twitch
|
||||
|
||||
self.state: State | None = None
|
||||
|
||||
@classmethod
|
||||
async def authorize(cls):
|
||||
twitch = Twitch(
|
||||
config.TWITCH_CLIENT_ID,
|
||||
config.TWITCH_CLIENT_SECRET
|
||||
)
|
||||
|
||||
twitch.user_auth_refresh_callback = TokenStorage.save
|
||||
|
||||
token, refresh_token = await TokenStorage.get()
|
||||
await twitch.set_user_authentication(token, cls.SCOPES, refresh_token)
|
||||
|
||||
return twitch
|
||||
|
||||
@classmethod
|
||||
async def run(cls):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
async def start(cls):
|
||||
twith = await cls.authorize()
|
||||
|
||||
await cls(twith).run()
|
||||
|
||||
|
||||
async def start_twitch_service():
|
||||
await TwitchService.start()
|
||||
|
||||
Reference in New Issue
Block a user