Home arrow Python arrow Page 3 - IRC on a Higher Level Concluded

Channel Information - Python

If you've been following this series of articles closely, then you already have the skills necessary to develop applications that communicate through IRC. The Python-IRCLib library is easy to learn and use, so putting it to work should not be a difficult task at all. However, the library contains a few more gems that are worth taking a look at, and these gems can make your development process go even more smoothly.

  1. IRC on a Higher Level Concluded
  2. SingleServerIRCBot
  3. Channel Information
  4. Something Practical
By: Peyton McCullough
Rating: starstarstarstarstar / 5
October 26, 2005

print this article



The SingleServerIRCBot class contains some tools that allow us to gather information about a channel because it maintains a list of channels it is currently in. The utilities are provided by the Channel class, and we can access them in our bot.

Let's create an example that uses a few of these tools. The bot we construct will join a channel and send us some basic information about the channel when we request it by messaging “!ChanInfo”:

import ircbot
import time

# Connection information
network = 'irc.freenode.net'
port = 6667
channel = '#irclib'
nick = 'PyTest'
name = 'Python Test'

# Create our bot class
class ExampleBot ( ircbot.SingleServerIRCBot ):

   # When welcomed, join the channel and perform the actions mentioned
   def on_welcome ( self, connection, event ):

      connection.join ( channel )

   def on_pubmsg ( self, connection, event ):

      if event.arguments() [ 0 ].upper() == '!CHANINFO':

         source = event.source().split ( '!' ) [ 0 ]
         channelObject = self.channels [ channel ]

         # Message some statistics
         connection.privmsg ( source, 'Moderated: ' + str ( channelObject.is_moderated() ) )
         connection.privmsg ( source, 'Number of users: ' + str ( len ( channelObject.users() ) ) )
         connection.privmsg ( source, 'Channel operators: ' + str ( len (channelObject.opers() ) ) )

# Create our bot
bot = ExampleBot ( [( network, port )], nick, name )

Most of the above is already familiar to you, so I'll only explain the new content. In the on_pubmsg method, we define channelObject, which is the Channel object that matches channel. From there, we access three methods. The is_moderated method checks to see whether the channel is moderated. The users method returns a list of users in the channel. The opers method returns a list of operators.

Of course, there are a number of other Channel methods that can gather information for our bots. Let's take a look at those methods. Notice how we can get a list of all users and a list of operators in a channel. We can also get a list of voiced users:

voiced = self.channels [ channel ].users()

We can also get information on specific users. To start with, we can check whether a channel has a certain user. A boolean value is returned:

hasUser = self.channels [ channel ].has_user ( 'User_Name' )

If we need to know whether a user has channel operator permissions, we can use the is_oper method to get a boolean value:

isOper = self.channels [ channel ].is_oper ( 'User_Name' )

We can check whether users are voiced or not, too:

isVoiced = self.channels [ channel ].is_voiced ( 'User_Name' )

It may be useful to your application if it knows the channel's modes. We can test for certain modes with the Channel object. For example, let's say we wanted to see if the channel was moderated:

isModerated = self.channels [ channel ].has_mode ('m' )

Of course, you saw the is_moderated method being used before. It is an alternative to the method used above, and the Channel object contains a few more of these. For example, let's say we wanted to check to see if the channel is invite-only:

isInvite = self.channels [ channel ].is_invite_only()

We can also check to see if the channel is secret:

isSecret = self.channels [ channel ].is_secret()

Some channels put a limit on the number of users that can be in a channel. We can check to see whether a channel has such a limit:

isLimited = self.channels [ channel ].has_limit()


Many channels do not allow external messages. The has_allow_external_messages can be used to check this:

acceptsExternal = self.channels [ channel ].has_allow_external_messages()

Some channels have a password, or key, protecting access to the channel. We can check this:

hasKey = self.channels [ channel ].has_key()

Many channels choose to lock their topics. The has_topic_lock can tell us if a channel has such a lock in place:

hasLock = self.channels [ channel ].has_topic_lock()

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: