[base] Non testé: ajout de choice
[utils-emojs] Ajout d'un émoji
This commit is contained in:
parent
bc9c6cb5f9
commit
493e1ca449
@ -1,12 +1,13 @@
|
|||||||
"""Base class for module, never use directly !!!"""
|
"""Base class for module, never use directly !!!"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
from typing import List
|
from typing import List, Union, Optional
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
from storage import Objects
|
from storage import Objects
|
||||||
|
from utils import emojis
|
||||||
|
|
||||||
|
|
||||||
class BaseClass:
|
class BaseClass:
|
||||||
@ -188,3 +189,72 @@ class BaseClass:
|
|||||||
|
|
||||||
async def on_error(self, event_method, *args, **kwargs):
|
async def on_error(self, event_method, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def choice(self, message: discord.Message, choices: List[Union[discord.Emoji, discord.PartialEmoji, str]],
|
||||||
|
validation: bool = False,
|
||||||
|
validation_emote: Union[discord.Emoji, discord.PartialEmoji, str] = emojis.WHITE_CHECK_MARK,
|
||||||
|
minimal_choices: int = 1,
|
||||||
|
maximal_choices: Optional[int] = None,
|
||||||
|
timeout: Optional[float] = None,
|
||||||
|
user: Optional[discord.User] = None,
|
||||||
|
unique: bool = False):
|
||||||
|
final_choices: List[Union[discord.Emoji, discord.PartialEmoji, str]] = []
|
||||||
|
validation_step = False
|
||||||
|
for emoji in choices:
|
||||||
|
await message.add_reaction(emoji)
|
||||||
|
|
||||||
|
def check_add(reaction, u):
|
||||||
|
nonlocal validation_step, final_choices
|
||||||
|
if (not user.bot) and (user is None or u.id == user.id):
|
||||||
|
if validation_step and reaction.emoji == validation_emote:
|
||||||
|
return True
|
||||||
|
if reaction in choices:
|
||||||
|
if not unique or reaction.emoji not in final_choices:
|
||||||
|
final_choices.append(reaction.emoji)
|
||||||
|
if maximal_choices is not None and len(final_choices) > maximal_choices:
|
||||||
|
validation_step = False
|
||||||
|
asyncio.ensure_future(message.remove_reaction(validation_emote, self.client.user))
|
||||||
|
try:
|
||||||
|
asyncio.get_running_loop().run_until_complete(message.clear_reaction(validation_emote))
|
||||||
|
except discord.errors.Forbidden:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
if len(final_choices) >= minimal_choices:
|
||||||
|
if validation:
|
||||||
|
asyncio.get_running_loop().run_until_complete(message.add_reaction(validation_emote))
|
||||||
|
validation_step = True
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_remove(reaction: discord.Reaction, u):
|
||||||
|
nonlocal validation_step, final_choices
|
||||||
|
if (not user.bot) and (user is None or u.id == user.id):
|
||||||
|
if reaction.emoji in choices:
|
||||||
|
if not unique or reaction.count != 0:
|
||||||
|
final_choices.remove(reaction.emoji)
|
||||||
|
if len(final_choices) < minimal_choices:
|
||||||
|
if validation_step:
|
||||||
|
asyncio.ensure_future(message.remove_reaction(validation_emote, self.client.user))
|
||||||
|
try:
|
||||||
|
asyncio.get_running_loop().run_until_complete(message.clear_reaction(validation_emote))
|
||||||
|
except discord.errors.Forbidden:
|
||||||
|
pass
|
||||||
|
validation_step = False
|
||||||
|
return False
|
||||||
|
if (maximal_choices is None or len(final_choices) <= maximal_choices) and len(
|
||||||
|
final_choices) >= minimal_choices:
|
||||||
|
if validation:
|
||||||
|
asyncio.get_running_loop().run_until_complete(message.add_reaction(validation_emote))
|
||||||
|
validation_step = True
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
done, pending = await asyncio.wait([
|
||||||
|
self.client.wait_for('reaction_add', timeout=timeout, check=check_add),
|
||||||
|
self.client.wait_for('reaction_remove', timeout=timeout, check=check_remove)],
|
||||||
|
return_when=asyncio.FIRST_COMPLETED)
|
||||||
|
return final_choices
|
||||||
|
@ -87,7 +87,7 @@ class MainClass(BaseClassPython):
|
|||||||
self.objects.save_object('errorsList', self.errorsList)
|
self.objects.save_object('errorsList', self.errorsList)
|
||||||
|
|
||||||
# Wait 60 seconds and delete message
|
# Wait 60 seconds and delete message
|
||||||
#await asyncio.sleep(60)
|
# await asyncio.sleep(60)
|
||||||
try:
|
try:
|
||||||
# channel = self.client.get_channel(msg_id["channel_id"])
|
# channel = self.client.get_channel(msg_id["channel_id"])
|
||||||
# delete_message = await channel.fetch_message(msg_id["msg_id"])
|
# delete_message = await channel.fetch_message(msg_id["msg_id"])
|
||||||
@ -96,7 +96,8 @@ class MainClass(BaseClassPython):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
reaction, user = await self.client.wait_for('reaction_add', timeout=60.0, check=lambda r,
|
reaction, user = await self.client.wait_for('reaction_add', timeout=60.0, check=lambda r,
|
||||||
u: r.emoji == "🗑️" and not u.bot and self.auth(u))
|
u: r.emoji == "🗑️" and not u.bot and self.auth(
|
||||||
|
user))
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
await message.delete()
|
await message.delete()
|
||||||
else:
|
else:
|
||||||
|
@ -2,6 +2,7 @@ NUMBERS = ["1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣
|
|||||||
|
|
||||||
THUMBS_UP = "👍"
|
THUMBS_UP = "👍"
|
||||||
THUMBS_DOWN = "👎"
|
THUMBS_DOWN = "👎"
|
||||||
|
WHITE_CHECK_MARK = "✅"
|
||||||
|
|
||||||
|
|
||||||
def write_with_number(i):
|
def write_with_number(i):
|
||||||
|
Loading…
Reference in New Issue
Block a user