Add Traceback prompt (#5851)

Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
yuansheng1549
2023-04-15 00:50:39 +08:00
committed by GitHub
parent 896d5e9200
commit 030607fb04
3 changed files with 133 additions and 64 deletions

View File

@@ -9,8 +9,6 @@ import discord
from discord.ext.commands import Context as DPYContext
from .requires import PermState
from ..utils.chat_formatting import box, text_to_file
from ..utils.predicates import MessagePredicate
from ..utils import can_user_react_in
if TYPE_CHECKING:
@@ -152,11 +150,12 @@ class Context(DPYContext):
async def send_interactive(
self,
messages: Iterable[str],
box_lang: str = None,
box_lang: Optional[str] = None,
timeout: int = 15,
join_character: str = "",
) -> List[discord.Message]:
"""Send multiple messages interactively.
"""
Send multiple messages interactively.
The user will be prompted for whether or not they would like to view
the next message, one at a time. They will also be notified of how
@@ -167,7 +166,7 @@ class Context(DPYContext):
messages : `iterable` of `str`
The messages to send.
box_lang : str
If specified, each message will be contained within a codeblock of
If specified, each message will be contained within a code block of
this language.
timeout : int
How long the user has to respond to the prompt before it times out.
@@ -176,53 +175,19 @@ class Context(DPYContext):
The character used to join all the messages when the file output
is selected.
Returns
-------
List[discord.Message]
A list of sent messages.
"""
messages = tuple(messages)
ret = []
for idx, page in enumerate(messages, 1):
if box_lang is None:
msg = await self.send(page)
else:
msg = await self.send(box(page, lang=box_lang))
ret.append(msg)
n_remaining = len(messages) - idx
if n_remaining > 0:
if n_remaining == 1:
plural = ""
is_are = "is"
else:
plural = "s"
is_are = "are"
query = await self.send(
"There {} still {} message{} remaining. "
"Type `more` to continue or `file` to upload all contents as a file."
"".format(is_are, n_remaining, plural)
)
pred = MessagePredicate.lower_contained_in(("more", "file"), self)
try:
resp = await self.bot.wait_for(
"message",
check=pred,
timeout=timeout,
)
except asyncio.TimeoutError:
with contextlib.suppress(discord.HTTPException):
await query.delete()
break
else:
try:
await self.channel.delete_messages((query, resp))
except (discord.HTTPException, AttributeError):
# In case the bot can't delete other users' messages,
# or is not a bot account
# or channel is a DM
with contextlib.suppress(discord.HTTPException):
await query.delete()
if pred.result == 1:
await self.send(file=text_to_file(join_character.join(messages)))
break
return ret
return await self.bot.send_interactive(
channel=self.channel,
messages=messages,
user=self.author,
box_lang=box_lang,
timeout=timeout,
join_character=join_character,
)
async def embed_colour(self):
"""