微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Discord.py-尝试禁止用户后出现UserNotFound错误

如何解决Discord.py-尝试禁止用户后出现UserNotFound错误

我正在尝试设计一个discord机器人,如果我将其禁止在我的两个不同服务器上,则可以禁止用户访问我的两个不同服务器。当测试到目前为止的进度时,一切似乎都起作用,直到真正禁止用户使用为止。没有出现错误,但是没有禁止用户。当我尝试再次测试时,我不断收到UserNotFound错误。我已经重新启动了该漫游器,并将它和测试对象用户重新添加到了这两个服务器,并试图清除缓存,但是该漫游器似乎仍然无法识别该用户的存在。

这是ban命令的代码

async def totalban(ctx,userID):
    if ctx.message.author.guild_permissions.administrator: # check if admin
        if ctx.guild.id == GUILD_ID: # if admin,check if right guild
            await ctx.send('Confirmed administrator on proper server.')
            time.sleep(1)
            global TARGET_ID
            TARGET_ID = int(userID) # record target user ID
            converter = UserConverter()
            user = await converter.convert(ctx,userID) # convert arg to User
            username = user.name + '#' + user.discriminator # easy reference in Name#1234 format
            global TARGET_USERNAME
            TARGET_USERNAME = username
            await ctx.send('User ID ' + userID + ' corresponds to ' + username + '.')
            time.sleep(1)
            await ctx.send('To confirm ban of ' + username + ',type **!confirmtotalban ' + username + '** Now.')
            global CANCONFIRMBAN
            CANCONFIRMBAN = True # allow !confirmtotalban command to function
        else:
            await ctx.send('Cannot run command from this server.')
    else:
        await ctx.send('User unauthorized to run command.')

这是我尝试禁止测试帐户时运行的功能

(如果有帮助,在运行此代码之前,我从未遇到UserNotFound错误。)

async def banloop(ctx):
    global TARGET_ID
    for guild in ctx.bot.guilds:
        await guild.ban(TARGET_ID)

这是回溯:

Traceback (most recent call last):
  File "...\Programs\Python\python38\lib\site-packages\discord\ext\commands\bot.py",line 903,in invoke
    await ctx.command.invoke(ctx)
  File "...\Programs\Python\python38\lib\site-packages\discord\ext\commands\core.py",line 859,in invoke
    await injected(*ctx.args,**ctx.kwargs)
  File "...\Programs\Python\python38\lib\site-packages\discord\ext\commands\core.py",line 85,in wrapped
    ret = await coro(*args,**kwargs)
  File "...\example_bot.py",line 41,in totalban
    user = await converter.convert(ctx,userID) # convert arg to User
  File "...\Programs\Python\python38\lib\site-packages\discord\ext\commands\converter.py",line 194,in convert
    raise UserNotFound(argument)
discord.ext.commands.errors.UserNotFound: User "165995850303012864" not found.

在此先感谢您提供的任何帮助,如果您需要我提供更多信息,请告诉我。

解决方法

 TARGET_ID = int(userID) # record target user ID

如果我理解正确,请在此处将userID转换为整数。但是,

discord.ext.commands.errors.UserNotFound: User "165995850303012864" not found.

目标ID显示为错误的字符串。您可能要检查一下。

,

回溯中的错误抱怨您的UserConverter.convert()函数中的totalban()调用会产生UserNotFound错误。因为此函数调用会导致错误,所以这意味着CANCONFIRMBAN永远不会设置为true,这将解释为什么没有人最终被禁止。

根据documentationUserConverter.convert()接受字符串参数。您确定userID中的totalban()参数是一个字符串而不是整数值吗?

还有关于UserConverter如何将值转换为User对象的说明:

所有查找都是通过全局用户缓存进行的。

  • 您确定机器人的内部缓存已准备就绪吗?

  • fetch_offline_members是否设置为False?这极大地限制了机器人的功能,并使其无法做很多事情。

  • 您是否尝试使用name#discrim而不是用户ID调用函数?

没有更多信息就很难确定地解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。