[英]discord.py Error message whenever user dms bot
Using the code linked here , I had created a custom prefix for my bot a couple months ago.使用此处链接的代码,几个月前我为我的机器人创建了一个自定义前缀。 However, I ran into an issue when I was finally getting into DM responses.然而,当我最终进入 DM 回复时,我遇到了一个问题。 Due to the custom prefix, I have been receiving this error and traceback whenever someone dms my bot:由于自定义前缀,每当有人 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
At first, I thought this was due to a bit of code I had previously added, where if the bot was offline, once it came back on it would add the prefix to the server (since, like I said, I'm using a custom prefix for each server).起初,我认为这是由于我之前添加的一些代码,如果机器人离线,一旦它回来,它会将前缀添加到服务器(因为,就像我说的,我正在使用每个服务器的自定义前缀)。 This code is as seen below:此代码如下所示:
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
At the time I did not find this to be an issue, but now that I have discovered this error, I realize that I cannot do commands related to DMs until this is fixed.当时我没有发现这是一个问题,但现在我发现了这个错误,我意识到在这个问题修复之前我无法执行与 DM 相关的命令。 Thank you in advance.先感谢您。
Something I would recommend you to do is to make a default prefix in bot DMs.我建议您做的事情是在机器人 DM 中创建一个默认前缀。 Let's say you want the default prefix to be .
假设您希望默认前缀为.
, change your get_prefix
function to: ,将您的get_prefix
function 更改为:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.