首页 > 解决方案 > 当用户发送特定消息时,如何使用 Discord py 通过 webhook 在不和谐上发送消息

问题描述

我正在尝试创建一个模仿该消息指定的用户的基本机器人。

我还不明白如何获取用户 ID,然后是该用户的昵称和个人资料图片。我做了一个演示,只是为了模仿调用机器人命令的用户,但它会导致机器人不断重复消息的无限循环。


@client.event

async def on_ready():

    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    
    if message.author == client.user:
        return
    
    if message.content=='!test':
        webhook = Webhook.from_url('webhook url here', adapter=RequestsWebhookAdapter())
        
        a=message.content
        webhook.send(content=a, username=message.author.display_name, 
                        avatar_url=message.author.avatar_url,wait=True)
        

标签: pythondiscorddiscord.pywebhooks

解决方案


很简单,检查消息是否由 webhook 发送。

async def on_message(message):
   if message.webhook_id:
       return
   # other stuff here

参考:

  • webhook_id是一个可选整数,普通消息将具有webhook_idNone。

推荐阅读