docker-less support
This commit is contained in:
parent
1d36c3e17e
commit
8a97216d88
2
.gitignore
vendored
2
.gitignore
vendored
@ -213,3 +213,5 @@ fabric.properties
|
||||
|
||||
|
||||
/data/
|
||||
.token.txt
|
||||
*.exe
|
||||
|
@ -29,7 +29,7 @@
|
||||
</DockerPortBindingImpl>
|
||||
</list>
|
||||
</option>
|
||||
<option name="commandLineOptions" value="--cpus="3" --memory="4000mb" --network="v6d"" />
|
||||
<option name="commandLineOptions" value="--cpus="3" --memory="4000mb" --network="v6d" --ip="172.18.0.30"" />
|
||||
<option name="showCommandPreview" value="true" />
|
||||
<option name="volumeBindings">
|
||||
<list>
|
||||
|
24
.idea/runConfigurations/guerilla.xml
Normal file
24
.idea/runConfigurations/guerilla.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="guerilla" type="PythonConfigurationType" factoryName="Python">
|
||||
<module name="v6d3music" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="$PROJECT_DIR$" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="IS_MODULE_SDK" value="false" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
|
||||
<option name="SCRIPT_NAME" value="v6d3music.run-bot" />
|
||||
<option name="PARAMETERS" value="guerilla" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="true" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import aiohttp
|
||||
import discord
|
||||
@ -10,7 +11,7 @@ from v6d0auth.appfactory import AppFactory
|
||||
from v6d0auth.run_app import start_app
|
||||
from v6d1tokens.client import request_token
|
||||
|
||||
from v6d3music.config import myroot
|
||||
from v6d3music.config import auth_redirect, myroot
|
||||
from v6d3music.utils.bytes_hash import bytes_hash
|
||||
|
||||
session_db = Db(myroot / 'session.db', kvrequest_type=KVJson)
|
||||
@ -25,12 +26,17 @@ class MusicAppFactory(AppFactory):
|
||||
client: discord.Client
|
||||
):
|
||||
self.secret = secret
|
||||
self.redirect = 'https://music.parrrate.ru/auth/'
|
||||
self.discord_auth = 'https://discord.com/api/oauth2/authorize?client_id=914432576926646322' \
|
||||
f'&redirect_uri={urllib.parse.quote(self.redirect)}&response_type=code&scope=identify'
|
||||
self.redirect = auth_redirect
|
||||
self.loop = asyncio.get_running_loop()
|
||||
self.client = client
|
||||
|
||||
def auth_link(self):
|
||||
if self.client.user is None:
|
||||
return ''
|
||||
else:
|
||||
return f'https://discord.com/api/oauth2/authorize?client_id={self.client.user.id}' \
|
||||
f'&redirect_uri={urllib.parse.quote(self.redirect)}&response_type=code&scope=identify'
|
||||
|
||||
def _file(self, file: str):
|
||||
with open(self.htmlroot / file) as f:
|
||||
return f.read()
|
||||
@ -46,14 +52,14 @@ class MusicAppFactory(AppFactory):
|
||||
text = await self.file(f'{file}.html')
|
||||
text = text.replace(
|
||||
'$$DISCORD_AUTH$$',
|
||||
self.discord_auth
|
||||
self.auth_link()
|
||||
)
|
||||
return web.Response(
|
||||
text=text,
|
||||
content_type='text/html'
|
||||
)
|
||||
|
||||
async def code_token(self, code: str):
|
||||
async def code_token(self, code: str) -> dict:
|
||||
data = {
|
||||
'client_id': '914432576926646322',
|
||||
'client_secret': self.secret,
|
||||
@ -68,14 +74,13 @@ class MusicAppFactory(AppFactory):
|
||||
async with session.post('https://discord.com/api/oauth2/token', data=data, headers=headers) as response:
|
||||
return await response.json()
|
||||
|
||||
async def session_client(self, session: str):
|
||||
data = self.session_data(session)
|
||||
client_token = data.get('token')
|
||||
if client_token is None:
|
||||
return None
|
||||
access_token = client_token.get('access_token')
|
||||
if access_token is None:
|
||||
return None
|
||||
@classmethod
|
||||
async def session_client(cls, data: dict) -> Optional[dict]:
|
||||
match data:
|
||||
case {'token': {'access_token': str() as access_token}}:
|
||||
pass
|
||||
case _:
|
||||
return None
|
||||
headers = {
|
||||
'Authorization': f'Bearer {access_token}'
|
||||
}
|
||||
@ -84,7 +89,7 @@ class MusicAppFactory(AppFactory):
|
||||
return await response.json()
|
||||
|
||||
@classmethod
|
||||
def client_status(cls, sclient: dict):
|
||||
def client_status(cls, sclient: dict) -> dict:
|
||||
user = cls.client_user(sclient)
|
||||
return {
|
||||
'expires': sclient.get('expires'),
|
||||
@ -92,7 +97,7 @@ class MusicAppFactory(AppFactory):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def user_status(cls, user: dict):
|
||||
def user_status(cls, user: dict) -> dict:
|
||||
return {
|
||||
'avatar': cls.user_avatar_url(user),
|
||||
'id': cls.user_id(user),
|
||||
@ -100,37 +105,27 @@ class MusicAppFactory(AppFactory):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def user_username_full(cls, user: dict):
|
||||
username = cls.user_username(user)
|
||||
if username is None:
|
||||
return None
|
||||
discriminator = cls.user_discriminator(user)
|
||||
if discriminator is None:
|
||||
return None
|
||||
return f'{username}#{discriminator}'
|
||||
def user_username_full(cls, user: dict) -> Optional[str]:
|
||||
match user:
|
||||
case {'username': str() as username, 'discriminator': str() as discriminator}:
|
||||
return f'{username}#{discriminator}'
|
||||
case _:
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def user_username(cls, user: dict):
|
||||
return user.get('username')
|
||||
|
||||
@classmethod
|
||||
def user_discriminator(cls, user: dict):
|
||||
return user.get('discriminator')
|
||||
|
||||
@classmethod
|
||||
def client_user(cls, sclient: dict):
|
||||
def client_user(cls, sclient: dict) -> Optional[dict]:
|
||||
return sclient.get('user')
|
||||
|
||||
@classmethod
|
||||
def user_id(cls, user: dict):
|
||||
def user_id(cls, user: dict) -> Optional[str | int]:
|
||||
return user.get('id')
|
||||
|
||||
@classmethod
|
||||
def user_avatar(cls, user: dict):
|
||||
def user_avatar(cls, user: dict) -> Optional[str]:
|
||||
return user.get('avatar')
|
||||
|
||||
@classmethod
|
||||
def user_avatar_url(cls, user: dict):
|
||||
def user_avatar_url(cls, user: dict) -> Optional[str]:
|
||||
cid = cls.user_id(user)
|
||||
if cid is None:
|
||||
return None
|
||||
@ -139,15 +134,26 @@ class MusicAppFactory(AppFactory):
|
||||
return None
|
||||
return f'https://cdn.discordapp.com/avatars/{cid}/{avatar}.png'
|
||||
|
||||
async def session_status(self, session: str):
|
||||
async def session_status(self, session: str) -> dict:
|
||||
data = self.session_data(session)
|
||||
sclient = await self.session_client(session)
|
||||
sclient = await self.session_client(data)
|
||||
return {
|
||||
'code_set': data.get('code') is not None,
|
||||
'token_set': data.get('token') is not None,
|
||||
'client': (None if sclient is None else self.client_status(sclient))
|
||||
}
|
||||
|
||||
async def session_queue(self, session: str):
|
||||
data = self.session_data(session)
|
||||
sclient = await self.session_client(data)
|
||||
if sclient is None:
|
||||
return None
|
||||
user = self.client_user(sclient)
|
||||
if user is None:
|
||||
return None
|
||||
cid = self.user_id(user)
|
||||
return cid
|
||||
|
||||
@classmethod
|
||||
def session_data(cls, session: str) -> dict:
|
||||
data = session_db.get(session, {})
|
||||
@ -198,6 +204,13 @@ class MusicAppFactory(AppFactory):
|
||||
data=await self.session_status(session)
|
||||
)
|
||||
|
||||
@routes.get('/queue/')
|
||||
async def api_queue(request: web.Request) -> web.Response:
|
||||
session = str(request.query.get('session'))
|
||||
return web.json_response(
|
||||
data=await self.session_queue(session)
|
||||
)
|
||||
|
||||
@routes.get('/main.js')
|
||||
async def state(_request: web.Request) -> web.Response:
|
||||
return web.Response(
|
||||
@ -212,5 +225,9 @@ class MusicAppFactory(AppFactory):
|
||||
|
||||
@classmethod
|
||||
async def start(cls, client: discord.Client):
|
||||
factory = cls(await request_token('music-client', 'token'), client)
|
||||
await start_app(factory.app())
|
||||
try:
|
||||
factory = cls(await request_token('music-client', 'token'), client)
|
||||
except aiohttp.ClientConnectorError:
|
||||
print('no web app (likely due to no token)')
|
||||
else:
|
||||
await start_app(factory.app())
|
||||
|
@ -35,7 +35,7 @@ presets: {shlex.join(allowed_presets)}
|
||||
(), 'help'
|
||||
)
|
||||
async with lock_for(ctx.guild, 'not in a guild'):
|
||||
queue = await queue_for(ctx, create=True)
|
||||
queue = await queue_for(ctx, create=True, force_play=False)
|
||||
async for audio in yt_audios(ctx, args):
|
||||
queue.append(audio)
|
||||
await ctx.reply('done')
|
||||
@ -50,15 +50,15 @@ async def skip(ctx: Context, args: list[str]) -> None:
|
||||
)
|
||||
match args:
|
||||
case []:
|
||||
queue = await queue_for(ctx, create=False)
|
||||
queue = await queue_for(ctx, create=False, force_play=False)
|
||||
queue.skip_at(0, ctx.member)
|
||||
case [pos] if pos.isdecimal():
|
||||
pos = int(pos)
|
||||
queue = await queue_for(ctx, create=False)
|
||||
queue = await queue_for(ctx, create=False, force_play=False)
|
||||
queue.skip_at(pos, ctx.member)
|
||||
case [pos0, pos1] if pos0.isdecimal() and pos1.isdecimal():
|
||||
pos0, pos1 = int(pos0), int(pos1)
|
||||
queue = await queue_for(ctx, create=False)
|
||||
queue = await queue_for(ctx, create=False, force_play=False)
|
||||
for i in range(pos0, pos1 + 1):
|
||||
if not queue.skip_at(pos0, ctx.member):
|
||||
pos0 += 1
|
||||
@ -83,7 +83,7 @@ async def skip_to(ctx: Context, args: list[str]) -> None:
|
||||
seconds = int(s)
|
||||
case _:
|
||||
raise Explicit('misformatted')
|
||||
queue = await queue_for(ctx, create=False)
|
||||
queue = await queue_for(ctx, create=False, force_play=False)
|
||||
queue.queue[0].set_seconds(seconds)
|
||||
|
||||
|
||||
@ -103,7 +103,7 @@ async def effects_(ctx: Context, args: list[str]) -> None:
|
||||
case _:
|
||||
raise Explicit('misformatted')
|
||||
assert_admin(ctx.member)
|
||||
queue = await queue_for(ctx, create=False)
|
||||
queue = await queue_for(ctx, create=False, force_play=False)
|
||||
yta = queue.queue[0]
|
||||
seconds = yta.source_seconds()
|
||||
yta.options = options_for_effects(effects)
|
||||
@ -122,17 +122,19 @@ async def queue_(ctx: Context, args: list[str]) -> None:
|
||||
)
|
||||
match args:
|
||||
case []:
|
||||
await ctx.long((await (await queue_for(ctx, create=False)).format()).strip() or 'no queue')
|
||||
await ctx.long(
|
||||
(await (await queue_for(ctx, create=True, force_play=False)).format()).strip() or 'no queue'
|
||||
)
|
||||
case ['clear']:
|
||||
(await queue_for(ctx, create=False)).clear(ctx.member)
|
||||
(await queue_for(ctx, create=False, force_play=False)).clear(ctx.member)
|
||||
await ctx.reply('done')
|
||||
case ['resume']:
|
||||
async with lock_for(ctx.guild, 'not in a guild'):
|
||||
await queue_for(ctx, create=True)
|
||||
await queue_for(ctx, create=True, force_play=True)
|
||||
await ctx.reply('done')
|
||||
case ['pause']:
|
||||
async with lock_for(ctx.guild, 'not in a guild'):
|
||||
vc = await vc_for(ctx, create=True)
|
||||
vc = await vc_for(ctx, create=True, force_play=False)
|
||||
vc.pause()
|
||||
await ctx.reply('done')
|
||||
case _:
|
||||
@ -149,7 +151,7 @@ async def swap(ctx: Context, args: list[str]) -> None:
|
||||
match args:
|
||||
case [a, b] if a.isdecimal() and b.isdecimal():
|
||||
a, b = int(a), int(b)
|
||||
(await queue_for(ctx, create=False)).swap(ctx.member, a, b)
|
||||
(await queue_for(ctx, create=False, force_play=False)).swap(ctx.member, a, b)
|
||||
case _:
|
||||
raise Explicit('misformatted')
|
||||
|
||||
@ -164,7 +166,7 @@ async def move(ctx: Context, args: list[str]) -> None:
|
||||
match args:
|
||||
case [a, b] if a.isdecimal() and b.isdecimal():
|
||||
a, b = int(a), int(b)
|
||||
(await queue_for(ctx, create=False)).move(ctx.member, a, b)
|
||||
(await queue_for(ctx, create=False, force_play=False)).move(ctx.member, a, b)
|
||||
case _:
|
||||
raise Explicit('misformatted')
|
||||
|
||||
@ -179,20 +181,20 @@ async def volume_(ctx: Context, args: list[str]) -> None:
|
||||
match args:
|
||||
case [volume]:
|
||||
volume = float(volume)
|
||||
await (await main_for(ctx, create=False)).set(volume, ctx.member)
|
||||
await (await main_for(ctx, create=True, force_play=False)).set(volume, ctx.member)
|
||||
case _:
|
||||
raise Explicit('misformatted')
|
||||
|
||||
|
||||
@at('commands', 'pause')
|
||||
async def pause(ctx: Context, _args: list[str]) -> None:
|
||||
vc = await vc_for(ctx, create=False)
|
||||
vc = await vc_for(ctx, create=False, force_play=False)
|
||||
vc.pause()
|
||||
|
||||
|
||||
@at('commands', 'resume')
|
||||
async def resume(ctx: Context, _args: list[str]) -> None:
|
||||
vc = await vc_for(ctx, create=False)
|
||||
vc = await vc_for(ctx, create=False, force_play=True)
|
||||
vc.resume()
|
||||
|
||||
|
||||
|
@ -3,5 +3,6 @@ import os
|
||||
from v6d0auth.config import root
|
||||
|
||||
prefix = os.getenv('v6prefix', '?/')
|
||||
auth_redirect = os.getenv('v6redirect', 'https://music.parrrate.ru/auth/')
|
||||
myroot = root / 'v6d3music'
|
||||
myroot.mkdir(exist_ok=True)
|
||||
|
@ -1,5 +1,6 @@
|
||||
import shlex
|
||||
import subprocess
|
||||
import time
|
||||
from threading import Thread
|
||||
from typing import Optional
|
||||
|
||||
@ -37,6 +38,8 @@ class FFmpegNormalAudio(discord.FFmpegAudio):
|
||||
self._chunk: Optional[bytes] = None
|
||||
self._generating = False
|
||||
self._started = False
|
||||
self._loaded = False
|
||||
self.loaded_at: Optional[float] = None
|
||||
|
||||
def _raw_read(self):
|
||||
return self._stdout.read(discord.opus.Encoder.FRAME_SIZE)
|
||||
@ -67,6 +70,9 @@ class FFmpegNormalAudio(discord.FFmpegAudio):
|
||||
self._generate()
|
||||
return chunk
|
||||
|
||||
def droppable(self) -> bool:
|
||||
return self._loaded and time.time() - self.loaded_at < 600
|
||||
|
||||
def read(self):
|
||||
ret = self._raw_read()
|
||||
if len(ret) != discord.opus.Encoder.FRAME_SIZE:
|
||||
@ -74,6 +80,8 @@ class FFmpegNormalAudio(discord.FFmpegAudio):
|
||||
print('poll')
|
||||
return FILL
|
||||
return b''
|
||||
self.loaded_at = time.time()
|
||||
self._loaded = True
|
||||
return ret
|
||||
|
||||
def is_opus(self):
|
||||
|
@ -21,13 +21,15 @@ async def raw_vc_for(ctx: Context) -> discord.VoiceClient:
|
||||
try:
|
||||
vc: discord.VoiceProtocol = await vch.connect()
|
||||
except discord.ClientException:
|
||||
vc: discord.VoiceProtocol = ctx.guild.voice_client
|
||||
await ctx.guild.fetch_channels()
|
||||
await vc.disconnect(force=True)
|
||||
raise Explicit('try again later')
|
||||
assert isinstance(vc, discord.VoiceClient)
|
||||
return vc
|
||||
|
||||
|
||||
async def main_for_raw_vc(vc: discord.VoiceClient, *, create: bool) -> MainAudio:
|
||||
async def main_for_raw_vc(vc: discord.VoiceClient, *, create: bool, force_play: bool) -> MainAudio:
|
||||
if vc.guild in mainasrcs:
|
||||
source = mainasrcs[vc.guild]
|
||||
else:
|
||||
@ -37,26 +39,26 @@ async def main_for_raw_vc(vc: discord.VoiceClient, *, create: bool) -> MainAudio
|
||||
await MainAudio.create(vc.guild)
|
||||
)
|
||||
else:
|
||||
raise Explicit('not playing')
|
||||
if vc.source != source or create and not vc.is_playing():
|
||||
raise Explicit('not playing, use `queue pause` or `queue resume`')
|
||||
if vc.source != source or create and not vc.is_playing() and (force_play or not vc.is_paused()):
|
||||
vc.play(source)
|
||||
return source
|
||||
|
||||
|
||||
async def vc_main_for(ctx: Context, *, create: bool) -> tuple[discord.VoiceClient, MainAudio]:
|
||||
async def vc_main_for(ctx: Context, *, create: bool, force_play: bool) -> tuple[discord.VoiceClient, MainAudio]:
|
||||
vc = await raw_vc_for(ctx)
|
||||
return vc, await main_for_raw_vc(vc, create=create)
|
||||
return vc, await main_for_raw_vc(vc, create=create, force_play=force_play)
|
||||
|
||||
|
||||
async def vc_for(ctx: Context, *, create: bool) -> discord.VoiceClient:
|
||||
vc, source = await vc_main_for(ctx, create=create)
|
||||
async def vc_for(ctx: Context, *, create: bool, force_play: bool) -> discord.VoiceClient:
|
||||
vc, source = await vc_main_for(ctx, create=create, force_play=force_play)
|
||||
return vc
|
||||
|
||||
|
||||
async def main_for(ctx: Context, *, create: bool) -> MainAudio:
|
||||
vc, source = await vc_main_for(ctx, create=create)
|
||||
async def main_for(ctx: Context, *, create: bool, force_play: bool) -> MainAudio:
|
||||
vc, source = await vc_main_for(ctx, create=create, force_play=force_play)
|
||||
return source
|
||||
|
||||
|
||||
async def queue_for(ctx: Context, *, create: bool) -> QueueAudio:
|
||||
return (await main_for(ctx, create=create)).queue
|
||||
async def queue_for(ctx: Context, *, create: bool, force_play: bool) -> QueueAudio:
|
||||
return (await main_for(ctx, create=create, force_play=force_play)).queue
|
||||
|
@ -11,14 +11,23 @@ from v6d3music.utils.assert_admin import assert_admin
|
||||
from v6d3music.utils.fill import FILL
|
||||
|
||||
queue_db = Db(myroot / 'queue.db', kvrequest_type=KVJson)
|
||||
PRE_SET_LENGTH = 24
|
||||
|
||||
|
||||
class QueueAudio(discord.AudioSource):
|
||||
def __init__(self, guild: discord.Guild, respawned: list[YTAudio]):
|
||||
self.queue: deque[YTAudio] = deque()
|
||||
self.queue.extend(respawned)
|
||||
for audio in respawned:
|
||||
self.append(audio)
|
||||
self.guild = guild
|
||||
|
||||
def _update_sources(self):
|
||||
for i in range(PRE_SET_LENGTH):
|
||||
try:
|
||||
self.queue[i].set_source_if_necessary()
|
||||
except IndexError:
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
async def respawned(guild: discord.Guild) -> list[YTAudio]:
|
||||
respawned = []
|
||||
@ -45,15 +54,19 @@ class QueueAudio(discord.AudioSource):
|
||||
queue_db.set_nowait(self.guild.id, hybernated)
|
||||
|
||||
def append(self, audio: YTAudio):
|
||||
if len(self.queue) < PRE_SET_LENGTH:
|
||||
audio.set_source_if_necessary()
|
||||
self.queue.append(audio)
|
||||
|
||||
def read(self) -> bytes:
|
||||
if not self.queue:
|
||||
return FILL
|
||||
audio = self.queue[0]
|
||||
audio.set_source_if_necessary()
|
||||
frame = audio.read()
|
||||
if len(frame) != discord.opus.Encoder.FRAME_SIZE:
|
||||
self.queue.popleft().cleanup()
|
||||
self._update_sources()
|
||||
frame = FILL
|
||||
return frame
|
||||
|
||||
@ -72,6 +85,7 @@ class QueueAudio(discord.AudioSource):
|
||||
self.queue.remove(audio)
|
||||
audio.cleanup()
|
||||
return True
|
||||
self._update_sources()
|
||||
return False
|
||||
|
||||
def clear(self, member: discord.Member) -> None:
|
||||
@ -83,6 +97,7 @@ class QueueAudio(discord.AudioSource):
|
||||
if max(a, b) >= len(self.queue):
|
||||
return
|
||||
self.queue[a], self.queue[b] = self.queue[b], self.queue[a]
|
||||
self._update_sources()
|
||||
|
||||
def move(self, member: discord.Member, a: int, b: int) -> None:
|
||||
assert_admin(member)
|
||||
@ -91,6 +106,7 @@ class QueueAudio(discord.AudioSource):
|
||||
audio = self.queue[a]
|
||||
self.queue.remove(audio)
|
||||
self.queue.insert(b, audio)
|
||||
self._update_sources()
|
||||
|
||||
async def format(self) -> str:
|
||||
stream = StringIO()
|
||||
|
@ -14,7 +14,7 @@ from v6d3music.utils.sparq import sparq
|
||||
|
||||
|
||||
class YTAudio(discord.AudioSource):
|
||||
source: discord.FFmpegAudio
|
||||
source: FFmpegNormalAudio
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@ -24,7 +24,7 @@ class YTAudio(discord.AudioSource):
|
||||
options: Optional[str],
|
||||
rby: discord.Member,
|
||||
already_read: int,
|
||||
tor: bool
|
||||
tor: bool,
|
||||
):
|
||||
self.url = url
|
||||
self.origin = origin
|
||||
@ -33,12 +33,15 @@ class YTAudio(discord.AudioSource):
|
||||
self.rby = rby
|
||||
self.already_read = already_read
|
||||
self.tor = tor
|
||||
self.loaded = False
|
||||
self.regenerating = False
|
||||
self.set_source()
|
||||
# self.set_source()
|
||||
self._durations: dict[str, str] = {}
|
||||
self.loop = asyncio.get_running_loop()
|
||||
|
||||
def set_source_if_necessary(self):
|
||||
if not hasattr(self, 'source'):
|
||||
self.set_source()
|
||||
|
||||
def set_source(self):
|
||||
self.schedule_duration_update()
|
||||
self.source = FFmpegNormalAudio(
|
||||
@ -64,8 +67,11 @@ class YTAudio(discord.AudioSource):
|
||||
hours, minutes = divmod(minutes, 60)
|
||||
return f'{hours}:{minutes:02d}:{seconds:02d}'
|
||||
|
||||
def _schedule_duration_update(self):
|
||||
self.loop.create_task(self.update_duration())
|
||||
|
||||
def schedule_duration_update(self):
|
||||
asyncio.get_running_loop().create_task(self.update_duration())
|
||||
self.loop.call_soon_threadsafe(self._schedule_duration_update)
|
||||
|
||||
async def update_duration(self):
|
||||
url: str = self.url
|
||||
@ -114,9 +120,7 @@ class YTAudio(discord.AudioSource):
|
||||
return FILL
|
||||
self.already_read += 1
|
||||
ret: bytes = self.source.read()
|
||||
if ret:
|
||||
self.loaded = True
|
||||
elif not self.loaded:
|
||||
if not ret and not self.source.droppable():
|
||||
if random.random() > .1:
|
||||
self.regenerating = True
|
||||
self.loop.create_task(self.regenerate())
|
||||
@ -126,7 +130,8 @@ class YTAudio(discord.AudioSource):
|
||||
return ret
|
||||
|
||||
def cleanup(self):
|
||||
self.source.cleanup()
|
||||
if hasattr(self, 'source'):
|
||||
self.source.cleanup()
|
||||
|
||||
def can_be_skipped_by(self, member: discord.Member) -> bool:
|
||||
permissions: discord.Permissions = member.guild_permissions
|
||||
@ -170,7 +175,8 @@ class YTAudio(discord.AudioSource):
|
||||
try:
|
||||
print(f'regenerating {self.origin}')
|
||||
self.url = await real_url(self.origin, True, self.tor)
|
||||
self.source.cleanup()
|
||||
if hasattr(self, 'source'):
|
||||
self.source.cleanup()
|
||||
self.set_source()
|
||||
print(f'regenerated {self.origin}')
|
||||
finally:
|
||||
|
@ -81,7 +81,8 @@ const pageHome = async () => {
|
||||
'div',
|
||||
baseEl('div', aLogin()),
|
||||
baseEl('div', await userAvatarImg()),
|
||||
await userId()
|
||||
baseEl('div', await userId()),
|
||||
baseEl('div', await userUsername()),
|
||||
)
|
||||
};
|
||||
let authbase;
|
||||
|
@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
import discord
|
||||
@ -52,7 +53,7 @@ async def restore_vcs():
|
||||
vp: discord.VoiceProtocol = await channel.connect()
|
||||
assert isinstance(vp, discord.VoiceClient)
|
||||
vc = vp
|
||||
await main_for_raw_vc(vc, create=True)
|
||||
await main_for_raw_vc(vc, create=True, force_play=True)
|
||||
if vc_is_paused:
|
||||
vc.pause()
|
||||
except Exception as e:
|
||||
@ -78,7 +79,6 @@ async def on_ready():
|
||||
|
||||
@client.event
|
||||
async def on_message(message: discord.Message) -> None:
|
||||
print('on message')
|
||||
await handle_content(message, message.content, prefix)
|
||||
|
||||
|
||||
@ -126,11 +126,24 @@ async def setup_tasks():
|
||||
|
||||
async def main():
|
||||
async with volume_db, queue_db, cache_db, session_db:
|
||||
await client.login(await request_token('music', 'token'))
|
||||
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)
|
||||
loop.create_task(setup_tasks())
|
||||
if os.getenv('v6monitor'):
|
||||
loop.create_task(monitor())
|
||||
subprocess.Popen('tor')
|
||||
try:
|
||||
subprocess.Popen('tor')
|
||||
except FileNotFoundError:
|
||||
print('no tor')
|
||||
await client.connect()
|
||||
|
||||
|
||||
|
@ -8,7 +8,7 @@ def extract(params: dict, url: str, kwargs: dict):
|
||||
if 'entries' in extracted:
|
||||
extracted['entries'] = list(extracted['entries'])
|
||||
return extracted
|
||||
except (youtube_dl.utils.ExtractorError, youtube_dl.utils.DownloadError) as e:
|
||||
except Exception as e:
|
||||
msg = str(e)
|
||||
msg = discord.utils.escape_markdown(msg)
|
||||
msg = msg.replace('\x1b[0;31m', '__')
|
||||
|
Loading…
Reference in New Issue
Block a user