on_message问题时未向用户发送消息

时间:2020-08-19 14:18:08

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

嗨,从频道向用户直接发送消息时出现问题。使用@user时出现的消息后跟消息应该是DM用户,我不确定为什么它不起作用。

消息中的提及应带有用户对象。

代码如下:

@commands.Cog.listener()
async def on_message(self, message):
    """Send a message thread reply to user."""
    for member in message.mentions: #get user object from mention in message?
        if not member.dm_channel: 
            await member.create_dm()
        try:
            time = datetime.utcnow()
            embed = discord.Embed(title=f"Reply", timestamp=time, colour=discord.Colour(0xff8100))
            embed.add_field(name="Message:", value="test message")
            await member.dm_channel.send(embed=embed)
        except discord.Forbidden:
            await message.channel.send(f"Reply cannot be sent because {member.name}'s direct messages are set to private.")
        except discord.HTTPException:
            await message.channel.send('I failed in sending the message.')
        except Exception as e:
            await message.channel.send(f'There\'s been a problem while sending the message that\'s not of type "Forbidden" or'
                       f' "HTTPException", but {e}.')
        else:
            await message.channel.send(f'Reply sent to {member.name}')

1 个答案:

答案 0 :(得分:0)

在discord.py中,您实际上不再需要创建dm通道。只需执行await ctx.author.send(# whatever message you want) 如果您尝试创建将 SPECIFIC DM 发送给邮件的作者的命令,则可以尝试

@client.command()
async def dm(ctx, *, message):
    await ctx.author.send(f"This is your dm {message}")

这非常简单。如果您有任何疑问或我误解了您的问题,请在评论中让我知道。

编辑: 如果您希望将dm发送到特定成员,则可以尝试此操作。

@client.command()
async def dm(ctx, member: discord.Member, *, message):
    await member.send(message)

希望您的问题已解决。

PS:以后,请详细解释您的问题/问题。