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.

Bot

This class need…

  • user-token.

  • bot-token.

  • app-token.

  • prefix.

Prefix is a string for command occur. If you don’t need this, you put prefix=””

Methods
class slack.commands.Bot

This is slack.Client’s subclass.

New in version 1.2.0.

user_token

The your-self token. It must be start ‘xoxp-…’

Type

str

bot_token

The bot token. It must be start ‘xoxb-…’

Type

str

token

App-level token. It is startwith ‘xapp-…’

Changed in version 1.4.0: To optional.

Type

Optional[str]

logger

Logger object.

New in version 1.4.0.

Type

Logger.Logger

prefix

Command-prefix.

Type

str

@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 close() None

Close connection.

property commands: Dict[str, Command]

Return all commands

New in version 1.4.0.

Return type

Dict[:class:`str, Command]

await connect(ws_url: str) None

connect to slack-API

Parameters

ws_url (str) –

get_command(name: str, /) Optional[Command]

Get registered command from name

New in version 1.4.0.

Parameters

name (str) –

Return type

Optional[Command]

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 with websocket. Get teams, channels and member data.

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:

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.

Context

Methods
class slack.commands.Context

A context is a message that is sent to a handler

client
Type

Bot

message

Message object of context.

Type

Message

id

Context ID (equals message ID)

Type

str

channel_id

Context channel ID

Type

str

prefix

Message prefix.

Type

str

property author: Optional[Member]

Message author.

Returns

Message author.

Return type

Optional[Member]

property channel: Channel

Returns context channel.

Returns

Context channel.

Return type

Channel

await delete() DeletedMessage

Delete context message.

Returns

Data of deleted message.

Return type

DeletedMessage

await edit(text: str, is_bot: bool = True)

This function is a coroutine Edit sent message.

New in version 1.4.0.

Parameters
  • text (str) – New message.

  • is_bot (bool) – If my(Bot) message, True.

Returns

message data with edited timestamp.

Return type

Message

await send(text: str = None) Context
await send(view: ViewFrame = None) Context

Send message. Parameters must required text or view.

Parameters
  • text (Optional[str]) –

  • view (Optional[View]) –

Returns

Context object contains message.

Return type

Context

property team: Team

Return context team.

Returns

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

It’s a class that represents a command

New in version 1.2.0.

property callback: Callable

It returns a function that is stored in the object

Returns

The function itself.

Return type

Callable