Skip to content

Commit

Permalink
Add qualified_name, full_parent_name and relative_name to Command
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Feb 4, 2025
1 parent 0a6cc2b commit 6000d1b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions twitchio/ext/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,45 @@ def name(self) -> str:
"""Property returning the name of this command."""
return self._name

@property
def relative_name(self) -> str:
"""Property returning the name of this command relative to it's direct parent, if it has one.
E.g. Closely equivalent to ``"{parent.name} {name}"``.
If this command has no parent, this simply returns the name.
"""
return self._name if not self._parent else f"{self._parent._name} {self._name}"

@property
def full_parent_name(self) -> str:
"""Property returning the fully qualified name for all the parents for this command.
This takes into account the full parent hierarchy. If this command has no parents, this will be an empty :class:`str`.
E.g Closely equivalent to ``"{first_parent.name} {second_parent.name}"``.
"""
names: list[str] = []
command = self

while command.parent is not None:
command = command.parent
names.append(command.name)

return " ".join(reversed(names))

@property
def qualified_name(self) -> str:
"""Property returning the fully qualified name for this command.
This takes into account the parent hierarchy. If this command has no parent, this simply returns the name.
E.g. Closely equivalent to ``"{first_parent.name} {second_parent.name} {name}"``.
This would be the string you would need to send minus the command prefix to invoke this command.
"""
return f"{self.full_parent_name} {self.name}" if self.full_parent_name else self.name

@property
def aliases(self) -> list[str]:
"""Property returning a copy of the list of aliases associated with this command, if it has any set.
Expand Down

0 comments on commit 6000d1b

Please sign in to comment.