pytwitcherapi.chat.IRCClient

class pytwitcherapi.chat.IRCClient(session, channel, queuesize=100)[source]

Bases: irc.client.SimpleIRCClient

Simple IRC client which can connect to a single pytwitcherapi.Channel.

You need an authenticated session with scope chat_login. Call IRCClient.process_forever() to start the event loop. This will block the current thread though. Calling IRCClient.shutdown() will stop the loop.

There are a lot of methods that can make the client send commands while the client is in its event loop. These methods are wrapped ones of irc.client.ServerConnection. They will always use IRCClient.out_connection!

You can implement handlers for all sorts of events by subclassing and creating a method called on_<event.type>. Note that IRCClient.out_connection will only get to the IRCClient.on_welcome() event (and then join a channel) and the IRCClient.on_join() event. For all other events, the IRCClient.in_connection will handle it and the other one will ignore it. This behaviour is implemented in IRCCLient._dispatcher()

Little example with threads. Change input to raw_input for python 2:

import threading

from pytwitcherapi import chat

session = ...  # we assume an authenticated TwitchSession
channel = session.get_channel('somechannel')
client = chat.IRCClient(session, channel)
t = threading.Thread(target=client.process_forever,
                     kwargs={'timeout': 0.2})
t.start()

try:
    while True:
        m = input('Send Message:')
        if not m: break;
        # will be processed in other thread
        client.send_msg(m)
finally:
    client.shutdown()
    t.join()
__init__(session, channel, queuesize=100)[source]

Initialize a new irc client which can connect to the given channel.

Parameters:
  • session (pytwitcherapi.TwitchSession) – a authenticated session. Used for quering the right server and the login username.
  • channel (pytwitcherapi.Channel) – a channel
  • queuesize (int) – The queuesize for storing messages in IRCClient.messages. If 0, unlimited size.
Raises:

exceptions.NotAuthorizedError

Methods

__init__(session, channel[, queuesize]) Initialize a new irc client which can connect to the given channel.
action(*args, **kwargs) Send a CTCP ACTION command.
admin(*args, **kwargs) Send an ADMIN command.
cap(*args, **kwargs) Send a CAP command according to the spec.
connect(*args, **kwargs) Connect using the underlying connection
ctcp(*args, **kwargs) Send a CTCP command.
ctcp_reply(*args, **kwargs) Send a CTCP REPLY command.
dcc_connect(address, port[, dcctype]) Connect to a DCC peer.
dcc_listen([dcctype]) Listen for connections from a DCC peer.
globops(*args, **kwargs) Send a GLOBOPS command.
info(*args, **kwargs) Send an INFO command.
invite(*args, **kwargs) Send an INVITE command.
ison(*args, **kwargs) Send an ISON command.
join(*args, **kwargs) Send a JOIN command.
kick(*args, **kwargs) Send a KICK command.
links(*args, **kwargs) Send a LINKS command.
list(*args, **kwargs) Send a LIST command.
lusers(*args, **kwargs) Send a LUSERS command.
mode(*args, **kwargs) Send a MODE command.
motd(*args, **kwargs) Send an MOTD command.
names(*args, **kwargs) Send a NAMES command.
negotiate_capabilities(connection) Send IRCClient.capabilities to the server.
nick(*args, **kwargs) Send a NICK command.
notice(*args, **kwargs) Send a NOTICE command.
on_privmsg(connection, event) Handle the private message event
on_pubmsg(connection, event) Handle the public message event
on_welcome(connection, event) Handle the welcome event
oper(*args, **kwargs) Send an OPER command.
part(*args, **kwargs) Send a PART command.
pass_(*args, **kwargs) Send a PASS command.
ping(*args, **kwargs) Send a PING command.
pong(*args, **kwargs) Send a PONG command.
privmsg(*args, **kwargs) Send a PRIVMSG command.
privmsg_many(*args, **kwargs) Send a PRIVMSG command to multiple targets.
quit(*args, **kwargs) Send a QUIT command.
send_msg(message) Send the given message to the channel
send_raw(*args, **kwargs) Send raw string to the server.
squit(*args, **kwargs) Send an SQUIT command.
start() Start the IRC client.
stats(*args, **kwargs) Send a STATS command.
store_message(connection, event) Store the message of event in IRCClient.messages.
time(*args, **kwargs) Send a TIME command.
topic(*args, **kwargs) Send a TOPIC command.
trace(*args, **kwargs) Send a TRACE command.
user(*args, **kwargs) Send a USER command.
userhost(*args, **kwargs) Send a USERHOST command.
users(*args, **kwargs) Send a USERS command.
version(*args, **kwargs) Send a VERSION command.
wallops(*args, **kwargs) Send a WALLOPS command.
who(*args, **kwargs) Send a WHO command.
whois(*args, **kwargs) Send a WHOIS command.
whowas(*args, **kwargs) Send a WHOWAS command.

Attributes

capabilities List of irc capabilities
channel The channel to connect to.
reactor_class

The reactor class which dispatches events

alias of Reactor3

capabilities = [':twitch.tv/membership', ':twitch.tv/commands', ':twitch.tv/tags']

List of irc capabilities

in_connection = None

Connection that receives messages

out_connection = None

Connection that sends messages

session = None

an authenticated session. Used for quering the right server and the login username.

login_user = None

The user that is used for logging in to the chat

shutdown = None

Call this method for shutting down the client. This is thread safe.

process_forever = None

Call this method to process messages until shutdown() is called.

Parameters:timeout (float) – timeout for waiting on data in seconds
messages = None

A queue which stores all private and public pytwitcherapi.chat.message.Message3. Usefull for accessing messages from another thread.

channel

The channel to connect to. When setting the channel, automatically connect to it. If channel is None, disconnect.

on_welcome(connection, event)[source]

Handle the welcome event

Automatically join the channel.

Parameters:
Returns:

None

negotiate_capabilities(connection)[source]

Send IRCClient.capabilities to the server.

Parameters:connection (irc.client.ServerConnection) – the connection to use for sending
Returns:None
Return type:None
Raises:None
store_message(connection, event)[source]

Store the message of event in IRCClient.messages.

Parameters:
Returns:

None

on_pubmsg(connection, event)[source]

Handle the public message event

This stores the message in IRCClient.messages via IRCClient.store_message().

Parameters:
Returns:

None

on_privmsg(connection, event)[source]

Handle the private message event

This stores the message in IRCClient.messages via IRCClient.store_message().

Parameters:
Returns:

None

send_msg(message)[source]

Send the given message to the channel

This is a convenience method for IRCClient.privmsg(), which uses the current channel as target. This method is thread safe and can be called from another thread even if the client is running in IRCClient.process_forever().

Parameters:message (str) – The message to send
Returns:None
Return type:None
Raises:None
action(*args, **kwargs)

Send a CTCP ACTION command.

admin(*args, **kwargs)

Send an ADMIN command.

cap(*args, **kwargs)

Send a CAP command according to the spec.

Arguments:

subcommand – LS, LIST, REQ, ACK, CLEAR, END args – capabilities, if required for given subcommand

Example:

.cap(‘LS’) .cap(‘REQ’, ‘multi-prefix’, ‘sasl’) .cap(‘END’)
ctcp(*args, **kwargs)

Send a CTCP command.

ctcp_reply(*args, **kwargs)

Send a CTCP REPLY command.

globops(*args, **kwargs)

Send a GLOBOPS command.

info(*args, **kwargs)

Send an INFO command.

invite(*args, **kwargs)

Send an INVITE command.

ison(*args, **kwargs)

Send an ISON command.

Arguments:

nicks – List of nicks.
join(*args, **kwargs)

Send a JOIN command.

kick(*args, **kwargs)

Send a KICK command.

Send a LINKS command.

list(*args, **kwargs)

Send a LIST command.

lusers(*args, **kwargs)

Send a LUSERS command.

mode(*args, **kwargs)

Send a MODE command.

motd(*args, **kwargs)

Send an MOTD command.

names(*args, **kwargs)

Send a NAMES command.

nick(*args, **kwargs)

Send a NICK command.

notice(*args, **kwargs)

Send a NOTICE command.

oper(*args, **kwargs)

Send an OPER command.

part(*args, **kwargs)

Send a PART command.

pass_(*args, **kwargs)

Send a PASS command.

ping(*args, **kwargs)

Send a PING command.

pong(*args, **kwargs)

Send a PONG command.

privmsg(*args, **kwargs)

Send a PRIVMSG command.

privmsg_many(*args, **kwargs)

Send a PRIVMSG command to multiple targets.

quit(*args, **kwargs)

Send a QUIT command.

send_raw(*args, **kwargs)

Send raw string to the server.

The string will be padded with appropriate CR LF.

squit(*args, **kwargs)

Send an SQUIT command.

stats(*args, **kwargs)

Send a STATS command.

time(*args, **kwargs)

Send a TIME command.

topic(*args, **kwargs)

Send a TOPIC command.

trace(*args, **kwargs)

Send a TRACE command.

user(*args, **kwargs)

Send a USER command.

userhost(*args, **kwargs)

Send a USERHOST command.

users(*args, **kwargs)

Send a USERS command.

version(*args, **kwargs)

Send a VERSION command.

wallops(*args, **kwargs)

Send a WALLOPS command.

who(*args, **kwargs)

Send a WHO command.

whois(*args, **kwargs)

Send a WHOIS command.

whowas(*args, **kwargs)

Send a WHOWAS command.