发送Webhook消息以通过不和谐py

时间:2019-05-15 15:34:49

标签: python discord.py

我正在尝试创建消息镜像(获取频道消息并通过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

1 个答案:

答案 0 :(得分:0)

您可以获取消息作者的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有几个关键字可用于发送文件和嵌入。

相关问题