向某人发送 dm 时如何修复 TypeError?

时间:2021-04-19 20:28:54

标签: discord.py

我一直在尝试制作一个发送消息的 DM 命令,并由作者提供。 但是,我一直收到此错误:TypeError: send() takes from 1 to 2 positional arguments but 3 were given

这是我现在拥有的代码:

@client.command()
async def sendadm(ctx, user: discord.User, *, message=None):
    if ctx.message.author.id == owner_discord_id:
        message = message or ""
        await user.send(message)
    else:
        await user.send(message, "\n\nSent by {ctx.author}")

1 个答案:

答案 0 :(得分:0)

请注意,User.send() 方法是一个实例方法,因此 self 将被隐式传递,因此您的错误提示给出了 3 个位置参数。 (第一个位置参数是 self。可选的第二个位置参数是 content。)主要问题是您试图将两个不同的值传递给位置参数 content

您似乎正在尝试组合消息字符串和 "\n\nSent by {ctx.author}",因此请参阅下面的“更正”代码。


@client.command()
async def sendadm(ctx, user: discord.User, *, message=None):
    if ctx.message.author.id == owner_discord_id:
        message = message or ""
        await user.send(message)
    else:
        await user.send(f"{message}\n\nSent by {ctx.author}")
相关问题