【问题标题】:How can i send a DM message to someone just by mentioning his name in on_message in discord.py我如何通过在 discord.py 的 on_message 中提及他的名字来向某人发送 DM 消息
【发布时间】:2021-01-29 08:54:10
【问题描述】:

我希望机器人向消息内容中包含姓名的用户发送 DM 消息,代码如下:


    @commands.Cog.listener()
    async def on_message(self, message):
        for user in People:  # People list includes all members' names in the server
            if f'{user}' in message.content:
                await message.user.send("Hi")
            else:
                pass

我知道问题出在await message.user.send("Hi"),但我只想在我的消息中说(例如)“你好 Ahmed”,机器人应该向 ahmed 发送一条 DM 消息说“你好”

【问题讨论】:

    标签: discord discord.py python-3.7 discord.py-rewrite


    【解决方案1】:

    您不必重复公会中每个人的名字。 discord.Message 具有属性 mentions。它返回消息中提到的成员列表。所以你可以迭代那个列表。

    @commands.Cog.listener()
    async def on_message(self, message):
        for member in message.mentions:
            await member.send('Hi')
    

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 2020-06-12
      • 2021-12-03
      • 1970-01-01
      • 2022-01-11
      • 2019-12-11
      • 2021-01-27
      • 2021-08-12
      相关资源
      最近更新 更多