discord.py 用户 dms bot 时的错误消息
discord.py Error message whenever user dms bot
几个月前,我使用 为我的机器人创建了一个自定义前缀。但是,当我最终进入 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
起初,我认为这是由于我之前添加了一些代码,如果机器人处于离线状态,一旦它恢复正常,它就会将前缀添加到服务器(因为,就像我说的,我正在为每台服务器使用自定义前缀)。此代码如下所示:
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 相关的命令。提前谢谢你。
我建议您做的是在机器人 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.
几个月前,我使用
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
起初,我认为这是由于我之前添加了一些代码,如果机器人处于离线状态,一旦它恢复正常,它就会将前缀添加到服务器(因为,就像我说的,我正在为每台服务器使用自定义前缀)。此代码如下所示:
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 相关的命令。提前谢谢你。
我建议您做的是在机器人 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.