From 3f84fe60ec33ab5ee263bca0e2cadf6f215f697c Mon Sep 17 00:00:00 2001 From: timotheyca Date: Wed, 22 Dec 2021 21:11:47 +0300 Subject: [PATCH] updated apis --- example.sh | 1 + requirements.txt | 4 +-- v6d3vote/config.py | 4 +++ v6d3vote/run-bot.py | 59 ++++++++------------------------------------- 4 files changed, 17 insertions(+), 51 deletions(-) create mode 100644 example.sh diff --git a/example.sh b/example.sh new file mode 100644 index 0000000..a2dba9c --- /dev/null +++ b/example.sh @@ -0,0 +1 @@ +docker run -v v6d3vote:/v6data --env v6ca=da5261eb5232b4b08452f25099b53b59d2e308b86aaf9c4204f0aa92569044d7 --env v6caurl=http://172.18.0.2:5900 --env v6taurl=http://172.18.0.3:5910 --name v6d3vote -m 500mb --cpus 0,5 --network v6d v6d3vote diff --git a/requirements.txt b/requirements.txt index 74d36de..9780724 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ aiohttp~=3.7.4.post0 discord.py~=1.7.3 -git+https://gitea.ongoteam.net/PTV/v6d1tokens.git@2dca5338ecec2042f731ff2855225417f66e1372 -git+https://gitea.ongoteam.net/PTV/v6d2ctx.git@087aa39918a147ad9df7de35e6484ccb3efdc6c9 +git+https://gitea.ongoteam.net/PTV/v6d1tokens.git@8b525ee05be22891a1c18d5e96153807f79930be +git+https://gitea.ongoteam.net/PTV/v6d2ctx.git@57919e65069e62fc31666adebd6c107be067b724 diff --git a/v6d3vote/config.py b/v6d3vote/config.py index 970c50a..f06f6a7 100644 --- a/v6d3vote/config.py +++ b/v6d3vote/config.py @@ -1,3 +1,7 @@ import os +from v6d0auth.config import root + prefix = os.getenv('v6prefix', '??') +myroot = root / 'v6d3vote' +myroot.mkdir(exist_ok=True) diff --git a/v6d3vote/run-bot.py b/v6d3vote/run-bot.py index acac8a7..224741a 100644 --- a/v6d3vote/run-bot.py +++ b/v6d3vote/run-bot.py @@ -1,20 +1,20 @@ import asyncio import os -import shlex from typing import Optional -# noinspection PyPackageRequirements import discord from ptvp35 import Db, KVJson -from v6d0auth.config import root from v6d1tokens.client import request_token -from v6d2ctx.context import Context, of, at, Implicit, monitor, Explicit +from v6d2ctx.context import Context, at, monitor, Explicit +from v6d2ctx.handle_content import handle_content +from v6d2ctx.lock_for import lock_for +from v6d2ctx.serve import serve -from v6d3vote.config import prefix +from v6d3vote.config import prefix, myroot loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) -token = loop.run_until_complete(request_token('vote')) +token = loop.run_until_complete(request_token('vote', 'token')) client = discord.Client( intents=discord.Intents( members=True, @@ -26,8 +26,6 @@ client = discord.Client( reactions=True ), ) -myroot = root / 'v6d3vote' -myroot.mkdir(exist_ok=True) vote_db = Db(myroot / 'vote.db', kvrequest_type=KVJson) @@ -39,10 +37,6 @@ async def on_ready(): )) -async def handle_command(ctx: Context, name: str, args: list[str]) -> None: - await of('commands', name)(ctx, args) - - @at('commands', 'help') async def help_(ctx: Context, args: list[str]) -> None: match args: @@ -100,7 +94,7 @@ class Poll: @classmethod async def create(cls, ctx: Context, options: list[tuple[str, discord.Emoji | str]], title: str): message: discord.Message = await ctx.reply('creating poll...') - async with lock_for(message): + async with lock_for(message, 'failed to create poll'): poll = Poll( message, {}, @@ -148,7 +142,7 @@ class Poll: message: discord.Message = await channel.fetch_message(rrae.message_id) if message.author != client.user: return - async with lock_for(message): + async with lock_for(message, 'no message'): poll = await cls.load(message) if poll is None: return @@ -168,18 +162,6 @@ class Poll: await self.message.remove_reaction(other_reaction.emoji, member) -locks: dict[discord.Message, asyncio.Lock] = {} - - -def lock_for(message: discord.Message) -> asyncio.Lock: - if message is None: - raise Explicit('not in a guild') - if message in locks: - return locks[message] - else: - return locks.setdefault(message, asyncio.Lock()) - - async def poll_options(args: list[str]) -> list[tuple[str, discord.Emoji | str]]: options: list[tuple[str, discord.Emoji | str]] = [] while args: @@ -208,30 +190,9 @@ async def create_poll(ctx: Context, args: list[str]) -> None: await Poll.create(ctx, await poll_options(args), title) -async def handle_args(message: discord.Message, args: list[str]): - match args: - case []: - return - case [command_name, *command_args]: - ctx = Context(message) - try: - await handle_command(ctx, command_name, command_args) - except Implicit: - pass - except Explicit as e: - await ctx.reply(e.msg) - - @client.event async def on_message(message: discord.Message) -> None: - if message.author.bot: - return - content: str = message.content - if not content.startswith(prefix): - return - content = content.removeprefix(prefix) - args = shlex.split(content) - await handle_args(message, args) + await handle_content(message, message.content, prefix) @client.event @@ -253,4 +214,4 @@ async def main(): if __name__ == '__main__': - loop.run_until_complete(main()) + serve(main(), client, loop)