1

所以,我最近一直在研究 IRC 机器人。作为反垃圾邮件功能的一部分,它存储了某人最后一次做某事的时间。它用于初始化 WHO 回复中的用户列表,我一直在使用 RPL_NAMREPLY。

它几乎可以工作。但是,我有一个看起来很奇怪的问题..

import time

# ...

    def irc_unknown(self, prefix, command, params):
        """Handle packets that aren't handled by the library."""

        # Prefix: asimov.freenode.net
        # Command: RPL_BANLIST
        # Params: ['MCBans_Testing', '#mcbans-test', 'a!*@*', 'gdude2002!g@unaffiliated/gdude2002', '1330592882']

        self.runHook("unknownMessage", {"prefix": prefix, "command": command, "params": params})

        if command == "RPL_BANLIST":
            channel = params[1]
            mask = params[2]
            owner = params[3]
            time = params[4]

            if channel not in self.banlist.keys():
                done = {"done": False, "total": 1}
                banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask,
                           "channel": channel}
                done[mask] = banmask
                self.banlist[channel] = done

            else:
                if not self.banlist[channel]["done"]:
                    banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask,
                               "channel": channel}
                    self.banlist[channel][mask] = banmask
                    self.banlist[channel]["total"] += 1

                else:
                    done = {"done": False, "total": 1}
                    banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask,
                               "channel": channel}
                    done[mask] = banmask
                    self.banlist[channel] = done

        elif command == "RPL_ENDOFBANLIST":
            channel = params[1]

            if channel in self.banlist.keys():
                self.banlist[channel]["done"] = True
            else:
                self.banlist[channel] = {"done": True, "total": 0}

            self.prnt("|= Got %s bans for %s." % (self.banlist[channel]["total"], channel))

            if self.is_op(channel, self.nickname):
                stuff = self.banlist[channel].keys()
                stuff.remove("done")
                stuff.remove("total")

                for element in stuff:
                    if stuff == "*!*@*":
                        self.send_raw("KICK %s %s :Do not set such ambiguous bans!" % (
                        channel, self.banlist[channel][element]["owner"]))
                        self.send_raw("MODE %s -b *!*@*" % channel)
                        self.send_raw(
                            "MODE %s +b *!*@%s" % (channel, self.banlist[channel][element]["ownerhost"].split("@")[1]))
                    else:
                        self.checkban(channel, element, self.banlist[channel][element]["owner"])

        elif command == "RPL_NAMREPLY":
            me, status, channel, names = params
            users = names.split()
            ranks = "+%@&~"

            if not channel in self.chanlist.keys():
                self.chanlist[channel] = {}

            for element in users:
                rank = ""

                for part in ranks:
                    if part in element:
                        rank = rank + part
                    element = element.strip(part)

                done = { 'server': prefix,
                         'status': rank,
                         'last_time': float( time.time() - 0.25 ) }
                self.chanlist[channel][element] = done

            print "Names for %s%s: %s" % (status, channel, names)

        elif command == "RPL_ENDOFNAMES":
            me, channel, message = params
            ops = 0
            voices = 0
            opers = 0
            aways = 0

            for element in self.chanlist[channel].values():
                status = element["status"]
                if "+" in status:
                    voices += 1
                if "@" in status:
                    ops += 1
                if "*" in status:
                    opers += 1
                if "G" in status:
                    aways += 1
            print("|= %s users on %s (%s voices, %s ops, %s opers, %s away)" % (
            len(self.chanlist[channel]), channel, voices, ops, opers, aways))
            print "[%s] (%s) %s" % (prefix, command, params)

        else:
            print "[%s] (%s) %s" % (prefix, command, params)

有了它,我收到以下错误..

  File "C:\Users\Gareth\Documents\GitHub\McBlockit---Helpbot\system\irc.py", line 1422, in irc_unknown
    done = {"server": prefix, "status": rank, "last_time": float(time.time() - 0.25)}
exceptions.UnboundLocalError: local variable 'time' referenced before assignment

如您所见,我在文件顶部导入时间。该代码在另一个函数中运行良好。有任何想法吗?

4

1 回答 1

7

您在函数的语句中time定义了一个局部变量:if

        time = params[4]

因为它在if语句中,所以没有为其分配任何内容,但是由于该变量,导入的time模块在您的函数中不可用。

重命名该变量以解决冲突。

于 2012-10-20T15:31:56.040 回答