Skip to content

Passing Callbacks Directly

If you choose not to use EventCollections directly, you can pass any async function.

from dispike import interactions

async def sample(...):
  return ...



from dispike import Dispike

bot = Dispike(...)
bot.register_event_command(
    function_event_name="sample",
  function=sample,
  function_type=EventTypes.COMMAND
)

This will register an event command. Usually you would use this if you are using dispike.interactions.on; instead of the self.on.

Parameters:

Name Type Description Default
function_event_name str

Function event name

required
function Callable

Function to register. Must be async

required
function_type EventTypes

[description]. Function type, defaults to None, but will still attempt to find the type. Will raise an error if it cannot.

None

Exceptions:

Type Description
AttributeError

If function_type is not a string or None, and it cannot be found in the functions attributes.

Source code in dispike/main.py
def register_event_command(
    self,
    function_event_name: str,
    function: typing.Callable,
    function_type: EventTypes = None,
    **kwargs,
):
    """This will register an event command.
    Usually you would use this if you are using dispike.interactions.on; instead of the self.on.

    Args:
        function_event_name (str): Function event name
        function (typing.Callable): Function to register. Must be async
        function_type (EventTypes): [description]. Function type, defaults to None, but will still attempt to find the type. Will raise an error if it cannot.

    Raises:
        AttributeError: If function_type is not a string or None, and it cannot be found in the functions attributes.
    """
    if function_type is None:
        # try to see if the function has a _event_type attribute
        try:
            function_type = function._dispike_event_type
        except AttributeError:
            raise AttributeError(
                "Unable to find function event type attribute inside function.. Did you add a decorator to this function?"
            )
    else:
        if isinstance(function_type, EventTypes):
            function_type = function_type

    self._add_function_to_callbacks(
        function_name=function_event_name,
        function_type=function_type,
        function=function,
    )