简体   繁体   中英

Send webhook message to clone a user message via discord py

I'm trying to do a message mirror (get message of a channel and send it to another channel via a webhook). How can i send the webhook message to replicate a user sent message (same name, same profile picture and same message content and attachements) ?

i know there is a documentation ( https://discordpy.readthedocs.io/en/latest/api.html#discord.Webhook ) but i just can't understand how it's supposed to get the profile picture and nickname of the user who sent the message then use them on the webhook

this is the bit of code i'm strugling with

@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

You can get the display_name and avatar_url of the message author and pass them as keyword arguments to 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)
    

This just sends the content, there are several more keywords to Webhook.send that you can use for sending file and embeds, depending on your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM