mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-10-23 00:42:56 +00:00
Refactor internal message sending and editing parameter passing
This reduces some repetition in many functions and is ripped out of the webhook code. This also removes the unused HTTP functions for interaction responses since those belong in the webhook code rather than the HTTPClient.
This commit is contained in:
110
discord/abc.py
110
discord/abc.py
@@ -51,6 +51,7 @@ from .permissions import PermissionOverwrite, Permissions
|
||||
from .role import Role
|
||||
from .invite import Invite
|
||||
from .file import File
|
||||
from .http import handle_message_parameters
|
||||
from .voice_client import VoiceClient, VoiceProtocol
|
||||
from .sticker import GuildSticker, StickerItem
|
||||
from . import utils
|
||||
@@ -1330,107 +1331,40 @@ class Messageable:
|
||||
channel = await self._get_channel()
|
||||
state = self._state
|
||||
content = str(content) if content is not None else None
|
||||
|
||||
if embed is not None and embeds is not None:
|
||||
raise InvalidArgument('cannot pass both embed and embeds parameter to send()')
|
||||
|
||||
if embed is not None:
|
||||
embed = embed.to_dict()
|
||||
|
||||
elif embeds is not None:
|
||||
if len(embeds) > 10:
|
||||
raise InvalidArgument('embeds parameter must be a list of up to 10 elements')
|
||||
embeds = [embed.to_dict() for embed in embeds]
|
||||
previous_allowed_mention = state.allowed_mentions
|
||||
|
||||
if stickers is not None:
|
||||
stickers = [sticker.id for sticker in stickers]
|
||||
|
||||
if allowed_mentions is not None:
|
||||
if state.allowed_mentions is not None:
|
||||
allowed_mentions = state.allowed_mentions.merge(allowed_mentions).to_dict()
|
||||
else:
|
||||
allowed_mentions = allowed_mentions.to_dict()
|
||||
else:
|
||||
allowed_mentions = state.allowed_mentions and state.allowed_mentions.to_dict()
|
||||
|
||||
if mention_author is not None:
|
||||
allowed_mentions = allowed_mentions or AllowedMentions().to_dict()
|
||||
allowed_mentions['replied_user'] = bool(mention_author)
|
||||
stickers = MISSING
|
||||
|
||||
if reference is not None:
|
||||
try:
|
||||
reference = reference.to_message_reference_dict()
|
||||
except AttributeError:
|
||||
raise InvalidArgument('reference parameter must be Message, MessageReference, or PartialMessage') from None
|
||||
|
||||
if view:
|
||||
if not hasattr(view, '__discord_ui_view__'):
|
||||
raise InvalidArgument(f'view parameter must be View not {view.__class__!r}')
|
||||
|
||||
components = view.to_components()
|
||||
else:
|
||||
components = None
|
||||
reference = MISSING
|
||||
|
||||
if file is not None and files is not None:
|
||||
raise InvalidArgument('cannot pass both file and files parameter to send()')
|
||||
if view and not hasattr(view, '__discord_ui_view__'):
|
||||
raise InvalidArgument(f'view parameter must be View not {view.__class__!r}')
|
||||
|
||||
if file is not None:
|
||||
if not isinstance(file, File):
|
||||
raise InvalidArgument('file parameter must be File')
|
||||
|
||||
try:
|
||||
data = await state.http.send_files(
|
||||
channel.id,
|
||||
files=[file],
|
||||
allowed_mentions=allowed_mentions,
|
||||
content=content,
|
||||
tts=tts,
|
||||
embed=embed,
|
||||
embeds=embeds,
|
||||
nonce=nonce,
|
||||
message_reference=reference,
|
||||
stickers=stickers,
|
||||
components=components,
|
||||
)
|
||||
finally:
|
||||
file.close()
|
||||
|
||||
elif files is not None:
|
||||
if len(files) > 10:
|
||||
raise InvalidArgument('files parameter must be a list of up to 10 elements')
|
||||
elif not all(isinstance(file, File) for file in files):
|
||||
raise InvalidArgument('files parameter must be a list of File')
|
||||
|
||||
try:
|
||||
data = await state.http.send_files(
|
||||
channel.id,
|
||||
files=files,
|
||||
content=content,
|
||||
tts=tts,
|
||||
embed=embed,
|
||||
embeds=embeds,
|
||||
nonce=nonce,
|
||||
allowed_mentions=allowed_mentions,
|
||||
message_reference=reference,
|
||||
stickers=stickers,
|
||||
components=components,
|
||||
)
|
||||
finally:
|
||||
for f in files:
|
||||
f.close()
|
||||
else:
|
||||
data = await state.http.send_message(
|
||||
channel.id,
|
||||
content,
|
||||
tts=tts,
|
||||
embed=embed,
|
||||
embeds=embeds,
|
||||
nonce=nonce,
|
||||
allowed_mentions=allowed_mentions,
|
||||
message_reference=reference,
|
||||
stickers=stickers,
|
||||
components=components,
|
||||
)
|
||||
with handle_message_parameters(
|
||||
content=content,
|
||||
tts=tts,
|
||||
file=file if file is not None else MISSING,
|
||||
files=files if files is not None else MISSING,
|
||||
embed=embed if embed is not None else MISSING,
|
||||
embeds=embeds if embeds is not None else MISSING,
|
||||
nonce=nonce,
|
||||
allowed_mentions=allowed_mentions,
|
||||
message_reference=reference,
|
||||
previous_allowed_mentions=previous_allowed_mention,
|
||||
mention_author=mention_author,
|
||||
stickers=stickers,
|
||||
view=view,
|
||||
) as params:
|
||||
data = await state.http.send_message(channel.id, params=params)
|
||||
|
||||
ret = state.create_message(channel=channel, data=data)
|
||||
if view:
|
||||
|
Reference in New Issue
Block a user