Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

python - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This would be a lot easier if you were to use the commands.Bot extension: discord.ext.commands.Bot() (check out a basic bot)

bot = commands.Bot(...)

@bot.command()
async def move(ctx, channel: discord.TextChannel, *message_ids: int): # Typehint to a Messageable
    '''Moves the message content to another channel'''
    # Loop over each message id provided
    for message_id in message_ids:
        # Holds the Message instance that should be moved
        message = await ctx.channel.fetch_message(message_id)

        # It is possible the bot fails to fetch the message, if it has been deleted for example
        if not message:
            return

        # Check if message has an embed (only webhooks can send multiple in one message)
        if message.embeds:
            # Just copy the embed including all its properties
            embed = message.embeds[0]
            # Overwrite their title
            embed.title = f'Embed by: {message.author}'

        else:
            embed = discord.Embed(
                title=f'Message by: {message.author}',
                description=message.content
            )

        # send the message to specified channel
        await channel.send(embed=embed)
        # delete the original
        await message.delete()

Possible problems:

  • The message has both an embed and content (easy fix, just add the message.content to the embed.description, and check whether the length isn't over the limit)
  • The command isn't used in the channel where the message resides, so it can't find the message (can be fixed by specifying a channel to search the message in, instead of using Context)
  • A video being in an embed, I am unsure what would happen with for example a youtube link that is embedded, as a bot can't embed videos afaik

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...