From 53c87100039144c2fdfdcfd2b31b32156ce20b74 Mon Sep 17 00:00:00 2001 From: timofey Date: Sun, 25 Dec 2022 05:58:24 +0000 Subject: [PATCH] constitution spam --- Dockerfile | 1 + constitution | 2 +- v6d3losyash/run-bot.py | 66 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index b7e4ebf..9207662 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,5 @@ RUN pip install -r base.requirements.txt COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY v6d3losyash v6d3losyash +COPY constitution constitution CMD ["python3", "-m", "v6d3losyash.run-bot"] diff --git a/constitution b/constitution index 74dcbf9..e09f773 160000 --- a/constitution +++ b/constitution @@ -1 +1 @@ -Subproject commit 74dcbf9fb6f0abb13eb4f7827f6a38d6bfd4d8ec +Subproject commit e09f773881751bde0b1daba4d626d681026c9c55 diff --git a/v6d3losyash/run-bot.py b/v6d3losyash/run-bot.py index 3d95c54..629ab41 100644 --- a/v6d3losyash/run-bot.py +++ b/v6d3losyash/run-bot.py @@ -1,12 +1,14 @@ import asyncio +import re from io import StringIO -from typing import Union, Optional +from pathlib import Path +from typing import Optional, Union import discord -from ptvp35 import KVJson, Db + +from ptvp35 import Db, KVJson from v6d1tokens.client import request_token from v6d2ctx.serve import serve - from v6d3losyash import config loop = asyncio.new_event_loop() @@ -36,7 +38,63 @@ def escape(s: str): return res.getvalue() +class Spam: + def __init__(self) -> None: + self.spammed = False + self.root = Path(__file__).parent / '../constitution' + + def format_segment(self, segment: str) -> str: + segment = re.sub( + r'\[[\d.xX]*?\]', + lambda m: f'\u001b[0;1;36m{m[0]}\u001b[0m', + segment, + ) + segment = re.sub( + r'\#[\w-]*', + lambda m: f'\u001b[0;34m{m[0]}\u001b[0m', + segment, + ) + segment = re.sub( + r'\:\:', + lambda m: f'\u001b[0;31;41m{m[0]}\u001b[0m', + segment, + ) + segment = re.sub( + r'\@(\[.*?\])', + lambda m: f'\u001b[0;33m{m[1]}\u001b[0m', + segment, + ) + segment = re.sub( + r'\{([\d;]*)\:(.*?)\}', + lambda m: f'\u001b[0;{m[1]}m{m[2]}\u001b[0m', + segment, + ) + return segment + + def ru_segments(self) -> list[str]: + return list(map(self.format_segment, (self.root / 'ru.md').read_text().split('=' * 80))) + + async def spam(self) -> None: + if self.spammed: + return + try: + print('spamming') + guild = await client.fetch_guild(config.guild) + channel = await guild.fetch_channel(1056432869834240080) + assert isinstance(channel, discord.abc.Messageable) + segments = await asyncio.to_thread(self.ru_segments) + for segment in segments: + await channel.send(segment.strip()) + except: + from traceback import print_exc + print_exc() + finally: + print('spammed') + self.spammed = True + + lock = asyncio.Lock() +spam = Spam() @client.event @@ -45,8 +103,10 @@ async def on_ready(): await client.change_presence(activity=discord.Game( name='феноменально', )) + task = asyncio.create_task(spam.spam()) async with lock: await state.reload() + await task class SimpleEmoji: