首页 > 解决方案 > Discord.py 向用户发送 dm

问题描述

所以我为我的机器人做了一个 dm 命令,它运行良好,但我不能 dm 用户名中有空格的用户。有谁知道如何做到这一点?

@client.command(pass_context=True)
@commands.is_owner()
async def dm(ctx, user: discord.User, *, message=None):
    message = message or "Message from bot owner"
    embed = discord.Embed(title="Message from bot owner", description=message, color = 0)
    await user.send(embed = embed)

例如,当我试图用空格对用户进行 dm 操作时:=dm Special user#1234我的终端说找不到用户 Special。

标签: pythonpython-3.xdiscord.py

解决方案


你可以做

@client.command(pass_context=True)
@commands.is_owner()
async def dm(ctx, user, *, message=None):
    if message is None:
        await ctx.send("You need to specify the message to send!")
        return
    user = client.fetch_user(user)
    await user.send(embed=discord.Embed(title="Message from bot owner", description=message, color = 0))

推荐阅读