簡體   English   中英

Discord.py — 嘗試禁止用戶后出現 UserNotFound 錯誤

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

我正在嘗試設計一個 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,這可以解釋為什么沒有人最終被禁止。

根據文檔UserConverter.convert()接受一個字符串參數。 您確定totalban()中的userID參數是字符串而不是整數值嗎?

還有關於UserConverter如何將值轉換為 User 對象的注釋:

所有查找都是通過全局用戶緩存進行的。

  • 你確定機器人的內部緩存已經准備好了?

  • fetch_offline_members是否設置為 False? 這極大地限制了機器人的功能,並使許多事情無法做。

  • 您是否嘗試使用name#discrim而不是用戶 ID 調用該函數?

如果沒有更多信息,很難確定地幫助解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM