首页 > 解决方案 > 我如何获得不和谐消息的作者,然后给他发私信?

问题描述

我如何扫描消息中的单词,如果消息包含该单词,DM 消息的作者?这是到目前为止的代码 -

    @client.event
    async def on_message(message):
        word_list = ['Idiot','noob','dumb']
        for word in word_list:
            if word in message.content:
                if message.author == 'ClientUser':
                    return
                member: discord.member
                member = message.author.name
                channel = await member.create_dm()
                await channel.send(f'Please do not say this word - {word}')

标签: pythondiscorddiscord.py

解决方案


我们可以通过获取用户message.author并向该特定用户发送消息,我们可以通过

await message.author.send(f'Please do not say this word - {word}')

但我们只需要检查成员而不是机器人消息!所以我们这样做

if message.author.bot != True:
    await message.author.send(f'Please do not say this word - {word}')

推荐阅读