Skip to content

Commit

Permalink
added set_prefix to Chat and added support for prefixes that are long…
Browse files Browse the repository at this point in the history
…er than one character. Closes #154
  • Loading branch information
Teekeks committed Dec 16, 2022
1 parent b80750b commit 3f25a54
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions twitchAPI/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ class Chat:
def __init__(self, twitch: Twitch, connection_url: Optional[str] = None):
self.logger: Logger = getLogger('twitchAPI.chat')
"""The logger used for Chat related log messages"""
self._prefix: str = "!"
self.twitch: Twitch = twitch
if not self.twitch.has_required_auth(AuthType.USER, [AuthScope.CHAT_READ]):
raise ValueError('passed twitch instance is missing User Auth.')
Expand Down Expand Up @@ -353,6 +354,18 @@ async def _get_username(self):
user: TwitchUser = await first(self.twitch.get_users())
self.username = user.login.lower()

def set_prefix(self, prefix: str):
"""Sets a command prefix.
The default prefix is !, the prefix can not start with / or .
:param prefix: the new prefix to use for command parsing
:raises ValueError. when the given prefix is None or starts with / or .
"""
if prefix is None or prefix[0] in ('/', '.'):
raise ValueError('Prefix starting with / or . are reserved for twitch internal use')
self._prefix = prefix

##################################################################################################################################################
# command parsing
##################################################################################################################################################
Expand Down Expand Up @@ -400,14 +413,14 @@ def _parse_irc_message(self, message: str):

parsed_message['source'] = self._parse_irc_source(raw_source_component)
parsed_message['parameters'] = raw_parameters_component
if raw_parameters_component is not None and raw_parameters_component[0] == '!':
if raw_parameters_component is not None and raw_parameters_component.startswith(self._prefix):
parsed_message['command'] = self._parse_irc_parameters(raw_parameters_component, parsed_message['command'])

return parsed_message

def _parse_irc_parameters(self, raw_parameters_component: str, command):
idx = 0
command_parts = raw_parameters_component[1::].strip()
command_parts = raw_parameters_component[len(self._prefix)::].strip()
try:
params_idx = command_parts.index(' ')
except ValueError:
Expand Down

0 comments on commit 3f25a54

Please sign in to comment.