Skip to content

Commit

Permalink
make code style 'black'
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBa001 committed Jan 17, 2021
1 parent d57001c commit 9536db2
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 114 deletions.
3 changes: 2 additions & 1 deletion disputils/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ async def update(self, text: str, color: hex = None, hide_author: bool = False):
:param text: The new text.
:param color: The new embed color.
:param hide_author: True if you want to hide the embed author (default: ``False``).
:param hide_author: True if you want to hide the embed author
(default: ``False``).
:rtype: ``None``
"""

Expand Down
48 changes: 29 additions & 19 deletions disputils/confirmation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
class Confirmation(Dialog):
""" Represents a message to let the user confirm a specific action. """

def __init__(self, client: discord.Client, color: hex = 0x000000, message: discord.Message = None):
def __init__(
self,
client: discord.Client,
color: hex = 0x000000,
message: discord.Message = None,
):
super().__init__(color=color)

self._client = client
Expand All @@ -24,8 +29,9 @@ def confirmed(self) -> bool:

return self._confirmed

async def confirm(self, text: str, user: discord.User, channel: discord.TextChannel = None)\
-> bool or None:
async def confirm(
self, text: str, user: discord.User, channel: discord.TextChannel = None
) -> bool or None:
"""
Run the confirmation.
Expand All @@ -35,21 +41,17 @@ async def confirm(self, text: str, user: discord.User, channel: discord.TextChan
:param user: The user who has to confirm.
:type user: :class:`discord.User`
:param channel: The channel the message will be sent to. Must only be specified if ``self.message`` is None.
:param channel: The channel the message will be sent to. Must only be specified
if ``self.message`` is None.
:type channel: :class:`discord.TextChannel`, optional
:return: True when it's been confirmed, otherwise False. Will return None when a timeout occurs.
:return: True when it's been confirmed, otherwise False. Will return None when a
timeout occurs.
:rtype: :class:`bool`, optional
"""

emb = discord.Embed(
title=text,
color=self.color
)
emb.set_author(
name=str(user),
icon_url=user.avatar_url
)
emb = discord.Embed(title=text, color=self.color)
emb.set_author(name=str(user), icon_url=user.avatar_url)

self._embed = emb

Expand All @@ -66,9 +68,11 @@ async def confirm(self, text: str, user: discord.User, channel: discord.TextChan

try:
reaction, user = await self._client.wait_for(
'reaction_add',
check=lambda r, u: (r.message.id == msg.id) and (u.id == user.id) and (r.emoji in self.emojis),
timeout=20
"reaction_add",
check=lambda r, u: (r.message.id == msg.id)
and (u.id == user.id)
and (r.emoji in self.emojis),
timeout=20,
)
except asyncio.TimeoutError:
self._confirmed = None
Expand All @@ -86,13 +90,19 @@ async def confirm(self, text: str, user: discord.User, channel: discord.TextChan


class BotConfirmation(Confirmation):
def __init__(self, ctx: commands.Context, color: hex = 0x000000, message: discord.Message = None):
def __init__(
self,
ctx: commands.Context,
color: hex = 0x000000,
message: discord.Message = None,
):
self._ctx = ctx

super().__init__(ctx.bot, color, message)

async def confirm(self, text: str, user: discord.User = None, channel: discord.TextChannel = None) \
-> bool or None:
async def confirm(
self, text: str, user: discord.User = None, channel: discord.TextChannel = None
) -> bool or None:

if user is None:
user = self._ctx.author
Expand Down
70 changes: 46 additions & 24 deletions disputils/multiple_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@

class MultipleChoice(Dialog):
"""
Generate and manage a reaction controlled rich embed multiple choice poll in Discord.
Generate and manage a reaction controlled rich embed multiple choice poll in
Discord.
:param title: Embed title.
:type title: :class:`str`
:param description: Embed description.
:type description: :class:`str`
:param options: Options to choose from. Each option is going to be a separate embed field.
:param options: Options to choose from. Each option is going to be a separate embed
field.
:type options: list[:class:`str`]
"""

def __init__(self, client: Client, options: List[str], title: str, description: str = "", **kwargs):
def __init__(
self,
client: Client,
options: List[str],
title: str,
description: str = "",
**kwargs
):
super().__init__(**kwargs)

self._client: Client = client
Expand All @@ -34,49 +43,45 @@ def __init__(self, client: Client, options: List[str], title: str, description:
self._embed: Optional[discord.Embed] = None
self._emojis: List[str] = []

self.close_emoji = '❌'
self.close_emoji = "❌"

self._choice = None

def _parse_kwargs(self, **kwargs):
self.message: Message = kwargs.get("message") or kwargs.get("msg") or self.message
self.message: Message = (
kwargs.get("message") or kwargs.get("msg") or self.message
)

def _generate_emojis(self) -> List[str]:
self._emojis.clear()

if len(self.options) > 10:
for i in range(len(self.options)): # generates unicode emojis [A,B,C,…]
hex_str = hex(224 + (6 + i))[2:]
emoji = b'\\U0001f1a'.replace(b'a', bytes(hex_str, "utf-8"))
emoji = b"\\U0001f1a".replace(b"a", bytes(hex_str, "utf-8"))
emoji = emoji.decode("unicode-escape")
self._emojis.append(emoji)

else:
for i in range(len(self.options)): # generates unicode emojis [1,2,3,…]
if i < 9:
emoji = 'x\u20e3'.replace('x', str(i + 1))
emoji = "x\u20e3".replace("x", str(i + 1))
else:
emoji = '\U0001f51f'
emoji = "\U0001f51f"

self._emojis.append(emoji)

return self._emojis

def _generate_embed(self) -> discord.Embed:
config_embed = discord.Embed(
title=self.title,
description=self.description,
color=self.color
title=self.title, description=self.description, color=self.color
)

emojis = self._generate_emojis()

for i in range(len(self.options)):
config_embed.add_field(
name=emojis[i],
value=self.options[i],
inline=False
)
config_embed.add_field(name=emojis[i], value=self.options[i], inline=False)

self._embed = config_embed
return config_embed
Expand All @@ -96,8 +101,12 @@ def choice(self) -> str:

return self._choice

async def run(self, users: Union[User, List[User]] = None, channel: TextChannel = None, **kwargs) \
-> Tuple[Optional[str], Message]:
async def run(
self,
users: Union[User, List[User]] = None,
channel: TextChannel = None,
**kwargs
) -> Tuple[Optional[str], Message]:
"""
Run the multiple choice dialog.
Expand Down Expand Up @@ -133,7 +142,10 @@ async def run(self, users: Union[User, List[User]] = None, channel: TextChannel
await self.message.clear_reactions()
await self.message.edit(content=self.message.content, embed=config_embed)
else:
raise TypeError("Missing argument. You need to specify either 'channel' or 'message' as a target.")
raise TypeError(
"Missing argument. "
+ "You need to specify either 'channel' or 'message' as a target."
)

for emoji in self._emojis:
await self.message.add_reaction(emoji)
Expand All @@ -156,7 +168,9 @@ def check(r, u):
return res

try:
reaction, user = await self._client.wait_for('reaction_add', check=check, timeout=timeout)
reaction, user = await self._client.wait_for(
"reaction_add", check=check, timeout=timeout
)
except asyncio.TimeoutError:
self._choice = None
return None, self.message
Expand All @@ -172,15 +186,23 @@ def check(r, u):


class BotMultipleChoice(MultipleChoice):
""" Same as :class:`MultipleChoice`, except for the discord.py commands extension. """
"""
Same as :class:`MultipleChoice`, except for the discord.py commands extension.
"""

def __init__(self, ctx: Context, options: list, title: str, description: str = "", **kwargs):
def __init__(
self, ctx: Context, options: list, title: str, description: str = "", **kwargs
):
super().__init__(ctx.bot, options, title, description, **kwargs)

self._ctx = ctx

async def run(self, users: Union[User, List[User]] = None, channel: TextChannel = None, **kwargs)\
-> Tuple[Optional[str], Message]:
async def run(
self,
users: Union[User, List[User]] = None,
channel: TextChannel = None,
**kwargs
) -> Tuple[Optional[str], Message]:

if self.message is None and channel is None:
channel = self._ctx.channel
Expand Down
Loading

0 comments on commit 9536db2

Please sign in to comment.