【问题标题】:Discord.py - Reply to DM with a specific messageDiscord.py - 使用特定消息回复 DM
【发布时间】:2020-12-27 08:32:28
【问题描述】:

试图让我的机器人用“这是一个 dm”回复 DM,但没有成功,这是我的代码:

@client.event
async def on_message(message):
    if message.guild == null:
        await message.channel.send('this is a dm')
    else:
        pass

我也试过用这个:

@client.event
async def on_message(message):
    if isinstance(message.channel, discord.channel.DMChannel):
        await ctx.send('This is a DM')

在最后一个上,由于上​​下文 (ctx) 我收到一个错误

【问题讨论】:

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


    【解决方案1】:

    您发送的第一个代码使用null,这在 python 中甚至都不是。第二个有点不必要,改用这个:

    @client.event
    async def on_message(message):
        if not message.guild:
            await message.channel.send('this is a dm')
    

    【讨论】:

      【解决方案2】:

      我知道这是一个很晚的回应,但我真的需要指出这一点。

      我建议使用此代码,否则它可能会继续响应自己的消息,并且您会得到一个响应循环。如果用户禁用了私人 DM,此代码也会捕获错误。

      @client.event()
      async def on_message(message):
          if message.author == client.user:
              return
          if not message.guild:
              try:
                  await message.channel.send("This is a DM.")
              except discord.errors.Forbidden:
                  pass
          else:
              pass
      

      【讨论】:

        【解决方案3】:

        试试这个:

        import discord
        
        client = discord.Client()
        
        @client.event
        async def on_message(message):
            if isinstance(message.channel, discord.channel.DMChannel) and message.author != client.user:
                await message.channel.send('This is a DM')
        
        client.run("KEY")
        

        【讨论】:

          猜你喜欢
          • 2019-04-21
          • 1970-01-01
          • 2021-05-06
          • 2021-04-20
          • 1970-01-01
          • 2021-03-25
          • 2021-12-24
          • 2021-11-21
          • 2020-09-13
          相关资源
          最近更新 更多