Skip to content

Commit

Permalink
Return some command handling logic to get_context.
Browse files Browse the repository at this point in the history
Add is_valid to `Context`.
  • Loading branch information
EvieePy committed Jan 21, 2020
1 parent 6e8a2df commit a0e8a97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
44 changes: 22 additions & 22 deletions twitchio/ext/commands/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,16 @@ async def get_context(self, message, *, cls=None):
cls = Context

prefix = await self.get_prefix(message)
if not prefix:
return cls(message=message, prefix=prefix, valid=False)


context = cls(message=message, prefix=prefix)
return context

async def handle_commands(self, message):
context = await self.get_context(message)

await self.invoke(context)

async def invoke(self, context):
# TODO Docs
if not context.prefix:
return

content = context.message.content[len(context.prefix)::].lstrip(' ') # Strip prefix and remainder whitespace
content = message.content[len(prefix)::].lstrip(' ') # Strip prefix and remainder whitespace
parsed = StringParser().process_string(content) # Return the string as a dict view

try:
command_ = parsed.pop(0)
except KeyError:
return # No command was found
raise CommandNotFound(f'No valid command was passed.')

try:
command_ = self._command_aliases[command_]
Expand All @@ -165,11 +153,23 @@ async def invoke(self, context):
if command_ in self.commands:
command_ = self.commands[command_]
else:
context.command = None
return await self.event_command_error(context, CommandNotFound(f'Command <{command_}> was not found.'))
raise CommandNotFound(f'No command "{command_}" was found.')

context.command = command_
instance = command_._instance
args, kwargs = command_.parse_args(command_._instance, parsed)

context = cls(message=message, prefix=prefix, command=command_, args=args, kwargs=kwargs,
valid=True)
return context

async def handle_commands(self, message):
context = await self.get_context(message)

await self.invoke(context)

async def invoke(self, context):
# TODO Docs
if not context.prefix:
return

check_result = await self.handle_checks(context)

Expand All @@ -181,9 +181,9 @@ async def invoke(self, context):
if limited:
return await self.event_command_error(context, limited[0])

try:
context.args, context.kwargs = command_.parse_args(instance, parsed)
instance = context.command._instance

try:
await self.global_before_invoke(context)

if context.command._before_invoke:
Expand Down
1 change: 1 addition & 0 deletions twitchio/ext/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def __init__(self, message, **attrs):
self.kwargs = attrs.get('kwargs')

self.view = attrs.get('view')
self.is_valid = attrs.get('valid')

self.bot = self.channel._bot
self._ws = self.channel._ws
Expand Down

0 comments on commit a0e8a97

Please sign in to comment.