From a110b13ffb6418379dc2a30d953596599015ffbc Mon Sep 17 00:00:00 2001 From: timotheyca Date: Tue, 29 Mar 2022 19:05:30 +0300 Subject: [PATCH] emoji fix + poll fork --- v6d3vote/run-bot.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/v6d3vote/run-bot.py b/v6d3vote/run-bot.py index 224741a..fa6b25d 100644 --- a/v6d3vote/run-bot.py +++ b/v6d3vote/run-bot.py @@ -92,12 +92,19 @@ class Poll: ) @classmethod - async def create(cls, ctx: Context, options: list[tuple[str, discord.Emoji | str]], title: str): + async def create( + cls, ctx: Context, options: list[tuple[str, discord.Emoji | str]], title: str, + votes: dict[discord.Member, str] + ): message: discord.Message = await ctx.reply('creating poll...') async with lock_for(message, 'failed to create poll'): - poll = Poll( + if len(set(emoji for option, emoji in options)) != len(options): + raise Explicit('duplicate emojis') + if len(set(option for option, emoji in options)) != len(options): + raise Explicit('duplicate options') + poll = cls( message, - {}, + votes, {option: str(emoji) for option, emoji in options}, [option for option, _ in options], title @@ -167,12 +174,13 @@ async def poll_options(args: list[str]) -> list[tuple[str, discord.Emoji | str]] while args: match args: case [emoji, option, *args]: - try: - emoji = client.get_emoji( - int(''.join(c for c in emoji.rsplit(':', 1)[-1] if c.isdecimal())) - ) - except (discord.NotFound, ValueError): - pass + if '<' in emoji and '>' in emoji: + try: + emoji = client.get_emoji( + int(''.join(c for c in emoji.rsplit(':', 1)[-1] if c.isdecimal())) + ) or emoji + except (discord.NotFound, ValueError): + pass options.append((option, emoji)) case _: raise Explicit('option not specified') @@ -184,10 +192,17 @@ async def create_poll(ctx: Context, args: list[str]) -> None: match args: case ['help']: await ctx.reply('`poll title emoji option [emoji option ...]`') + await ctx.reply('`poll emoji option [emoji option ...]` (reply fork)') case []: raise Explicit('no options') + case [*args] if ctx.message.reference is not None and ctx.message.reference.resolved is not None: + refd: discord.Message = ctx.message.reference.resolved + poll = await Poll.load(refd) + if poll is None: + raise Explicit('referenced message is not a poll') + await Poll.create(ctx, await poll_options(args), poll.title, poll.votes) case [title, *args]: - await Poll.create(ctx, await poll_options(args), title) + await Poll.create(ctx, await poll_options(args), title, {}) @client.event