【问题标题】:How to send a user a DM from a bot and catch the response via Discord.py如何从机器人向用户发送 DM 并通过 Discord.py 捕获响应
【发布时间】:2017-10-21 14:04:15
【问题描述】:

这可能是一个愚蠢的问题,但我真的需要弄清楚这一点。我正在努力在我的机器人和 DM 频道之间建立一个接口。我当前的代码是:

if input.startswith('.direct'):
    content = re.sub(r'^\W*\w+\W*', '', message.content)
    await client.send_message(discord.PrivateChannel(User ID), content)

这基本上是尝试通过用户 ID 发送消息,但不起作用。我已经检查了几次 API,但无法弄清楚。我不想要 message.author 或类似的东西。我需要能够根据用户的标签或用户 ID 向用户发送消息。任何帮助表示赞赏,对于奖励积分,如何捕捉响应?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    @client.command(description="send prv messege (only admins)") 
    async def dm(ctx, member:discord.User=None, text =None):
     try:
        if (text == None):
            await ctx.channel.send("You  have to write a text!")
            return
        if (member == ctx.message.author or member == None):
            await ctx.send("""You cannot DM yourself!""") 
    
        message = f"You have been dmed from {ctx.guild.name} for {text}"
        await member.send(message)
        await ctx.channel.send(f"{member} is dmed!")
     except:
        await ctx.send(f"Error dming user {member} (cannot dm owner or bot)")
    

    【讨论】:

    • 欢迎来到 StackOverflow。虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    【解决方案2】:

    您是否厌倦了使用:

    client.start_private_message(user)
    

    ?

    【讨论】:

      【解决方案3】:

      从 id 获取他们的用户对象,然后使用client.send_message(destination, content) 将消息发送给他们。您可以在一行中执行此操作,因为 client.get_user_info(id)(从 ID 返回用户对象)也是一个协程。

      if input.startswith('.direct'):
          content = re.sub(r'^\W*\w+\W*', '', message.content)
          await client.send_message(client.get_user_info(id), content)
      

      【讨论】:

        猜你喜欢
        • 2021-07-15
        • 2021-12-03
        • 2021-12-23
        • 2021-11-14
        • 2019-12-11
        • 2018-05-10
        • 2021-12-24
        • 1970-01-01
        • 2021-06-12
        相关资源
        最近更新 更多