add more attributes to context

This commit is contained in:
Zomatree
2021-10-29 03:48:38 +01:00
parent 287b2d42ea
commit c5a67335d2
2 changed files with 21 additions and 9 deletions
+18 -6
View File
@@ -13,7 +13,7 @@ __all__ = (
class Context:
"""Stores metadata the commands execution.
Parameters
-----------
command: Optional[:class:`Command`]
@@ -22,16 +22,28 @@ class Context:
The command name that was used, this can be an alias, the commands name or a command that doesnt exist
message: :class:`Message`
The message that was sent to invoke the command
channel: :class:`Messageable`
The channel the command was invoked in
server: :class:`Server`
The server the command was invoked in
author: Union[:class:`Member`, :class:`User`]
The user or member that invoked the commad, will be :class:`User` in DMs
args: list[:class:`str`]
The arguments being passed to the command
"""
__slots__ = ("command", "invoked_with", "message")
__slots__ = ("command", "invoked_with", "args", "message", "server", "channel", "author")
def __init__(self, command: Optional[Command], invoked_with: str, message: revolt.Message):
def __init__(self, command: Optional[Command], invoked_with: str, args: list[str], message: revolt.Message):
self.command = command
self.invoked_with = invoked_with
self.args = args
self.message = message
self.server = message.server
self.channel = message.channel
self.author = message.author
async def invoke(self, args: list[str]) -> Any:
"""Invokes the command, this is equal to `await context.command.invoke(context, args)`.
async def invoke(self) -> Any:
"""Invokes the command, this is equal to `await command.invoke(context, context.args)`.
.. note:: If the command is `None`, this function will do nothing.
@@ -41,7 +53,7 @@ class Context:
The args being passed to the command
"""
if command := self.command:
return await command.invoke(self, args)
return await command.invoke(self, self.args)
@copy_doc(revolt.Messageable.send)
async def send(self, content: Optional[str] = None, *, embeds: Optional[list[revolt.Embed]] = None, embed: Optional[revolt.Embed] = None, attachments: Optional[list[revolt.File]] = None) -> revolt.Message:
+3 -3
View File
@@ -172,12 +172,12 @@ class CommandsMixin(metaclass=CommandsMeta):
try:
command = self.get_command(command_name)
except KeyError:
return self.dispatch("command_error", Context(None, command_name, message), CommandNotFound(command_name))
return self.dispatch("command_error", Context(None, command_name, args, message), CommandNotFound(command_name))
context = self.get_context(message)(command, command_name, message)
context = self.get_context(message)(command, command_name, args, message)
try:
return await context.invoke(args)
return await context.invoke()
except Exception as e:
self.dispatch("command_error", context, e)