trial + _upgrade_abm

This commit is contained in:
AF 2022-11-13 04:33:05 +00:00
parent 9588493288
commit 1c825918d5
6 changed files with 91 additions and 9 deletions

1
.gitignore vendored
View File

@ -215,3 +215,4 @@ fabric.properties
/data/
.token.txt
*.exe
.trial_token.env

1
.trial_token_example.env Normal file
View File

@ -0,0 +1 @@
trial_token=paste here

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# music bot
## try for yourself
default prefix is `?/`
### set token
```sh
cp .trial_token_example.env .trial_token.env
vim .trial_token.env
```
### start or update
```sh
docker compose up -d --build
```

14
docker-compose.yml Normal file
View File

@ -0,0 +1,14 @@
services:
music-bot-trial:
build: .
volumes:
- "/v6data"
env_file:
- .trial_token.env
deploy:
resources:
limits:
cpus: '2'
memory: 2G
tty: true
stop_signal: SIGINT

View File

@ -144,6 +144,7 @@ async def default(ctx: Context, args: list[str]) -> None:
await ctx.reply(f'effects set to `{effects}`')
@at('commands', '//')
@at('commands', 'queue')
async def queue_(ctx: Context, args: list[str]) -> None:
await catch(

View File

@ -1,15 +1,11 @@
import time
import contextlib
import asyncio
import os
import sys
import traceback
import discord
from v6d1tokens.client import request_token
from v6d2ctx.handle_content import handle_content
from v6d2ctx.lock_for import lock_for
from v6d2ctx.serve import serve
from v6d2ctx.pain import ABlockMonitor
from v6d3music.app import MusicAppFactory, session_db
from v6d3music.commands import register_commands
from v6d3music.config import prefix
@ -19,6 +15,13 @@ from v6d3music.core.mainaudio import volume_db
from v6d3music.core.queueaudio import queue_db
from v6d3music.utils.entries_effects_for_args import effects_db
from rainbowadn.instrument import Instrumentation
from v6d1tokens.client import request_token
from v6d2ctx.handle_content import handle_content
from v6d2ctx.lock_for import lock_for
from v6d2ctx.pain import ABlockMonitor
from v6d2ctx.serve import serve
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
register_commands()
@ -34,6 +37,7 @@ class MusicClient(discord.Client):
except Exception:
traceback.print_exc()
client = MusicClient(
intents=discord.Intents(
members=True,
@ -61,7 +65,15 @@ async def restore_vcs():
async with lock_for(guild, 'not in a guild'):
channels = await guild.fetch_channels()
channel: discord.VoiceChannel
channel, = [ch for ch in (chc for chc in channels if isinstance(chc, discord.VoiceChannel)) if ch.id == vccid]
channel, = [
ch for ch in
(
chc for chc in channels
if
isinstance(chc, discord.VoiceChannel)
)
if ch.id == vccid
]
vp: discord.VoiceProtocol = await channel.connect()
assert isinstance(vp, discord.VoiceClient)
vc = vp
@ -148,9 +160,47 @@ async def setup_tasks():
loop.create_task(start_app())
class UpgradeABMInit(Instrumentation):
def __init__(self):
super().__init__(ABlockMonitor, '__init__')
def instrument(self, method, abm, *, threshold=0.0, delta=10.0, interval=0.0):
print('created upgraded')
method(abm, threshold=threshold, delta=delta, interval=interval)
abm.threshold = threshold
class UpgradeABMTask(Instrumentation):
def __init__(self):
super().__init__(ABlockMonitor, '_monitor')
async def instrument(self, _, abm):
print('started upgraded')
while True:
delta = abm.delta
t = time.time()
await asyncio.sleep(delta)
spent = time.time() - t
delay = spent - delta
if delay > abm.threshold:
abm.threshold = delay
print(
f'upgraded block monitor reached new peak delay {delay:.4f}')
interval = abm.interval
if interval > 0:
await asyncio.sleep(interval)
def _upgrade_abm() -> contextlib.ExitStack:
with contextlib.ExitStack() as es:
es.enter_context(UpgradeABMInit())
es.enter_context(UpgradeABMTask())
return es.pop_all()
raise RuntimeError
async def main():
async with volume_db, queue_db, cache_db, session_db, effects_db, ABlockMonitor():
async with volume_db, queue_db, cache_db, session_db, effects_db, ABlockMonitor(delta=0.5):
if 'guerilla' in sys.argv:
from pathlib import Path
tokenpath = Path('.token.txt')
@ -159,6 +209,8 @@ async def main():
else:
token = input('token:')
tokenpath.write_text(token)
elif (token_ := os.getenv('trial_token')):
token = token_
else:
token = await request_token('music', 'token')
await client.login(token)
@ -170,4 +222,5 @@ async def main():
if __name__ == '__main__':
serve(main(), client, loop)
with _upgrade_abm():
serve(main(), client, loop)