Skip to content

Commit f597e62

Browse files
committed
♻️ Extract name validation logic to helper method _validate_name_prefix
1 parent e127333 commit f597e62

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

discord/cog.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def _name_filter(c: Any) -> str:
8787
)
8888

8989

90+
def _validate_name_prefix(base_class: type, name: str) -> None:
91+
if name.startswith(("cog_", "bot_")):
92+
raise TypeError(
93+
f"Commands or listeners must not start with cog_ or bot_ (in method {base_class}.{name})"
94+
)
95+
96+
9097
class CogMeta(type):
9198
"""A metaclass for defining a cog.
9299
@@ -204,7 +211,8 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
204211
if getattr(value, "parent", None) and isinstance(
205212
value, ApplicationCommand
206213
):
207-
# Skip commands if they are a part of a group
214+
# Skip application commands if they are a part of a group
215+
# Since they are already added when the group is added
208216
continue
209217

210218
is_static_method = isinstance(value, staticmethod)
@@ -216,8 +224,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
216224
f"Command in method {base}.{elem!r} must not be"
217225
" staticmethod."
218226
)
219-
if elem.startswith(("cog_", "bot_")):
220-
raise TypeError(no_bot_cog.format(base, elem))
227+
_validate_name_prefix(base, elem)
221228
commands[elem] = value
222229

223230
if _is_bridge_command(value) and not value.parent:
@@ -226,8 +233,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
226233
f"Command in method {base}.{elem!r} must not be"
227234
" staticmethod."
228235
)
229-
if elem.startswith(("cog_", "bot_")):
230-
raise TypeError(no_bot_cog.format(base, elem))
236+
_validate_name_prefix(base, elem)
231237

232238
commands[f"ext_{elem}"] = value.ext_variant
233239
commands[f"app_{elem}"] = value.slash_variant
@@ -243,8 +249,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
243249
except AttributeError:
244250
continue
245251
else:
246-
if elem.startswith(("cog_", "bot_")):
247-
raise TypeError(no_bot_cog.format(base, elem))
252+
_validate_name_prefix(base, elem)
248253
listeners[elem] = value
249254

250255
new_cls.__cog_commands__ = list(commands.values())

0 commit comments

Comments
 (0)