discord.py用户dms bot出现错误消息 [英] discord.py Error message whenever user dms bot

查看:55
本文介绍了discord.py用户dms bot出现错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此处链接的代码,几个月前,我已经为我的机器人创建了一个自定义前缀.但是,当我最终进入DM响应时,我遇到了一个问题.由于使用自定义前缀,每当有人向我的漫游器发送dms时,我就会收到此错误和回溯信息:

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

起初,我认为这是由于我之前添加的一些代码所致,如果该机器人处于脱机状态,一旦它重新启动,它将在服务器上添加前缀(因为就像我说的那样,m为每个服务器使用自定义前缀).该代码如下所示:

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

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

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.

推荐答案

我建议您做的是在bot DM中设置默认前缀.假设您希望默认前缀为.,将您的 get_prefix 函数更改为:

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.

这篇关于discord.py用户dms bot出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆