From 1c825918d5c2b00fe6a6fa11ecf6dd9c60efb8e7 Mon Sep 17 00:00:00 2001 From: timofey Date: Sun, 13 Nov 2022 04:33:05 +0000 Subject: [PATCH] trial + _upgrade_abm --- .gitignore | 1 + .trial_token_example.env | 1 + README.md | 12 +++++++ docker-compose.yml | 14 ++++++++ v6d3music/commands.py | 1 + v6d3music/run-bot.py | 71 +++++++++++++++++++++++++++++++++++----- 6 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 .trial_token_example.env create mode 100644 README.md create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index 958a114..21b5bc0 100644 --- a/.gitignore +++ b/.gitignore @@ -215,3 +215,4 @@ fabric.properties /data/ .token.txt *.exe +.trial_token.env diff --git a/.trial_token_example.env b/.trial_token_example.env new file mode 100644 index 0000000..847ce27 --- /dev/null +++ b/.trial_token_example.env @@ -0,0 +1 @@ +trial_token=paste here diff --git a/README.md b/README.md new file mode 100644 index 0000000..61208de --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a4c2b60 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/v6d3music/commands.py b/v6d3music/commands.py index c616bda..5924956 100644 --- a/v6d3music/commands.py +++ b/v6d3music/commands.py @@ -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( diff --git a/v6d3music/run-bot.py b/v6d3music/run-bot.py index a306afd..5b18ebd 100644 --- a/v6d3music/run-bot.py +++ b/v6d3music/run-bot.py @@ -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)