All pastes #1637668 Raw Edit

erepbot.py

public python v1 · immutable
#1637668 ·published 2009-10-22 16:39 UTC
rendered paste body
#! /usr/bin/env python##  Copyright (C) 2009 by Filip Brcic <brcha@gna.org>##  eRepublik bot using python irclib##  This program is free software: you can redistribute it and/or modify#  it under the terms of the GNU General Public License as published by#  the Free Software Foundation, either version 3 of the License, or#  (at your option) any later version.##  This program is distributed in the hope that it will be useful,#  but WITHOUT ANY WARRANTY; without even the implied warranty of#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the#  GNU General Public License for more details.##  You should have received a copy of the GNU General Public License#  along with this program.  If not, see <http://www.gnu.org/licenses/>.#from ircbot import SingleServerIRCBotfrom irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr, is_channelfrom eRepublik import Citizenclass ERepBot(SingleServerIRCBot):    def __init__(self, channels, nickname, server, port=6667):        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)        self.initial_channels = channels    def on_nicknameinuse(self, c, e):        c.nick(c.get_nickname() + "_")    def on_welcome(self, c, e):        for ch in self.initial_channels:            c.join('#' + ch)    def on_privmsg(self, c, e):        self.do_command(e, e.arguments()[0])    def on_pubmsg(self, c, e):        a = e.arguments()[0].split(":", 1)        if len(a) > 1 and irc_lower(a[0]) == irc_lower(self.connection.get_nickname()):            self.do_command(e, a[1].strip())        elif e.arguments()[0].startswith('!'):            self.do_command(e, e.arguments()[0])        return    def on_dccmsg(self, c, e):        c.privmsg("You said: " + e.arguments()[0])    def on_dccchat(self, c, e):        if len(e.arguments()) != 2:            return        args = e.arguments()[1].split()        if len(args) == 4:            try:                address = ip_numstr_to_quad(args[2])                port = int(args[3])            except ValueError:                return            self.dcc_connect(address, port)    def do_command(self, e, cmd):        nick = nm_to_n(e.source())        c = self.connection        t = e.target()        if not is_channel(t):            t = nick        if cmd == "disconnect" and nick.lower().startswith('hostilian'):            self.disconnect()        elif cmd == "die" and nick.lower().startswith('hostilian'):            self.die()        elif cmd == "stats":            for chname, chobj in self.channels.items():                c.notice(nick, "--- Channel statistics ---")                c.notice(nick, "Channel: " + chname)                users = chobj.users()                users.sort()                c.notice(nick, "Users: " + ", ".join(users))                opers = chobj.opers()                opers.sort()                c.notice(nick, "Opers: " + ", ".join(opers))                voiced = chobj.voiced()                voiced.sort()                c.notice(nick, "Voiced: " + ", ".join(voiced))        elif cmd.startswith('join'):            c.join(cmd.split(' ')[1])        elif cmd.startswith('info') or cmd.startswith('!info'):            name = ' '.join(cmd.split(' ')[1:])            cit = Citizen().loadByName(name)            if cit == None:                cit = Citizen().loadById(name)            if cit == None:                c.notice(t, 'Citizen "' + name + '" doesn\'t exist!')                return            c.notice(t, cit.toString())        elif cmd.startswith('fight') or cmd.startswith('!fight'):            name = ' '.join(cmd.split(' ')[1:])            cit = Citizen().loadByName(name)            if cit == None:                cit = Citizen().loadById(name)            if cit == None:                c.notice(t, nick + ', citizen "' + name + '" doesn\'t exist!')                return            c.notice(t, (cit.getName() + ' at ' + str(cit.getWellness())                            + '%: ' + str(cit.fightCalcStr(cit.getWellness()))))            c.notice(t, (cit.getName() + ' at 100.0%: ' +                            str(cit.fightCalcStr(100.0))))        elif cmd.startswith('link') or cmd.startswith('!link'):            name = ' '.join(cmd.split(' ')[1:])            cit = Citizen().loadByName(name)            if cit == None:                cit = Citizen().loadById(name)            if cit == None:                c.notice(t, nick + ', citizen "' + name + '" doesn\'t exist!')                return            c.notice(t, ('http://www.erepublik.com/en/citizen/profile/' +                                  str(cit.getId())))        elif cmd.startswith('donate') or cmd.startswith('!donate'):            name = ' '.join(cmd.split(' ')[1:])            cit = Citizen().loadByName(name)            if cit == None:                cit = Citizen().loadById(name)            if cit == None:                c.notice(t, nick + ', citizen "' + name + '" doesn\'t exist!')                return            c.notice(t,                     ('http://www.erepublik.com/en/citizen/donate/items/' +                      str(cit.getId())))        elif cmd.startswith('manu') or cmd.startswith('!manu'):            manu = [                'http://www.erepublik.com/en/company/pavicevic-weapon-202709',                'http://www.erepublik.com/en/company/pistolji-na-vodu-2-198907',                'http://www.erepublik.com/en/company/pistolji-na-vodu-197483',                ]            map(lambda x: c.notice(t, x), manu)        elif cmd.startswith('land') or cmd.startswith('!land'):            land = [                'http://www.erepublik.com/en/company/valjevska-pivara-198739',                'http://www.erepublik.com/en/company/iron-q4-2-lic-high-194682',                ]            map(lambda x: c.notice(t, x), land)        else:            c.notice(t, nick + ': command not understood: ' + cmd)def main():    import sys    if len(sys.argv) < 4:        print 'Usage: erepbot <server[:port]> <nickname> <channel> [<channel>+]'        sys.exit(1)    s = sys.argv[1].split(":", 1)    server = s[0]    if len(s) == 2:        try:            port = int(s[1])        except ValueError:            print "Error: Erroneous port."            sys.exit(1)    else:        port = 6667    nickname = sys.argv[2]        channels = sys.argv[3:]        bot = ERepBot(channels, nickname, server, port)    bot.start()if __name__ == "__main__":    main()