token env

This commit is contained in:
AF 2024-06-29 18:20:31 +00:00
parent 555cb3d082
commit a1c043a032
Signed by: alisa
SSH Key Fingerprint: SHA256:vNY4pdIZvO1FYJKHROkdHLtvyopizvZVAEwg9AF6h04
2 changed files with 13 additions and 17 deletions

View File

@ -1,18 +1,20 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
import os
import aiohttp import aiohttp
from aiohttp import web from aiohttp import web
from v6d0auth.appfactory import AppFactory from v6d0auth.appfactory import AppFactory
from v6d0auth.run_app import start_app from v6d0auth.run_app import start_app
from v6d1tokens.client import request_token
from v6d3music.api import Api from v6d3music.api import Api
from v6d3music.core.set_config import set_config from v6d3music.core.set_config import set_config
__all__ = ("AppContext",) __all__ = ("AppContext",)
token = os.getenv("DISCORD_APP_TOKEN", "unset")
class MusicAppFactory(AppFactory): class MusicAppFactory(AppFactory):
def __init__(self, secret: str, api: Api): def __init__(self, secret: str, api: Api):
@ -25,7 +27,8 @@ class MusicAppFactory(AppFactory):
@routes.post("/api/") @routes.post("/api/")
async def api(request: web.Request) -> web.Response: async def api(request: web.Request) -> web.Response:
async with aiohttp.ClientSession() as s, s.get( async with aiohttp.ClientSession() as s, s.get(
"http://sessionservice/userid/", params={"session": request.query.get("session")} "http://sessionservice/userid/",
params={"session": request.query.get("session")},
) as response: ) as response:
user_id: int | None = await response.json() user_id: int | None = await response.json()
if user_id is None: if user_id is None:
@ -46,7 +49,7 @@ class AppContext:
async def start(self) -> tuple[web.Application, asyncio.Task[None]] | None: async def start(self) -> tuple[web.Application, asyncio.Task[None]] | None:
try: try:
factory = MusicAppFactory(await request_token("music-client", "token"), self.api) factory = MusicAppFactory(token, self.api)
await set_config("secret", factory.secret) await set_config("secret", factory.secret)
await set_config("redirect", factory.redirect) await set_config("redirect", factory.redirect)
except aiohttp.ClientConnectorError: except aiohttp.ClientConnectorError:
@ -57,7 +60,9 @@ class AppContext:
return app, task return app, task
async def __aenter__(self) -> AppContext: async def __aenter__(self) -> AppContext:
self.__task: asyncio.Task[tuple[web.Application, asyncio.Task[None]] | None] = asyncio.create_task(self.start()) self.__task: asyncio.Task[tuple[web.Application, asyncio.Task[None]] | None] = (
asyncio.create_task(self.start())
)
return self return self
async def __aexit__(self, exc_type, exc_val, exc_tb): async def __aexit__(self, exc_type, exc_val, exc_tb):

View File

@ -9,7 +9,6 @@ import discord
from ptvp35 import DbConnection from ptvp35 import DbConnection
from rainbowadn.instrument import Instrumentation from rainbowadn.instrument import Instrumentation
from v6d1tokens.client import request_token
from v6d2ctx.handle_content import handle_content from v6d2ctx.handle_content import handle_content
from v6d2ctx.integration.event import Events from v6d2ctx.integration.event import Events
from v6d2ctx.integration.targets import Targets from v6d2ctx.integration.targets import Targets
@ -25,6 +24,7 @@ from v6d3music.core.set_config import set_config
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
token = os.getenv("DISCORD_TOKEN", "unset")
class MusicClient(discord.Client): class MusicClient(discord.Client):
@ -47,7 +47,9 @@ _client = MusicClient(
) )
banned_guilds = set(map(int, filter(bool, map(str.strip, os.getenv("banned_guilds", "").split(":"))))) banned_guilds = set(
map(int, filter(bool, map(str.strip, os.getenv("banned_guilds", "").split(":"))))
)
def guild_allowed(guild: discord.Guild | None) -> bool: def guild_allowed(guild: discord.Guild | None) -> bool:
@ -157,17 +159,6 @@ async def amain(client: discord.Client):
ABlockMonitor(delta=0.5), ABlockMonitor(delta=0.5),
): ):
register_handlers(client, mainservice) register_handlers(client, mainservice)
if "guerilla" in sys.argv:
from pathlib import Path
tokenpath = Path(".token.txt")
if tokenpath.exists():
token = tokenpath.read_text()
else:
token = input("token:")
tokenpath.write_text(token)
else:
token = await request_token("music", "token")
await client.login(token) await client.login(token)
await client.connect() await client.connect()
print("exited") print("exited")