Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import discord
from discord import Message, Client, TextChannel, User
from discord.ext.commands import Context
import asyncio
from typing import Tuple, List, Optional
from .abc import Dialog
class MultipleChoice(Dialog):
def __init__(self, client: Client, options: list, title: str, description: str = "", **kwargs):
super().__init__(**kwargs)
self._client: Client = client
self.options: List[str] = options
self.title: str = title
self.description: str = description
self.message: Optional[Message] = None
self._parse_kwargs(**kwargs)
self._embed: Optional[discord.Embed] = None
self._emojis: List[str] = []
self.close_emoji = '❌'
import discord
from discord.ext import commands
import asyncio
from .abc import Dialog
from typing import Optional
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):
super().__init__(color=color)
self._client = client
self.color = color
self.emojis = {"✅": True, "❌": False}
self._confirmed = None
self.message = message
self._embed: Optional[discord.Embed] = None
@property
def confirmed(self) -> bool:
""" Whether the user has confirmed the action. """
import discord
from discord.ext import commands
import asyncio
from copy import deepcopy
from typing import List
from .abc import Dialog
class EmbedPaginator(Dialog):
""" Represents an interactive menu containing multiple embeds. """
def __init__(self, client: discord.Client, pages: [discord.Embed], message: discord.Message = None):
"""
Initialize a new EmbedPaginator.
:param client: The :class:`discord.Client` to use.
:param pages: A list of :class:`discord.Embed` to paginate through.
:param message: An optional :class:`discord.Message` to edit. Otherwise a new message will be sent.
"""
super().__init__()
self._client = client
self.pages = pages
self.message = message
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
return
finally:
await msg.clear_reactions()
confirmed = self.emojis[reaction.emoji]
self._confirmed = confirmed
return confirmed
class BotConfirmation(Confirmation):
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:
if user is None:
user = self._ctx.author
if self.message is None and channel is None:
channel = self._ctx.channel
return await super().confirm(text, user, channel)
reaction, user = await self._client.wait_for('reaction_add', check=check, timeout=timeout)
except asyncio.TimeoutError:
self._choice = None
return None, self.message
if closable and reaction.emoji == self.close_emoji:
self._choice = None
return None, self.message
index = self._emojis.index(reaction.emoji)
self._choice = self.options[index]
return self._choice, self.message
class BotMultipleChoice(MultipleChoice):
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: List[User] = None, channel: TextChannel = None, **kwargs)\
-> Tuple[Optional[str], Message]:
if users is None:
users = [self._ctx.author]
if self.message is None and channel is None:
channel = self._ctx.channel
return await super().run(users, channel, **kwargs)
def __init__(self, ctx: commands.Context, pages: [discord.Embed], message: discord.Message = None):
"""
Initialize a new EmbedPaginator.
:param ctx: The :class:`discord.ext.commands.Context` to use.
:param pages: A list of :class:`discord.Embed` to paginate through.
:param message: An optional :class:`discord.Message` to edit. Otherwise a new message will be sent.
"""
self._ctx = ctx
super(BotEmbedPaginator, self).__init__(ctx.bot, pages, message)
if len(l) > 25:
sub_lists = []
while len(l) > 20:
sub_lists.append(l[:20])
del l[:20]
sub_lists.append(l)
else:
sub_lists = [l]
return sub_lists
class BotEmbedPaginator(EmbedPaginator):
def __init__(self, ctx: commands.Context, pages: [discord.Embed], message: discord.Message = None):
"""
Initialize a new EmbedPaginator.
:param ctx: The :class:`discord.ext.commands.Context` to use.
:param pages: A list of :class:`discord.Embed` to paginate through.
:param message: An optional :class:`discord.Message` to edit. Otherwise a new message will be sent.
"""
self._ctx = ctx
super(BotEmbedPaginator, self).__init__(ctx.bot, pages, message)
async def run(self, channel: discord.TextChannel = None, users: List[discord.User] = None):
"""
Runs the paginator.