简体   繁体   English

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

[英]Discord.py — UserNotFound error after attempting to ban user

I'm trying to design a Discord bot that can ban a user across two different servers of mine if I ban them on the main server.我正在尝试设计一个 Discord 机器人,如果我在主服务器上禁止用户,它可以禁止我的两个不同服务器上的用户。 When testing the progress so far, everything seemed to work until it came to actually banning the user.在测试到目前为止的进展时,一切似乎都有效,直到真正禁止用户。 No errors came up, but the user wasn't banned.没有出现错误,但用户没有被禁止。 When I try to test again, I keep getting UserNotFound errors.当我再次尝试测试时,我不断收到 UserNotFound 错误。 I've restarted the bot, re-added it and the test subject user to both servers, and tried to clear the cache, but the bot still doesn't seem to recognize the user exists anymore.我重新启动了机器人,将它和测试对象用户重新添加到两台服务器,并尝试清除缓存,但机器人似乎仍然无法识别用户的存在。

This is the code for the ban command:这是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.')

And this is is the function I ran when trying to ban the test account:这是我在尝试禁止测试帐户时运行的功能:

(If it's any help, I never had UserNotFound errors before running this code.) (如果有任何帮助,在运行此代码之前,我从未遇到过 UserNotFound 错误。)

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

And here's the traceback:这是回溯:

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.

Thanks in advance for any help you can offer, and let me know if you need any more information out of me.预先感谢您提供的任何帮助,如果您需要我提供更多信息,请告诉我。

 TARGET_ID = int(userID) # record target user ID

If I understand it right, you convert userID to integer here.如果我理解正确,您可以在此处将 userID 转换为整数。 However,然而,

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

Target ID shows up as a string on the error.目标 ID 在错误中显示为字符串。 You may want to check it.您可能想检查一下。

The error in the traceback is complaining about the UserConverter.convert() call in your totalban() function which produces the UserNotFound error.在回溯的误差抱怨UserConverter.convert()在您的呼叫totalban()函数产生的UserNotFound错误。 Because this function call causes the error, it means that CANCONFIRMBAN will never be set to true which would explain why no one ends up getting banned.因为这个函数调用会导致错误,这意味着CANCONFIRMBAN永远不会被设置为 true,这可以解释为什么没有人最终被禁止。

According to the documentation , UserConverter.convert() accepts a string argument.根据文档UserConverter.convert()接受一个字符串参数。 Are you certain that the userID argument in totalban() is a string and not an integer value?您确定totalban()中的userID参数是字符串而不是整数值吗?

There is also this note on how the UserConverter is able to convert values to a User object:还有关于UserConverter如何将值转换为 User 对象的注释:

All lookups are via the global user cache.所有查找都是通过全局用户缓存进行的。

  • Are you sure that the internal cache of the bot is ready?你确定机器人的内部缓存已经准备好了?

  • Is fetch_offline_members set to False? fetch_offline_members是否设置为 False? This greatly limits the functionality of bots and can make it impossible to do many things.这极大地限制了机器人的功能,并使许多事情无法做。

  • Have you tried calling the function with name#discrim instead of the user ID?您是否尝试使用name#discrim而不是用户 ID 调用该函数?

It is difficult to help solve the problem with certainty without more information.如果没有更多信息,很难确定地帮助解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM