Core API Reference

This section outlines the core classes of pytmi.

TwitchClient

class pytmi.TwitchClient

Represents a connection to Twitch’s Messaging Interface. Instances of this class can join twitch channels and communicate with users.

event(event_coro)

A decorator that registers an event. The registered function must be a coroutine.

@client.event
async def on_message(message):
    print(f"Received message from {message.author}: {message.content}")
coroutine join(channel: str)

This function is a coroutine.

Joins a twitch channel. Twitch channels use the name of the streamer.

coroutine part(channel: str)

This function is a coroutine.

Leaves a twitch channel. Twitch channels use the name of the streamer.

coroutine run(username=None, password=None, channels=[])

This function is a coroutine.

Log in to twitch, autojoins the given channels, and begins listening for messages.

Since this is a coroutine, it cannot be run from normal code. If you want a simpler version, use run_sync() instead.

run_sync(username=None, password=None, channels=[])

A blocking function that starts executing the bot using the provided connection info.

This function adds the method run() to the event loop, and pauses execution until the client disconnects. Therefore, this function must be performed as the last function.

coroutine send_event(event_name: str, *args, **kwargs)

This function is a coroutine.

Internal function that triggers an event and passes arguments to it. You almost never want to use this.

coroutine send_message(channel: str, message: str)

This function is a coroutine.

Send a chat message to a channel

coroutine send_raw(raw_message: str)

This function is a coroutine.

Internal method to send a raw command message. The messages supported are those in the tmi protocol. You almost never want to use this.

If you want to send a chat message, use send_message() instead.

Event Reference

This is a list of all possible events that can be propogated by TwitchClient.

Events can be listened to by either registering them via the TwitchClient.event() decorator, or by subclassing TwitchClient and overriding the event.

All events must be coroutines.

pytmi.on_ping()

Triggered when the client receives a ping from the server. The library already handles sending PONG back to the server internally.

pytmi.on_raw_message(raw_message)

Triggered when the client retrieves any message from the server, but before it is parsed. This includes such messages such as PING or PRIVMSG.

Parameters:raw_message – A str containing the raw message.
pytmi.on_message(message)

Triggered when the client receives a chat channel message from a user.

Parameters:message – A Message representing the received message.

Data Structures

The following classes have minimal behavior on their own, and are used to encapsulate data supplied by the TwitchClient.

Message

class pytmi.Message

Represents a user message in the twitch message interface.

channel: str
The channel the message was sent from
content: str
The text content of the message
author: User
The User object that sent this message
badges: list
TODO: change badges to objects, then document
bits: int
The number of bits the user sent in this message. This value is 0 if no bits were sent.

User

class pytmi.UserType

Represents a user type of a user in chat

class pytmi.User

Represents a twitch user in a channel.

It is available during certain events, such as when a message arrives in a chat channel.

Note that this is not fully parsed, and much of the data is still raw. This is highly subject to change in future versions of this library.

color: str
The color code as a hexadecimal string (ex: #0D4200)
id: str
The id of the twitch user as a raw string.
name: str
The display name of the user
mod: bool
True if this user is a moderator. False otherwise. It is usually better to check the badges on the Message when checking for permissions.
subscriber: bool
True if this user is a subscriber. False otherwise.
turbo: bool
True if this user has a turbo badge. False otherwise.
userType: UserType
The type of user this object represents.