コード例 #1
0
ファイル: ircbot.py プロジェクト: rtgdk/inkscape-web
    def handle(self, *args, **options):
        if not hasattr(settings, 'IRCBOT_PID'):
            print "Please set IRCBOT_PID to a file location to enable bot."
            return

        with open(settings.IRCBOT_PID, 'w') as pid:
            pid.write(str(os.getpid()))
        atexit.register(lambda: os.unlink(settings.IRCBOT_PID))

        HeartBeat.objects.filter(name="ircbot").delete()
        self.beat = HeartBeat.objects.create(name="ircbot")

        self.client = BotClient()
        self.commands = list(self.load_irc_modules())
        self.client.start()
        self.connection = self.client.connections[0]

        self.log_status("Server Started!", 0)
        drum = 10  # wait for a minute between beats

        while True:
            try:
                time.sleep(drum)
                self.beat.save()
                assert (self.connection.socket.connected)
                self.ready_commands()
            except KeyboardInterrupt:
                self.client.quit()
                for x, conn in enumerate(self.client.connections):
                    if conn.socket.connected:
                        conn.socket.disconnect()
                self.log_status("Keyboard Interrupt", 1)
                drum = 0.1
            except AssertionError:
                threads = [
                    t for t in threading.enumerate()
                    if t.name != 'MainThread' and t.isAlive()
                ]
                for t in threads:
                    # This is for error tracking when treading is messed up
                    self.log_status(
                        "Thread Locked: %s (Alive:%s, Daemon:%s)" %
                        (t.name, t.isAlive(), t.isDaemon()), -10)

                if not threads:
                    self.log_status("Socket Disconnected", -1)
                    break
                else:
                    drum = 0.1
コード例 #2
0
# -*- coding: utf-8 -*-
"""Example bot code for basic command-based bot using prefix command."""

from easyirc.client.bot import BotClient

# Creates a bot client to write a new bot.
client = BotClient()
pevent = client.events.msgprefix  # alias to avoid verbosity

# Change the prefix to '.'
# Now commands are used like: .help .hello .sayback
pevent.prefix = '.'


@pevent.hook('reply')
def on_reply(context, reply='No message to reply'):
    # 'reply' is a convinient tool to reply sender.
    # If message comes from channel, reply to channel. If message comes from
    # user, reply to user.
    # If message is privmsg, reply as privmsg. If message is notice, reply as
    # notice.
    context.reply(reply)


@pevent.hook('ident')
def on_ident(context, message=None):
    # 'ident' is a user identifier in irc protocol.
    # 'ident' consists with parts of identity: nick, username, host
    context.reply('ident:' + context.ident)
    context.reply('nick:' + context.ident.nick)
    context.reply('username:' + context.ident.username)