【问题标题】:How do I send a message on discord through a webhook using Discord py when a user sends a certain message当用户发送特定消息时,如何使用 Discord py 通过 webhook 在不和谐上发送消息
【发布时间】:2021-06-15 11:58:41
【问题描述】:

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

我还不明白如何获取用户 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)
        

【问题讨论】:

    标签: python discord discord.py webhooks


    【解决方案1】:

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

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

    参考资料:

    • webhook_id 是一个可选整数,普通消息将 webhook_id 设为 None。

    【讨论】:

      猜你喜欢
      • 2019-10-02
      • 1970-01-01
      • 2020-10-01
      • 2018-01-06
      • 2020-03-20
      • 2021-01-19
      • 2023-02-15
      • 2021-03-13
      • 2020-04-07
      相关资源
      最近更新 更多