Python Discord.py—;尝试禁止用户后出现UserNotFound错误
我正在尝试设计一个Discord机器人,如果我在主服务器上禁止一个用户,它可以在我的两个不同服务器上禁止他们。当测试到目前为止的进展,一切似乎都工作,直到它真正禁止用户。没有出现错误,但用户没有被禁止。当我再次尝试测试时,我不断收到UserNotFound错误。我重新启动了bot,将它和测试主体用户重新添加到两台服务器,并尝试清除缓存,但bot似乎仍然无法识别用户的存在 这是ban命令的代码:Python Discord.py—;尝试禁止用户后出现UserNotFound错误,python,discord,discord.py,python-3.8,Python,Discord,Discord.py,Python 3.8,我正在尝试设计一个Discord机器人,如果我在主服务器上禁止一个用户,它可以在我的两个不同服务器上禁止他们。当测试到目前为止的进展,一切似乎都工作,直到它真正禁止用户。没有出现错误,但用户没有被禁止。当我再次尝试测试时,我不断收到UserNotFound错误。我重新启动了bot,将它和测试主体用户重新添加到两台服务器,并尝试清除缓存,但bot似乎仍然无法识别用户的存在 这是ban命令的代码: async def totalban(ctx, userID): if ctx.message
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错误。)
这是回溯:
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()
调用了生成UserNotFound
错误的totalban()
函数。由于此函数调用会导致错误,这意味着canconfirban
将永远不会设置为true,这将解释为什么没有人最终被禁止
根据,UserConverter.convert()
接受字符串参数。您确定totalban()
中的userID
参数是字符串而不是整数值吗
还有关于UserConverter
如何将值转换为用户对象的说明:
所有查找都是通过全局用户缓存进行的
- 您确定bot的内部缓存已准备就绪吗
是否设置为False?这极大地限制了机器人的功能,并使其无法完成许多事情fetch\u offline\u members
- 您是否尝试过使用
而不是用户ID调用函数name#discrim
如果没有更多信息,很难确定地帮助解决问题。我没有详细查看您的代码,但您是否确保bot有权禁止它们,因为这困扰了我半个星期。bot以禁止权限添加到两台服务器。此玩家的级别是否高于bot在服务器角色中的级别,我遇到的另一个问题不,我认为事实上它们是一样的。全局变量的使用可能是问题的一部分,因为全局变量有一种使代码很难调试的趋势。这是唯一修改TARGET_ID的位置吗?不幸的是,这似乎不是问题所在。该程序显然可以识别任何其他用户,但不是我最初试图禁止的用户。您是否尝试将Discord.member作为参数并尝试以这种方式执行ban命令?感谢您迄今为止的帮助。你会建议如何检查内部缓存是否准备好了?@黄绿色尝试调用CTX.BOT.ISX Read()并在转换之前打印它。此外,试着让你的朋友拥有比BOT稍低的权限,并检查这是否会使错误消失。
discord.ext.commands.errors.UserNotFound: User "165995850303012864" not found.