emoji fix + poll fork

This commit is contained in:
AF 2022-03-29 19:05:30 +03:00
parent 3f84fe60ec
commit a110b13ffb

View File

@ -92,12 +92,19 @@ class Poll:
) )
@classmethod @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...') message: discord.Message = await ctx.reply('creating poll...')
async with lock_for(message, 'failed to create 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, message,
{}, votes,
{option: str(emoji) for option, emoji in options}, {option: str(emoji) for option, emoji in options},
[option for option, _ in options], [option for option, _ in options],
title title
@ -167,12 +174,13 @@ async def poll_options(args: list[str]) -> list[tuple[str, discord.Emoji | str]]
while args: while args:
match args: match args:
case [emoji, option, *args]: case [emoji, option, *args]:
try: if '<' in emoji and '>' in emoji:
emoji = client.get_emoji( try:
int(''.join(c for c in emoji.rsplit(':', 1)[-1] if c.isdecimal())) emoji = client.get_emoji(
) int(''.join(c for c in emoji.rsplit(':', 1)[-1] if c.isdecimal()))
except (discord.NotFound, ValueError): ) or emoji
pass except (discord.NotFound, ValueError):
pass
options.append((option, emoji)) options.append((option, emoji))
case _: case _:
raise Explicit('option not specified') raise Explicit('option not specified')
@ -184,10 +192,17 @@ async def create_poll(ctx: Context, args: list[str]) -> None:
match args: match args:
case ['help']: case ['help']:
await ctx.reply('`poll title emoji option [emoji option ...]`') await ctx.reply('`poll title emoji option [emoji option ...]`')
await ctx.reply('`poll emoji option [emoji option ...]` (reply fork)')
case []: case []:
raise Explicit('no options') 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]: case [title, *args]:
await Poll.create(ctx, await poll_options(args), title) await Poll.create(ctx, await poll_options(args), title, {})
@client.event @client.event