discord.py Error message whenever user dms bot

☆樱花仙子☆ 提交于 2021-01-29 10:36:52

问题


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. Due to the custom prefix, I have been receiving this error and traceback whenever someone dms my bot:

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. Thank you in advance.


回答1:


Something I would recommend you to do is to make a default prefix in bot DMs. Let's say you want the default prefix to be ., change your get_prefix function to:

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.


来源:https://stackoverflow.com/questions/65333249/discord-py-error-message-whenever-user-dms-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!