我在转让之前被引用了

时间:2018-01-09 17:24:41

标签: python discord.py

我发现了一个我似乎无法修复的错误,因为我之前添加了一个更新错误是在分配之前引用了局部变量我不确定这里的问题是什么?有人可以帮忙吗?

line 243, in on_reaction_add
if has_message is not None:
UnboundLocalError: local variable 'has_message' referenced before assignment

代码

 async def on_reaction_add(self, reaction, user):
        server = reaction.message.server
        msg = reaction.message
        if server.id not in self.settings:
            return
        if msg.channel.id in self.settings[server.id]["ignore"]:
            return
        if not await self.check_roles(user, msg.author, server):
            return
        react =self.settings[server.id]["emoji"]
        if react in str(reaction.emoji):
            threshold = self.settings[server.id]["threshold"]
            count = await self.get_count(server, msg)
            if await self.check_is_posted(server, msg):
                channel = self.bot.get_channel(self.settings[server.id]["channel"])
                msg_id, count = await self.get_posted_message(server, msg)
                if msg_id is not None:
                    msg_edit = await self.bot.get_message(channel, msg_id)
                    await self.bot.edit_message(msg_edit, new_content="{} **#{}**".format(reaction.emoji, count-1))
                    return
            if count < threshold and threshold != 0:
                store = {"original_message":msg.id, "new_message":None,"count":count+1}
                for message in self.settings[server.id]["messages"]:
                    has_message = None
                    if msg.id == message["original_message"]:
                        has_message = message
                if has_message is not None:
                    self.settings[server.id]["messages"].remove(has_message)
                    self.settings[server.id]["messages"].append(store)
                    dataIO.save_json("data/star/settings.json", self.settings)
                else:
                    self.settings[server.id]["messages"].append(store)
                    dataIO.save_json("data/star/settings.json", self.settings)
                return
            if threshold == 0:
                count = 2
            # else:

这行代码:

if has_message is not None:
    self.settings[server.id]["messages"].remove(has_message)
    self.settings[server.id]["messages"].append(store)
    dataIO.save_json("data/star/settings.json", self.settings)

1 个答案:

答案 0 :(得分:1)

基本上你没有在内存中初始化变量has_message。 只需添加就好。

async def on_reaction_add(self, reaction, user):
        has_message = None
        server = reaction.message.server
        msg = reaction.message
        if server.id not in self.settings:
            return
        if msg.channel.id in self.settings[server.id]["ignore"]:
            return
        if not await self.check_roles(user, msg.author, server):
            return
        react =self.settings[server.id]["emoji"]
        if react in str(reaction.emoji):
            threshold = self.settings[server.id]["threshold"]
            count = await self.get_count(server, msg)
            if await self.check_is_posted(server, msg):
                channel = self.bot.get_channel(self.settings[server.id]["channel"])
                msg_id, count = await self.get_posted_message(server, msg)
                if msg_id is not None:
                    msg_edit = await self.bot.get_message(channel, msg_id)
                    await self.bot.edit_message(msg_edit, new_content="{} **#{}**".format(reaction.emoji, count-1))
                    return
            if count < threshold and threshold != 0:
                store = {"original_message":msg.id, "new_message":None,"count":count+1}
                for message in self.settings[server.id]["messages"]:
                    has_message = None
                    if msg.id == message["original_message"]:
                        has_message = message
                if has_message is not None:
                    self.settings[server.id]["messages"].remove(has_message)
                    self.settings[server.id]["messages"].append(store)
                    dataIO.save_json("data/star/settings.json", self.settings)
                else:
                    self.settings[server.id]["messages"].append(store)
                    dataIO.save_json("data/star/settings.json", self.settings)
                return
            if threshold == 0:
                count = 2
            # else: