首页 > 解决方案 > 无法使用他们的 ID 向 discord.py 中的用户发送消息

问题描述

我有一个问题。我正在制作一个使用用户 ID 的 mod-mail 机器人。但是当我使用 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")

标签: pythondiscorddiscord.py

解决方案


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")

推荐阅读