【问题标题】:How to make a discord.py bot private/direct message someone who's not the author?如何使 discord.py bot 私人/直接消息不是作者的人?
【发布时间】:2018-08-12 09:01:39
【问题描述】:

假设我想制作一个具有“poke”功能的机器人(也就是当有人说“!poke @user#0000”时向用户发送一个说“Boop”的 pm),我该怎么做?当我这样做时效果很好:

@bot.command(pass_context=True)
async def poke(ctx, message):
    await client.send_message(ctx.message.author, 'boop')

但前提是我想戳消息的作者。我想戳那些被@'d 的人。

我知道 discord.py 文件说我可以使用这个:

start_private_message(user)

但我不知道用什么代替用户。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    我可能会删除这篇文章,但最近的版本应该是这样的:

    @bot.command():
    async def poke(ctx, user: discord.Member=None):
    
        if user is None:
            await ctx.send("Incorrect Syntax:\nUsage: `!poke [user]`")
    
        await user.send("boop")
    

    if user is None: 块是可选的,但如果您希望在未正确使用命令时显示错误消息,则该块很有用。

    【讨论】:

      【解决方案2】:

      其实比这更简单

      @bot.command(pass_context=True)
      async def poke(ctx, member: discord.Member):
          await bot.send_message(member, 'boop')
      

      send_message 包含私人消息的逻辑,因此您不必自己使用start_private_message: discord.Member 称为转换器,描述为 in the documentation here

      【讨论】:

        猜你喜欢
        • 2018-01-13
        • 2019-09-19
        • 2021-12-25
        • 1970-01-01
        • 2018-09-19
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 2019-06-22
        相关资源
        最近更新 更多