创建将消息作为嵌入消息移动的Discord Bot方法

2024-10-03 04:34:37 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我一直在学习如何编写一个discord机器人,我遇到了麻烦。我想创建一种方法,允许我将消息从一个通道移动到另一个通道。我创建了一个有效的解决方案,但不是我想要的。理想情况下,我想让机器人几乎做一个reddit转发,在那里它接收准确的消息并将其嵌入。目前我对这个方法的理解是

@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()

现在,这对于文本消息非常有效,但对于图像或例如,如果帖子首先被嵌入,则不适用。如果有人有一个更优雅的解决方案,或者能够在文档中为我指出正确的方向,我将不胜感激。谢谢


Tags: theto方法消息urlmessagecontextchannel
2条回答

@Buster的解决方案工作正常,但用户将图片作为附加到消息的文件上传时除外。为了解决这个问题,我最终设置了嵌入消息的图像,并附加了图像的代理url。我的整个移动命令如下

# 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)

感谢所有帮助解决这个问题的人

如果您使用commands.Bot扩展名:^{}(请查看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()

可能的问题:

  • 消息同时具有嵌入和内容(很容易修复,只需将message.content添加到embed.description,并检查长度是否超过限制)
  • 该命令未在消息所在的通道中使用,因此无法找到消息(可以通过指定搜索消息的通道而不是使用上下文来修复)
  • 如果视频被嵌入,我不确定嵌入的youtube链接会发生什么,因为机器人不能完全嵌入视频

相关问题 更多 >