【问题标题】:Send webhook message to clone a user message via discord py通过 discord py 发送 webhook 消息以克隆用户消息
【发布时间】:2019-10-02 19:34:31
【问题描述】:

我正在尝试做一个消息镜像(获取一个频道的消息并通过 webhook 将其发送到另一个频道)。我如何发送 webhook 消息来复制用户发送的消息(相同的名称、相同的头像、相同的消息内容和附件)?

我知道有一个文档 (https://discordpy.readthedocs.io/en/latest/api.html#discord.Webhook),但我只是不明白它应该如何获取发送消息的用户的个人资料图片和昵称,然后在 webhook 上使用它们

这是我正在努力解决的代码

@client.event
async def on_message(message):
    fchannel = client.get_channel(fromchannel)
    tchannel = client.get_channel(tochannel)
    if message.channel == fchannel:
        attach = message.attachments
        print ("Found message to forward: "+ message.content)
        if attach:
            for attachment in attach:
                #need code to send the message and image throught the webhook

        else:
            #need code to send the message throught the webhook

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您可以获取消息作者的display_nameavatar_url,并将它们作为关键字参数传递给Webhook.send

    from discord.utils import get
    
    @client.event
    async def on_message(message):
        fchannel = client.get_channel(fromchannel)
        tchannel = client.get_channel(tochannel)
        webhook_id = 12345
        hooks = await tchannel.webhooks()
        hook = get(hooks, id=webhook_id)  
        if message.channel == fchannel:
            await hook.send(content=message.content, username=message.author.display_name, 
                            avatar_url=message.author.avatar_url)
        
    

    这只是发送内容,还有几个关键字到Webhook.send,您可以使用它们来发送文件和嵌入,这取决于您的要求。

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2020-03-20
      • 2020-04-07
      • 2021-06-17
      • 1970-01-01
      • 2019-03-05
      • 2021-03-24
      • 2020-10-01
      • 1970-01-01
      相关资源
      最近更新 更多