Created
February 23, 2016 14:33
-
-
Save UVClay/a33c8fcbed1ca10f2f46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# | |
# Example program using irc.bot. | |
# | |
# Joel Rosdahl <joel@rosdahl.net> | |
"""A simple example bot. | |
This is an example bot that uses the SingleServerIRCBot class from | |
irc.bot. The bot enters a channel and listens for commands in | |
private messages and channel traffic. Commands in channel messages | |
are given by prefixing the text by the bot name followed by a colon. | |
It also responds to DCC CHAT invitations and echos data sent in such | |
sessions. | |
The known commands are: | |
stats -- Prints some channel information. | |
disconnect -- Disconnect the bot. The bot will try to reconnect | |
after 60 seconds. | |
die -- Let the bot cease to exist. | |
dcc -- Let the bot invite you to a DCC CHAT connection. | |
""" | |
import configparser | |
import irc.bot | |
import irc.strings | |
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr | |
class TestBot(irc.bot.SingleServerIRCBot): | |
def __init__(self, channel, nickname, server, port=6667): | |
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) | |
self.channel = channel | |
def on_nicknameinuse(self, c, e): | |
c.nick(c.get_nickname() + "_") | |
def on_welcome(self, c, e): | |
c.join(self.channel) | |
global buffernick | |
global buffermsg | |
global radbuffer | |
buffernick = 'wew' | |
buffermsg = 'wew' | |
radbuffer = 'wew' | |
def on_privmsg(self, c, e): | |
self.do_command(e, e.arguments[0]) | |
def on_pubmsg(self, c, e): | |
global buffernick | |
global buffermsg | |
global radbuffer | |
print('buffernick 1: ' + buffernick) | |
print('buffermsg 1: ' + buffermsg) | |
print(e) | |
a = e.arguments[0].split("!", 1) | |
if len(a) > 1: | |
self.do_command(e, a[1].strip()) | |
buffernick = e.source.nick | |
buffermsg = e.arguments[0] | |
print('buffernick 2: ' + buffernick) | |
print('buffermsg 2: ' + buffermsg) | |
if e.source == 'Radbot!Radbot@krad-no7d07.house': | |
radbuffer == e.arguments[0] | |
return | |
def do_command(self, e, cmd): | |
nick = e.source.nick | |
c = self.connection | |
global buffernick | |
global buffermsg | |
global radbuffer | |
if cmd == "disconnect": | |
if nick == 'clay' or nick == 'ice': | |
self.disconnect() | |
elif cmd == "die": | |
if nick == 'clay' or nick == 'ice': | |
self.die() | |
elif cmd == "stats": | |
if nick == 'clay' or nick == 'ice': | |
for chname, chobj in self.channels.items(): | |
c.privmsg(self.channel, "--- Channel statistics ---") | |
c.privmsg(self.channel, "Channel: " + chname) | |
users = sorted(chobj.users()) | |
c.privmsg(self.channel, "Users: " + ", ".join(users)) | |
opers = sorted(chobj.opers()) | |
c.privmsg(self.channel, "Opers: " + ", ".join(opers)) | |
voiced = sorted(chobj.voiced()) | |
c.privmsg(self.channel, "Voiced: " + ", ".join(voiced)) | |
elif cmd == "quote": | |
if buffernick and buffermsg == 'wew': | |
c.privmsg(self.channel, "No quote stored") | |
elif buffermsg == '!quote': | |
c.privmsg(self.channel, "No quote stored") | |
else: | |
c.privmsg(self.channel, "Quote: " + buffernick + ": " + buffermsg) | |
elif cmd == "radquote": | |
if radbuffer == 'wew': | |
c.privmsg(self.channel, "No quote stored") | |
else: | |
c.privmsg(self.channel, "RadQuote: " + radbuffer) | |
else: | |
c.privmsg(self.channel, "Not understood: " + cmd) | |
def main(): | |
config = configparser.ConfigParser() | |
config.read('bot.conf') | |
network = config['main']['Network'] | |
server = config[network]['Address'] | |
channel = config[network]['Channel'] | |
nickname = config['main']['Nick'] | |
ssl = config[network]['SSL'] | |
port = 6667 | |
bot = TestBot(channel, nickname, server, port) | |
bot.start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment