簡體   English   中英

無法使用用戶的 ID 向 discord.py 中的用戶發送消息

[英]Cannot send message to user in discord.py with their id

我有一個問題。 我正在制作一個使用用戶 ID 的 mod-mail bot。 但是當我使用 id 時,我收到以下錯誤:

raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'

這很奇怪,因為經過研究,我發現這是最好的方法。 目標是將消息發送給用戶。 我查了一下,錯誤不在user_id部分,因為這是正確的 id。 我需要做什么來解決這個問題?

這個命令只是為了測試。 這不是實際的命令

這是我的代碼:

@bot.command()
async def id(ctx):
    # take the id of the user it needs to send the message to
    channel_name = ctx.channel.name
    user_id = channel_name
    # declare the member it needs to send it to
    member = bot.get_user(user_id)

    # printing some things so I can check what It returns
    print (user_id)
    print (member)
    print('------')

    # send the message to the user
    await member.send("Confirmed")

user_id是一個字符串,它應該是一個整數

@bot.command()
async def id(ctx):
    # take the id of the user it needs to send the message to
    channel_name = ctx.channel.name
    user_id = int(channel_name)

    member = bot.get_user(user_id)

    print(user_id)
    print(member)

    await member.send("Confirmed")

發生這種情況是因為bot.get_user()無法檢索Member因為您向它傳遞了實際 ID 的str表示。

在將 user_id 傳遞給函數之前對其進行轉換: bot.get_user(int(user_id))

但是,您為什么要執行所有這些操作,而不僅僅是從ctx提取成員?

錯誤消息告訴您,您嘗試調用不存在的對象的send方法,該對象由NoneType表示。 作為一般方法,您需要檢查調用 send 的位置。 從您的代碼中,我可以找到一個示例:

await member.send("Confirmed")

因此, memberNoneType 這就是member的定義方式

member = bot.get_user(user_id)

並且由於您已明確聲明user_id是正確的,因此從邏輯bot.get_userbot.get_user不會產生由正確的user_id標識的用戶。 user_id是頻道名稱,聽起來很奇怪,因為頻道通常與用戶不同。

為了弄清楚,如果可能,您需要調試get_user 在那里傳遞正確的user_id並查看是否找到它。 您應該能夠在您測試的確切場景中重現該問題。 頻道名稱可能與存儲的user_id值的類型不同。 查看您是否能夠通過 id 獲取正確的用戶。 如果您能夠這樣做,那么它也應該適用於真實場景。

暫無
暫無

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

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