【问题标题】:Python - DM a User Discord BotPython - DM 一个用户 Discord 机器人
【发布时间】:2019-02-19 22:22:37
【问题描述】:

我正在使用 Python 开发 User Discord Bot。如果 bot 所有者键入 !DM @user,那么 bot 将 DM 所有者提到的用户。

@client.event
async def on_message(message):
    if message.content.startswith('!DM'):
        msg = 'This Message is send in DM'
        await client.send_message(message.author, msg)

【问题讨论】:

    标签: python discord bots discord.py dm


    【解决方案1】:

    我注意到我放入我的代码行的每个代码都不能完全工作,所以我添加了我的自己的位到他们并且成功了!将此添加到您的机器人代码时,不要忘记在 bot name here 的位置添加机器人的名称。它只会 DM 发送它的人,但您可以每天更改它所说的内容,让使用该命令的每个人都感到惊讶。它每次都对我有用。

    @client.command()
    async def botdm(ctx):
      await ctx.message.author.send('hi my name is *bot name here* and i am a bot!')
    

    【讨论】:

      【解决方案2】:
      @bot.command()
      async def dm(ctx, user: discord.User, *, message=None):
          if message == None:
            message = "Hi!"
          embed = make_embed(title=f"Sent by {user}", desc=message)
          await user.send(embed=embed)
          await ctx.send("Message sent!")```
      

      【讨论】:

        【解决方案3】:

        我过去使用过这个命令,我认为它最适合我:

        @bot.command(pass_context=True)
        async def mall(ctx, *, message):
          await ctx.message.delete()
          for user in ctx.guild.members:
            try:
              await user.send(message)
              print(f"Successfully DMed users!")
            except:
              print(f"Unsuccessfully DMed users, try again later.")
        

        【讨论】:

          【解决方案4】:

          自从大迁移到 v1.0,send_message 不再存在。
          相反,他们在每个端点(成员、公会等)上都有migrated to .send()

          v1.0 的一个例子是:

          async def on_message(self, message):
              if message.content == '!verify':
                  await message.author.send("Your message goes here")
          

          这会 DM !verify 的发件人。同样明智的是,您可以这样做:

          for guild in client.guilds:
              for channel in guild.channels:
                  channel.send("Hey yall!")
          

          如果您想向您的所有服务器和机器人所在的所有频道发送“hi yall”消息。

          由于可能不完全清楚(根据评论判断),棘手的部分可能是从客户端/会话获取用户身份句柄。如果您需要向尚未发送消息的用户发送消息,并且在on_message 事件之外。您必须:

          1. 循环浏览您的频道并根据某些标准抓住句柄
          2. 存储用户句柄/实体并使用内部标识符访问它们

          但发送给用户的唯一方法是通过客户端身份句柄,该句柄位于on_message 中,位于message.author 中,或者位于guild.channels[index].members[index] 中的通道中。为了更好地理解这一点,我建议阅读how to send a DM? 上的官方文档。

          【讨论】:

          • 您对send 的看法是正确的,但这并不能真正回答问题。
          • @PatrickHaugh 以什么方式不回答如何向用户发送消息?这正是我的第一个代码 sn-p 所做的。您必须找到用户句柄对象并在该实体上调用.send()。通过循环通道或通过on_message 请求。
          • "。如果 bot 所有者键入 !DM @user,则 bot 将 DM 所有者提到的用户。"
          【解决方案5】:

          最简单的方法是使用discord.ext.commands 扩展。这里我们使用converter 来获取目标用户,并使用keyword-only argument 作为可选消息发送给他们:

          from discord.ext import commands
          import discord
          
          bot = commands.Bot(command_prefix='!')
          
          @bot.command(pass_context=True)
          async def DM(ctx, user: discord.User, *, message=None):
              message = message or "This Message is sent via DM"
              await bot.send_message(user, message)
          
          bot.run("TOKEN")
          

          对于较新的 1.0+ 版本的 discord.py,您应该使用 send 而不是 send_message

          from discord.ext import commands
          import discord
          
          bot = commands.Bot(command_prefix='!')
          
          @bot.command()
          async def DM(ctx, user: discord.User, *, message=None):
              message = message or "This Message is sent via DM"
              await user.send(message)
          
          bot.run("TOKEN")
          

          【讨论】:

          • 按照@Torxed的建议迁移到v1.0后不再有用
          猜你喜欢
          • 2021-05-11
          • 2018-08-22
          • 1970-01-01
          • 2023-02-26
          • 1970-01-01
          • 2022-08-11
          • 2020-09-14
          • 2021-03-17
          • 2020-05-22
          相关资源
          最近更新 更多