Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from . import utils
from .context_managers import Typing
from .enums import ChannelType
from .errors import ClientException, InvalidArgument
from .errors import ClientException
from .file import File, VoiceMessage
from .flags import ChannelFlags, MessageFlags
from .invite import Invite
Expand Down Expand Up @@ -357,7 +357,7 @@ async def _move(
reason: str | None,
) -> None:
if position < 0:
raise InvalidArgument("Channel position cannot be less than 0.")
raise ValueError("Channel position cannot be less than 0.")

http = self._state.http
bucket = self._sorting_bucket
Expand Down Expand Up @@ -460,7 +460,7 @@ async def _edit(self, options: dict[str, Any], reason: str | None) -> ChannelPay
perms = []
for target, perm in overwrites.items():
if not isinstance(perm, PermissionOverwrite):
raise InvalidArgument(f"Expected PermissionOverwrite received {perm.__class__.__name__}")
raise TypeError(f"Expected PermissionOverwrite received {perm.__class__.__name__}")

allow, deny = perm.pair()
payload = {
Expand All @@ -479,7 +479,7 @@ async def _edit(self, options: dict[str, Any], reason: str | None) -> ChannelPay
pass
else:
if not isinstance(ch_type, ChannelType):
raise InvalidArgument("type field must be of type ChannelType")
raise TypeError("type field must be of type ChannelType")
options["type"] = ch_type.value

try:
Expand All @@ -496,7 +496,7 @@ async def _edit(self, options: dict[str, Any], reason: str | None) -> ChannelPay
elif default_reaction_emoji is None:
pass
else:
raise InvalidArgument("default_reaction_emoji must be of type: GuildEmoji | int | str | None")
raise TypeError("default_reaction_emoji must be of type: GuildEmoji | int | str | None")

options["default_reaction_emoji"] = (
default_reaction_emoji._to_forum_reaction_payload() if default_reaction_emoji else None
Expand Down Expand Up @@ -893,7 +893,7 @@ async def set_permissions(self, target, *, overwrite=MISSING, reason=None, **per
Editing channel specific permissions failed.
~discord.NotFound
The role or member being edited is not part of the guild.
~discord.InvalidArgument
TypeError or ValueError
The overwrite parameter invalid or the target type was not
:class:`~discord.Role` or :class:`~discord.Member`.
"""
Expand All @@ -905,17 +905,17 @@ async def set_permissions(self, target, *, overwrite=MISSING, reason=None, **per
elif isinstance(target, Role):
perm_type = _Overwrites.ROLE
else:
raise InvalidArgument("target parameter must be either Member or Role")
raise TypeError("target parameter must be either Member or Role")

if overwrite is MISSING:
if len(permissions) == 0:
raise InvalidArgument("No overwrite provided.")
raise ValueError("No overwrite provided.")
try:
overwrite = PermissionOverwrite(**permissions)
except (ValueError, TypeError) as e:
raise InvalidArgument("Invalid permissions given to keyword arguments.") from e
raise ValueError("Invalid permissions given to keyword arguments.") from e
elif len(permissions) > 0:
raise InvalidArgument("Cannot mix overwrite and keyword arguments.")
raise ValueError("Cannot mix overwrite and keyword arguments.")

# TODO: wait for event

Expand All @@ -925,7 +925,7 @@ async def set_permissions(self, target, *, overwrite=MISSING, reason=None, **per
(allow, deny) = overwrite.pair()
await http.edit_channel_permissions(self.id, target.id, allow.value, deny.value, perm_type, reason=reason)
else:
raise InvalidArgument("Invalid overwrite type provided.")
raise ValueError("Invalid overwrite type provided.")

async def _clone_impl(
self: GCH,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ async def move(self, **kwargs) -> None:

Raises
------
InvalidArgument
ValueError
An invalid position was given or a bad mix of arguments was passed.
Forbidden
You do not have permissions to move the channel.
Expand All @@ -1089,7 +1089,7 @@ async def move(self, **kwargs) -> None:
before, after = kwargs.get("before"), kwargs.get("after")
offset = kwargs.get("offset", 0)
if sum(bool(a) for a in (beginning, end, before, after)) > 1:
raise InvalidArgument("Only one of [before, after, end, beginning] can be used.")
raise ValueError("Only one of [before, after, end, beginning] can be used.")

bucket = self._sorting_bucket
parent_id = kwargs.get("category", MISSING)
Expand Down Expand Up @@ -1124,7 +1124,7 @@ async def move(self, **kwargs) -> None:
index = next((i + 1 for i, c in enumerate(channels) if c.id == after.id), None)

if index is None:
raise InvalidArgument("Could not resolve appropriate move position")
raise ValueError("Could not resolve appropriate move position")

channels.insert(max((index + offset), 0), self)
payload = []
Expand Down Expand Up @@ -1481,7 +1481,7 @@ async def send(
Sending the message failed.
~discord.Forbidden
You do not have the proper permissions to send the message.
~discord.InvalidArgument
TypeError or ValueError
The ``files`` list is not of the appropriate size,
you specified both ``file`` and ``files``,
or you specified both ``embed`` and ``embeds``,
Expand All @@ -1494,14 +1494,14 @@ async def send(
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()")
raise ValueError("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")
raise ValueError("embeds parameter must be a list of up to 10 elements")
embeds = [embed.to_dict() for embed in embeds]

flags = MessageFlags(
Expand Down Expand Up @@ -1537,13 +1537,13 @@ async def send(
"3.0",
)
except AttributeError:
raise InvalidArgument(
raise ValueError(
"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}")
raise TypeError(f"view parameter must be View not {view.__class__!r}")

components = view.to_components()
if view.is_components_v2():
Expand All @@ -1557,17 +1557,17 @@ async def send(
poll = poll.to_dict()

if file is not None and files is not None:
raise InvalidArgument("cannot pass both file and files parameter to send()")
raise ValueError("cannot pass both file and files parameter to send()")

if file is not None:
if not isinstance(file, File):
raise InvalidArgument("file parameter must be File")
raise TypeError("file parameter must be File")
files = [file]
elif files is not None:
if len(files) > 10:
raise InvalidArgument("files parameter must be a list of up to 10 elements")
raise ValueError("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")
raise TypeError("files parameter must be a list of File")

if files is not None:
flags = flags + MessageFlags(is_voice_message=any(isinstance(f, VoiceMessage) for f in files))
Expand Down
24 changes: 12 additions & 12 deletions discord/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import yarl

from . import utils
from .errors import DiscordException, InvalidArgument
from .errors import DiscordException

__all__ = ("Asset",)

Expand Down Expand Up @@ -401,7 +401,7 @@ def replace(

Raises
------
InvalidArgument
TypeError or ValueError
An invalid size or format was passed.
"""
url = yarl.URL(self._url)
Expand All @@ -410,21 +410,21 @@ def replace(
if format is not MISSING:
if self._animated:
if format not in VALID_ASSET_FORMATS:
raise InvalidArgument(f"format must be one of {VALID_ASSET_FORMATS}")
raise ValueError(f"format must be one of {VALID_ASSET_FORMATS}")
url = url.with_path(f"{path}.{format}")
elif static_format is MISSING:
if format not in VALID_STATIC_FORMATS:
raise InvalidArgument(f"format must be one of {VALID_STATIC_FORMATS}")
raise ValueError(f"format must be one of {VALID_STATIC_FORMATS}")
url = url.with_path(f"{path}.{format}")

if static_format is not MISSING and not self._animated:
if static_format not in VALID_STATIC_FORMATS:
raise InvalidArgument(f"static_format must be one of {VALID_STATIC_FORMATS}")
raise ValueError(f"static_format must be one of {VALID_STATIC_FORMATS}")
url = url.with_path(f"{path}.{static_format}")

if size is not MISSING:
if not _valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 4096")
raise ValueError("size must be a power of 2 between 16 and 4096")
url = url.with_query(size=size)
else:
url = url.with_query(url.raw_query_string)
Expand All @@ -447,11 +447,11 @@ def with_size(self, size: int, /) -> Asset:

Raises
------
InvalidArgument
TypeError or ValueError
The asset had an invalid size.
"""
if not _valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 4096")
raise ValueError("size must be a power of 2 between 16 and 4096")

url = str(yarl.URL(self._url).with_query(size=size))
return Asset(state=self._state, url=url, key=self._key, animated=self._animated)
Expand All @@ -471,15 +471,15 @@ def with_format(self, format: ValidAssetFormatTypes, /) -> Asset:

Raises
------
InvalidArgument
TypeError or ValueError
The asset has an invalid format.
"""

if self._animated:
if format not in VALID_ASSET_FORMATS:
raise InvalidArgument(f"format must be one of {VALID_ASSET_FORMATS}")
raise ValueError(f"format must be one of {VALID_ASSET_FORMATS}")
elif format not in VALID_STATIC_FORMATS:
raise InvalidArgument(f"format must be one of {VALID_STATIC_FORMATS}")
raise ValueError(f"format must be one of {VALID_STATIC_FORMATS}")

url = yarl.URL(self._url)
path, _ = os.path.splitext(url.path)
Expand All @@ -504,7 +504,7 @@ def with_static_format(self, format: ValidStaticFormatTypes, /) -> Asset:

Raises
------
InvalidArgument
TypeError or ValueError
The asset had an invalid format.
"""

Expand Down
34 changes: 17 additions & 17 deletions discord/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
try_enum,
)
from .enums import ThreadArchiveDuration as ThreadArchiveDurationEnum
from .errors import ClientException, InvalidArgument
from .errors import ClientException
from .file import File
from .flags import ChannelFlags, MessageFlags
from .invite import Invite
Expand Down Expand Up @@ -562,7 +562,7 @@ async def follow(self, *, destination: TextChannel, reason: str | None = None) -
raise ClientException("The channel must be a news channel.")

if not isinstance(destination, TextChannel):
raise InvalidArgument(f"Expected TextChannel received {destination.__class__.__name__}")
raise TypeError(f"Expected TextChannel received {destination.__class__.__name__}")

from .webhook import Webhook # noqa: PLC0415

Expand Down Expand Up @@ -827,7 +827,7 @@ async def edit(self, *, reason=None, **options):

Raises
------
InvalidArgument
TypeError or ValueError
If position is less than 0 or greater than the number of channels, or if
the permission overwrite information is not in proper form.
Forbidden
Expand Down Expand Up @@ -1138,7 +1138,7 @@ async def edit(self, *, reason=None, **options):

Raises
------
InvalidArgument
TypeError or ValueError
If position is less than 0 or greater than the number of channels, or if
the permission overwrite information is not in proper form.
Forbidden
Expand Down Expand Up @@ -1245,14 +1245,14 @@ async def create_thread(
message_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 create_thread()")
raise ValueError("cannot pass both embed and embeds parameter to create_thread()")

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")
raise ValueError("embeds parameter must be a list of up to 10 elements")
embeds = [embed.to_dict() for embed in embeds]

if stickers is not None:
Expand All @@ -1272,7 +1272,7 @@ async def create_thread(

if view:
if not hasattr(view, "__discord_ui_view__"):
raise InvalidArgument(f"view parameter must be View not {view.__class__!r}")
raise TypeError(f"view parameter must be View not {view.__class__!r}")

components = view.to_components()
if view.is_components_v2():
Expand All @@ -1286,17 +1286,17 @@ async def create_thread(
applied_tags = [str(tag.id) for tag in applied_tags]

if file is not None and files is not None:
raise InvalidArgument("cannot pass both file and files parameter to send()")
raise ValueError("cannot pass both file and files parameter to send()")

if files is not None:
if len(files) > 10:
raise InvalidArgument("files parameter must be a list of up to 10 elements")
raise ValueError("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")
raise TypeError("files parameter must be a list of File")

if file is not None:
if not isinstance(file, File):
raise InvalidArgument("file parameter must be File")
raise TypeError("file parameter must be File")
files = [file]

try:
Expand Down Expand Up @@ -1499,7 +1499,7 @@ async def edit(self, *, reason=None, **options):

Raises
------
InvalidArgument
TypeError or ValueError
If position is less than 0 or greater than the number of channels, or if
the permission overwrite information is not in proper form.
Forbidden
Expand Down Expand Up @@ -2074,7 +2074,7 @@ async def edit(self, *, reason=None, **options):

Raises
------
InvalidArgument
TypeError or ValueError
If the permission overwrite information is not in proper form.
Forbidden
You do not have permissions to edit the channel.
Expand Down Expand Up @@ -2606,7 +2606,7 @@ async def create_instance(

Raises
------
InvalidArgument
TypeError or ValueError
If the ``privacy_level`` parameter is not the proper type.
Forbidden
You do not have permissions to create a stage instance.
Expand All @@ -2622,7 +2622,7 @@ async def create_instance(

if privacy_level is not MISSING:
if not isinstance(privacy_level, StagePrivacyLevel):
raise InvalidArgument("privacy_level field must be of type PrivacyLevel")
raise TypeError("privacy_level field must be of type PrivacyLevel")

payload["privacy_level"] = privacy_level.value

Expand Down Expand Up @@ -2725,7 +2725,7 @@ async def edit(self, *, reason=None, **options):

Raises
------
InvalidArgument
TypeError or ValueError
If the permission overwrite information is not in proper form.
Forbidden
You do not have permissions to edit the channel.
Expand Down Expand Up @@ -2870,7 +2870,7 @@ async def edit(self, *, reason=None, **options):

Raises
------
InvalidArgument
TypeError or ValueError
If position is less than 0 or greater than the number of categories.
Forbidden
You do not have permissions to edit the category.
Expand Down
Loading