【问题标题】:How can I DM someone who has a mutual server with my bot using my .dm command, if possible? (discord.py)如果可能的话,我如何使用我的 .dm 命令与我的机器人拥有共同服务器的人进行 DM? (discord.py)
【发布时间】:2021-08-01 21:29:07
【问题描述】:

当人们想使用我的反馈命令提供有关机器人的反馈时,我希望能够通过机器人将他们 dm 回复,但我的 DM 命令不起作用,即使他们与机器人有一个共同的服务器。当我运行该命令时,我收到此错误:/usr/lib/python3.7/asyncio/events.py:88: RuntimeWarning: coroutine 'Client.fetch_user' was never awaited。谁能帮我这个?这是我的代码:

@client.command(description="This command DMs people and can only be used by the owner.")
@commands.is_owner()
async def dm(ctx, id, *, message):
    user = await client.fetch_user(id)
    await user.send(message)
    return

【问题讨论】:

  • 正如错误所说,你应该等待client.fetch_user,因为它是一个协程
  • 哦,好的,我现在明白了。我会解决的。我还是 dpy 的新手,这就是为什么我有时会搞砸一些愚蠢的事情
  • @ŁukaszKwieciński 我做到了,但是机器人仍然只能在服务器中运行命令的人。你知道为什么吗?
  • 我不太明白,介意改写吗?

标签: python python-3.x discord.py


【解决方案1】:
@client.command(description="This command DMs people and can only be used by the owner.")
@commands.is_owner()
async def dm(ctx, member: discord.Member = None):
    if member == None:
        await ctx.send("Mention the user or provide user id of the user you want me to send a DM to.")

    else:
        embed=discord.Embed(title=f"Sure, What do you want me to send to {member.display_name} ?", description="write anything here")
        await ctx.send(embed=embed)
        def check(m):
            return m.author.id == ctx.author.id
        message = await client.wait_for('message', check=check)
        await ctx.send(f'Message sent to {member.display_name}!')
    
        await member.send(f"{ctx.author.mention} Sent a message - \n{message.content}")

【讨论】:

  • 嘿,Taufeeq!不只是粘贴一个大的代码段,而是使用# 添加 cmets 或添加描述,以便我们知道您在做什么。 Robert 可能不知道 wait_for 是如何工作的,或者可能不知道什么是什么,所以最好尽可能地解释它。祝您编码愉快,并尽您所能进行编辑!
  • 嘿 Bagle,谢谢你的建议:D 我会确保解释而不是仅仅给出代码。我只是想通过提供我的机器人的代码来提供帮助。但我会确保下次解释它:D。谢谢你,祝你有美好的一天!
猜你喜欢
  • 2022-01-14
  • 2019-03-09
  • 1970-01-01
  • 2021-11-14
  • 2023-03-31
  • 2021-06-12
  • 2021-06-13
  • 2019-12-11
相关资源
最近更新 更多