【问题标题】:How to fix AttributeError: 'NoneType' object has no attribute 'get' on Discord bot code如何修复 AttributeError:“NoneType”对象在 Discord 机器人代码上没有“get”属性
【发布时间】:2026-01-24 15:10:02
【问题描述】:

我有这个功能

此功能不起作用并打印 AttributeError:“NoneType”对象没有属性“get” 如何解决

        @self.client.event
        async def on_message(message):
            if message.author == self.client.user:
                return
            if self.check_buttons.get(message.author.id, None):
                ans = None
                for i in range(len(self.check_buttons.get(message.author.id, []))):
                    if self.check_buttons[message.author.id][i].lower() == message.content.lower() or message.content.lower() == "#" + str(i + 1):
                        ans = self.check_buttons[message.author.id][i]
                        break
                if ans:
                    self.channel.send_message(message.author.id, ans)
                    self.check_buttons[message.author.id] = []
                else:
                    msg = "You entered an invalid value. Copy (Short command: # and numbers buttons):\n"
                    for i in range(len(self.btns)):
                        msg += str(i + 1) + ". " + self.btns[i] + "\n"
                    self.send_message(message.author.id, msg)
            else:
                self.channel.send_message(message.author.id,
                                          message.content)  # send event (UserID + UserMessage) to channel```

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

if ans = self.check_buttons.get(message.author.id, None) 替换为if ans = self.check_buttons.get(message.author.id) == None:

【讨论】:

    最近更新 更多