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 # Connection information # Create our bot class # When welcomed, join the channel and perform the actions mentioned connection.join ( channel ) def on_pubmsg ( self, connection, event ): if event.arguments() [ 0 ].upper() == '!CHANINFO': source = event.source().split ( '!' ) [ 0 ] # Message some statistics # Create our bot 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: ... 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: ... If we need to know whether a user has channel operator permissions, we can use the is_oper method to get a boolean value: ... We can check whether users are voiced or not, too: ... 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: ... 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: ... We can also check to see if the 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: ...
Many channels do not allow external messages. The has_allow_external_messages can be used to check this: ... Some channels have a password, or key, protecting access to the channel. We can check this: ... Many channels choose to lock their topics. The has_topic_lock can tell us if a channel has such a lock in place: ...
blog comments powered by Disqus |
|
|
|
|
|
|
|