Skip to content
Advertisement

Creating Discord Bot method that moves messages as embeded message

I’ve been learning how to code a discord bot recently and I’ve hit a wall. I am wanting to create a method that allows me to move messages from one channel to another. I created a solution that works but not how I would like it to. Idealy I would want to just have the bot almost do a reddit repost where it takes the exact message and just has it embedded. Currently what I have for the method is

@client.event
async def on_message(message):
    author = message.author
    content = message.context
    channel = message.channel

    if message.content.startswith('!move'):
        #code to process the input, nothing special or important to this
        for i in fetchedMessages:
            embededMessage = discord.Embed()
            embededMessage.description = i.context
            embededMessage.set_author(name=i.author, icon_url=i.author.avatar_url)
            await channelToPostIn.send(embeded=embededMessage)
            # delete the old message
            i.delete()

Now this works perfectly for text messages but not for images or for example if the post was embedded in the first place. If someone has a more elegant solution or is able to point me in the right direction in the documentation I would be much appreciative. Thanks.

Advertisement

Answer

@Buster’s solution worked correctly except for when a user uploaded a picture as a file attached to the message. To fix this I ended up setting the image of the embedded message with the proxy_url of the image attached. my whole move command is as followed.

# Move: !move {channel to move to} {number of messages}
# Used to move messages from one channel to another.
@client.command(name='move')
async def move(context):

    if "mod" not in [y.name.lower() for y in context.message.author.roles]:
        await context.message.delete()
        await context.channel.send("{} you do not have the permissions to move messages.".format(context.message.author))
        return

    # get the content of the message
    content = context.message.content.split(' ')
    # if the length is not three the command was used incorrectly
    if len(content) != 3 or not content[2].isnumeric():
        await context.message.channel.send("Incorrect usage of !move. Example: !move {channel to move to} {number of messages}.")
        return
    # channel that it is going to be posted to
    channelTo = content[1]
    # get the number of messages to be moved (including the command message)
    numberOfMessages = int(content[2]) + 1
    # get a list of the messages
    fetchedMessages = await context.channel.history(limit=numberOfMessages).flatten()
    
    # delete all of those messages from the channel
    for i in fetchedMessages:
        await i.delete()

    # invert the list and remove the last message (gets rid of the command message)
    fetchedMessages = fetchedMessages[::-1]
    fetchedMessages = fetchedMessages[:-1]

    # Loop over the messages fetched
    for messages in fetchedMessages:
        # get the channel object for the server to send to
        channelTo = discord.utils.get(messages.guild.channels, name=channelTo)

        # if the message is embeded already
        if messages.embeds:
            # set the embed message to the old embed object
            embedMessage = messages.embeds[0]
        # else
        else:
            # Create embed message object and set content to original
            embedMessage = discord.Embed(
                        description = messages.content
                        )
            # set the embed message author to original author
            embedMessage.set_author(name=messages.author, icon_url=messages.author.avatar_url)
            # if message has attachments add them
            if messages.attachments:
                for i in messages.attachments:
                    embedMessage.set_image(url = i.proxy_url)

        # Send to the desired channel
        await channelTo.send(embed=embedMessage)

Thanks to everyone that helped with this problem.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement