【问题标题】:Discord.py cant Ban Fetched UserDiscord.py 无法禁止获取的用户
【发布时间】:2021-07-04 21:54:02
【问题描述】:

我试图通过 UserID 禁止某人,但不知何故我无法通过获取用户 ID 来禁止他们。这是错误还是什么?

@client.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, userid):
    member = await client.fetch_user(userid)
    await member.ban()
    await ctx.send("Banned")

错误:

Ignoring exception in command ban:
Traceback (most recent call last):
  File "C:\Users\maddo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\maddo\Desktop\Batbot\main.py", line 58, in ban
    await member.ban()
AttributeError: 'User' object has no attribute 'ban'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\maddo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\maddo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\maddo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'User' object has no attribute 'ban'

【问题讨论】:

  • 为什么要获取 ID?用户不在服务器上了吗?
  • 不,用户在服务器上,我只是想为我的机器人添加 ID Ban 支持

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


【解决方案1】:

您可以使用 discord.Member 作为必需参数,以通过 ID 或提及来禁止用户访问您的服务器。

您应该永远不要为此使用fetch,因为这样您会向 API 发出请求,这可能会在一段时间后导致速率限制。

一个简单的禁止命令可以是:

@client.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member): # ID/mention needed
    await ctx.guild.ban(member) # Get your guild and ban the member
    await ctx.send(f"{member} got banned") # Send a message

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2020-09-25
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多