每当用户dms bot时,discord.py错误消息

2024-04-25 04:00:14 发布

您现在位置:Python中文网/ 问答频道 /正文

几个月前,我使用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

起初,我认为这是因为我之前添加了一些代码,如果bot离线,一旦它返回,就会向服务器添加前缀(因为,正如我所说,我为每个服务器使用自定义前缀)。该代码如下所示:

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

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


Tags: injsonmessageprefixliblocalsiteusers
1条回答
网友
1楼 · 发布于 2024-04-25 04:00:14

我建议您在bot-DMs中创建一个默认前缀。假设您希望默认前缀为.,请将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.

相关问题 更多 >