【问题标题】:discord.py Error message whenever user dms bot每当用户 dms bot 时出现 discord.py 错误消息
【发布时间】:2021-03-27 16:52:58
【问题描述】:

几个月前,我使用the code linked here 为我的机器人创建了一个自定义前缀。然而,当我最终进入 DM 回复时,我遇到了一个问题。由于自定义前缀,每当有人 dms 我的机器人时,我都会收到此错误和回溯:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\bagle\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 802, in get_prefix
    ret = list(ret)
TypeError: 'NoneType' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\bagle\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\bagle\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 943, in on_message
    await self.process_commands(message)
  File "C:\Users\bagle\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 939, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\bagle\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 853, in get_context
    prefix = await self.get_prefix(message)
  File "C:\Users\bagle\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 810, in get_prefix
    "returning either of these, not {}".format(ret.__class__.__name__))
TypeError: command_prefix must be plain string, iterable of strings, or callable returning either of these, not NoneType

起初,我认为这是由于我之前添加的一些代码,如果机器人离线,一旦它回来,它就会将前缀添加到服务器(因为,就像我说的,我' m 为每个服务器使用自定义前缀)。这段代码如下所示:

def get_prefix(client, message):
    try:
        with open('prefixes.json', 'r') as f:
            prefixes = json.load(f)
            return prefixes[str(message.guild.id)]
        
    except KeyError: # if the guild's prefix cannot be found in 'prefixes.json'
        with open('prefixes.json', 'r') as k:
            prefixes = json.load(k)
        prefixes[str(message.guild.id)] = 'bl!'

        with open('prefixes.json', 'w') as j:
            json.dump(prefixes, j, indent = 4)

        with open('prefixes.json', 'r') as t:
            prefixes = json.load(t)
            return prefixes[str(message.guild.id)]
        
    except: # I added this when I started getting dm error messages
        pass

当时我没有发现这是一个问题,但现在我发现了这个错误,我意识到在这个问题得到解决之前我无法执行与 DM 相关的命令。提前谢谢你。

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

    我建议您做的事情是在机器人 DM 中创建一个默认前缀。假设您希望默认前缀为 .,请将您的 get_prefix 函数更改为:

    def get_prefix(client, message):
        try:
            with open('prefixes.json', 'r') as f:
                prefixes = json.load(f)
                return prefixes[str(message.guild.id)]
            
        except KeyError: # if the guild's prefix cannot be found in 'prefixes.json'
            with open('prefixes.json', 'r') as k:
                prefixes = json.load(k)
            prefixes[str(message.guild.id)] = 'bl!'
    
            with open('prefixes.json', 'w') as j:
                json.dump(prefixes, j, indent = 4)
    
            with open('prefixes.json', 'r') as t:
                prefixes = json.load(t)
                return prefixes[str(message.guild.id)]
            
        except: # I added this when I started getting dm error messages
            return '.' # This will return "." as a prefix. You can change it to any default prefix.
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2021-06-17
      • 2020-10-31
      • 1970-01-01
      • 2021-09-30
      • 2021-01-09
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多