Commands Object

Note

This commands object is an extention of basic-client.

Usage..

from slack import commands

client = commands.Client(..., prefix="!")

@client.command(name="msg")
async def message(ctx: commands.Context, *args):
    await ctx.channel.send("message received!")

@client.command()
async def ping(ctx: commands.Context, *args):
    await ctx.channel.send("pong!")

If commands was start your prefix and the command was registered, dispatch command-function.

Context

Methods
class slack.commands.Context
message

Message object of context.

Type

Message

prefix

Message prefix.

Type

str

await archive()

This channel archive.

property author: Optional[Member]

Message author.

Returns

Message author.

Return type

Optional[Member]

property channel: Channel

Returns context channel.

Return type

Channel

await delete()

It deletes a message.

await send(text: str) Message

This function is a coroutine

It sends a message to a channel.

Parameters

text (str) – The text of the message to send.

Returns

A Message object.

Return type

Message

await send_as_user(text: str)

This function is a coroutine

Parameters

text (str) – Message you want to send by your.

Returns

A Message object.

Return type

Message

property team: Team

Return context team.

Return type

Team

Command

Note

If raise exception in commands object, it will go to on_command_error(ctx, err) event.

Attributes
class slack.commands.Command
func
Type

Callable

Bot

Attributes
Methods
class slack.commands.Bot

This is slack.Client’s subclass.

prefix

Command-prefix.

Type

str

commands

command-name: Command-Obj.

Type

Dict[str, Command]

@command(name=None)

Register command of your client-object.

Parameters

name (str) – command name. If you don’t set, use function name.

@event
event is a decorator that takes a coroutine function and sets

it as an attribute of the class it’s decorating.

Parameters

coro (Coro) – The coroutine function to be decorated.

Return type

The coro function itself.

await connect(ws_url: str) None

connect to slack-API :param ws_url: :type ws_url: str

is_closed() bool

It returns a boolean value.

Returns

The return value is a boolean value.

Return type

bool

await login() None

login as bot

await on_error(event_name, exc: Exception, *args, **kwargs) None

It prints the name of the event that raised the exception, the name of the exception, and the name of the class that the event is in.

Parameters
  • event_name – The name of the event that was raised.

  • exc (Exception) – The exception that was raised.

run() None

A blocking call that abstracts away the event loop initialisation from you. If you want more control over the event loop then this function should not be used. Use start() coroutine or connect() + login(). Roughly Equivalent to:

.. codeblock:: python
try:

loop.run_until_complete(start(*args, **kwargs))

except KeyboardInterrupt:

loop.run_until_complete(close()) # cancel all tasks lingering

finally:

loop.close()

Warning

This function must be the last function to call due to the fact that it is blocking. That means that registration of events or anything being called after this function call will not execute until it returns.

await start() None

start connection.