【问题标题】:Cannot get discord to show error in chat无法在聊天中显示错误
【发布时间】:2018-12-17 18:31:01
【问题描述】:

我试图让我的机器人通过吐出You do not have the permissions {} 来告诉用户是否有错误但是当我尝试使用此代码时:

@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 dms 用户,python 控制台中没有错误,如果我删除 @ban.error 部分,我会得到一个权限错误太低。

【问题讨论】:

  • 而且它甚至没有禁止人并且该成员已被禁止因为内容没有出现在服务器聊天中

标签: python python-3.x discord discord.py


【解决方案1】:

CheckFailure 只有在 check 失败时才会被提升。您的代码中没有检查,所以这永远不会发生。您可以使用 commands.has_permission 轻松地将您的 if 语句转换为支票:

@client.command(pass_context = True)
@has_permissions(administrator=True)
async def ban(ctx, member : discord.Member, *, content: str):
    msg = "{} has been banned for {}".format(ctx.message.author.mention, content)
    await client.send_message(member, content)
    await client.ban(member)
    await client.send_message(ctx.message.channel, msg)

错误处理程序可能会隐藏有价值的错误信息。对于不是CheckFailures 的任何其他错误,我们可以调用内置错误处理的机器人

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)

【讨论】:

  • 使用你的代码仍然不会禁止用户或吐出他们被禁止的原因或是否有错误
  • 仍然是相同的结果(顺便说一句,如果机器人不是客户端,我只会收到错误)
  • 立即尝试。如果您的机器人没有必要的权限来禁止/发送消息,您可能会收到 403: FORBIDDEN 错误。
  • 我现在在我的代码中遇到这些错误Traceback (most recent call last): File "/usr/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready data = self._sock.recv(self.max_size) ConnectionResetError: [Errno 104] Connection reset by peer
  • 这些问题(权限太低和连接重置)都不是代码可以解决的。您需要让您的机器人获得更高的权限。 peer 重置连接意味着 discord 正在关闭你和 API 之间的连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2020-07-04
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多