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
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