Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法获取discord以在聊天中显示错误_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

Python 无法获取discord以在聊天中显示错误

Python 无法获取discord以在聊天中显示错误,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我试图通过吐出您没有权限{}来让我的机器人告诉用户是否有错误,但当我尝试使用此代码时: @client.command(pass_context = True) async def ban(ctx, member : discord.Member, *, content: str): if ctx.message.author == client.user: return if ctx.message.author.server_permissions.admin

我试图通过吐出
您没有权限{}
来让我的机器人告诉用户是否有错误,但当我尝试使用此代码时:

@client.command(pass_context = True)
async def ban(ctx, member : discord.Member, *, content: str):
    if ctx.message.author == client.user:
        return
    if ctx.message.author.server_permissions.administrator:
        msg = (str(member) + "has been banned for" + str(content)).format(ctx.message)
        await client.send_message(member, content)
        await client.ban(member)
        await client.send_message(ctx.message.channel, msg)
@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)

discord bot管理用户,python控制台中没有错误,如果我删除
@ban.error
部分,我会得到一个太低的权限错误。

检查失败
仅在失败时才会引发。代码中没有检查,因此这种情况永远不会发生。您可以使用以下方法轻松地将
if
语句转换为支票:

可能是错误处理程序正在抑制有价值的错误信息。我们只需调用bot内置的错误处理程序,即可处理任何其他非
CheckFailure
s的错误

from discord.ext.commands import CheckFailure
from discord import Forbidden


@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)
    elif isinstance(error, Forbidden):
        await client.send_message(ctx.message.channel, "I do not have the correct permissions")
    else:
        print(error)
        await client.on_command_error(error, ctx)

此外,它甚至不禁止用户和成员被禁止,因为内容没有出现在服务器聊天中。使用您的代码仍然不会禁止用户,也不会回吐他们被禁止的原因,或者如果有错误,直到相同的结果(顺便说一句,如果bot不是客户端,我只会出错),请立即尝试。如果您的bot没有禁止/发送消息的必要权限,您可能会收到一个
403:probled
错误。我现在在我的代码
回溯过程中收到这些错误(最近一次调用):文件“/usr/lib/python3.6/asyncio/selector\u events.py”,第723行,在_read\u ready data=self.\u sock.recv(self.max\u size)ConnectionResetError:[Errno 104]由对等方重置连接
这些问题(权限过低和连接重置)都无法通过代码解决。你需要获得你的机器人更高的权限。对等连接重置意味着discord正在关闭您与API之间的连接。
from discord.ext.commands import CheckFailure
from discord import Forbidden


@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)
    elif isinstance(error, Forbidden):
        await client.send_message(ctx.message.channel, "I do not have the correct permissions")
    else:
        print(error)
        await client.on_command_error(error, ctx)