【问题标题】:Is there a way for a discord bot to respond to a mention of a specific user using Discord.py?有没有办法让 Discord 机器人使用 Discord.py 来回应提及特定用户?
【发布时间】:2021-03-29 16:24:27
【问题描述】:

我希望我的机器人能够回复提及特定用户(例如,一个人提到我的个人帐户,而机器人回复说我目前不在这里)

有没有办法使用类似的格式来做到这一点?

@client.event
async def on_message(message):

    if message.author == client.user:
        return

    if message.content.startswith('@user_id'):
        await message.channel.send('Im not here leave a message!') 

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案1】:

    您需要使用不和谐机器人收到提及的特定格式。格式为 。

    @client.event
    async def on_message(message):
        if ("<@!put user id here>" in message.content):
            await message.channel.send("Im not here leave a message!")
    

    如何应用它并为我工作的示例

    @client.event
    async def on_message(message):
        if ("<@!348256959671173120>" in message.content):
            await message.channel.send("Im not here leave a message!")
    

    【讨论】:

    • 好吧,我重新设计了我的程序以遵循这种格式,但仍然不知道这是一种过时的格式还是还有更多?
    • 我删除了 f 字符串和大括号,并准确地发布了我使用的内容,以便它在下面工作。你可以试试这个。
    【解决方案2】:

    Discord Member objects 有一个 .mentioned_in(message) 方法。

    WIZARD_ID = 123456 # <- replace with user ID you want
    async def on_message(message):
        wizard_of_oz = message.guild.get_member(WIZARD_ID)
        if wizard_of_oz.mentioned_in(message):
             await message.channel.send("Who dares summon the great Wizard of OZ?!")
    

    如果您还想判断用户是否被 role 提及,您还需要检查消息中的 role_mentions。所以更完整的例子如下:

    def was_mentioned_in(message: discord.Message, member: discord.Member) -> bool:
        """
        Whether or not the member (or a role the member has) was mentioned in a message.
        """
        if member.mentioned_in(message):
            return True
        for role in message.role_mentions:
            if role in member.roles:
                return True
        return False
    
    @client.event
    async def on_message(message):
        wizard_of_oz = message.guild.get_member(WIZARD_ID)
        if was_mentioned_in(message, wizard_of_oz):
             await message.channel.send("Who dares summon the great Wizard of OZ?!")
    

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 2021-10-22
      • 2020-10-11
      • 2021-01-21
      • 2018-08-17
      • 1970-01-01
      • 2021-08-03
      • 2021-01-12
      • 2021-01-08
      相关资源
      最近更新 更多