Skip to content

Commit 34de90c

Browse files
committed
Add reply_translated to Context
1 parent d134dc1 commit 34de90c

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

twitchio/ext/commands/context.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,93 @@ async def reply(self, content: str, *, me: bool = False) -> SentMessage:
663663
new = (f"/me {content}" if me else content).strip()
664664
return await self.channel.send_message(sender=self.bot.bot_id, message=new, reply_to_message_id=self._payload.id)
665665

666+
async def reply_translated(self, content: str, *, me: bool = False, langcode: str | None = None) -> SentMessage:
667+
"""|coro|
668+
669+
Send a translated chat message as a reply to the user who this message is associated with and to the channel associated with
670+
this context.
671+
672+
You must have added a :class:`.commands.Translator` to your :class:`.commands.Command` in order to effectively use
673+
this method. If no :class:`.commands.Translator` is found, this method acts identical to :meth:`.reply`.
674+
675+
If this method can not find a valid language code, E.g. both :meth:`.commands.Translator.get_langcode` and the parameter
676+
``langcode`` return ``None``, this method acts identical to :meth:`.reply`.
677+
678+
See the following documentation for more details on translators:
679+
680+
- :class:`.commands.Translator`
681+
- :func:`.commands.translator`
682+
683+
.. warning::
684+
685+
You cannot use this method in Reward based context. E.g.
686+
if :attr:`~.commands.Context.type` is :attr:`~.commands.ContextType.REWARD`.
687+
688+
.. important::
689+
690+
You must have the ``user:write:chat`` scope. If an app access token is used,
691+
then additionally requires the ``user:bot`` scope on the bot,
692+
and either ``channel:bot`` scope from the broadcaster or moderator status.
693+
694+
Parameters
695+
----------
696+
content: str
697+
The content of the message you would like to translate and then send.
698+
This **and** the translated version of this content cannot exceed ``500`` characters.
699+
Additionally the content parameter will be stripped of all leading and trailing whitespace.
700+
me: bool
701+
An optional bool indicating whether you would like to send this message with the ``/me`` chat command.
702+
langcode: str | None
703+
An optional ``langcode`` to override the ``langcode`` returned from :meth:`.commands.Translator.get_langcode`.
704+
This should only be provided if you do custom language code lookups outside of your
705+
:class:`.commands.Translator`. Defaults to ``None``.
706+
707+
708+
Returns
709+
-------
710+
SentMessage
711+
The payload received by Twitch after sending this message.
712+
713+
Raises
714+
------
715+
HTTPException
716+
Twitch failed to process the message, could be ``400``, ``401``, ``403``, ``422`` or any ``5xx`` status code.
717+
MessageRejectedError
718+
Twitch rejected the message from various checks.
719+
TranslatorError
720+
An error occurred during translation.
721+
"""
722+
if self._type is ContextType.REWARD:
723+
raise TypeError("Cannot reply to a message in a Reward based context.")
724+
725+
translator: Translator | None = getattr(self.command, "translator", None)
726+
new = (f"/me {content}" if me else content).strip()
727+
728+
if not self.command or not translator:
729+
return await self.channel.send_message(sender=self.bot.bot_id, message=new, reply_to_message_id=self._payload.id)
730+
731+
invoked = self.invoked_with
732+
733+
try:
734+
code = langcode or translator.get_langcode(self, invoked.lower()) if invoked else None
735+
except Exception as e:
736+
raise TranslatorError(f"An exception occurred fetching a language code for '{invoked}'.", original=e) from e
737+
738+
if not code:
739+
return await self.channel.send_message(sender=self.bot.bot_id, message=new, reply_to_message_id=self._payload.id)
740+
741+
try:
742+
translated = await translator.translate(self, content, code)
743+
except Exception as e:
744+
raise TranslatorError(f"An exception occurred translating content for '{invoked}'.", original=e) from e
745+
746+
new_translated = (f"/me {translated}" if me else translated).strip()
747+
return await self.channel.send_message(
748+
sender=self.bot.bot_id,
749+
message=new_translated,
750+
reply_to_message_id=self._payload.id,
751+
)
752+
666753
async def send_announcement(
667754
self, content: str, *, color: Literal["blue", "green", "orange", "purple", "primary"] | None = None
668755
) -> None:

0 commit comments

Comments
 (0)