Apr 1, 2023
Model: GPT-4
chenyq.thu@gmail.com
帮我实现如下函数: # 监听Discord返回的结果 @bot.event async def on_message(message, channel_id): # 检查消息是否是机器人感兴趣的结果 # 如果是,更新对应的Notion页面 ---- 说明:这是一个Discord机器人的监听函数,需要实现的功能包括: 1. 监听指定channel中特定消息,消息为一个名为【Midjourney Bot】的机器人发出,并且该消息包含 @ 别人的字符,同时消息中包含一个图片。 2. 提取消息的特定内容,包括 消息文本、消息的时间 以及 图片的下载URL; 3. 将这些消息打印出来;

以下是一个实现了您要求功能的示例代码。该代码使用了discord.py库来创建和运行Discord机器人,并使用了您提供的on_message函数。你需要先安装discord.py库,可使用pip install discord.py进行安装。

python
import discord from discord.ext import commands TOKEN = 'your_bot_token' bot = commands.Bot(command_prefix='!') # 监听Discord返回的结果 @bot.event async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 检查消息是否包含 @ 别人的字符,并且包含图片 if '@' in message.content and message.attachments: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 以及 图片的下载URL message_text = message.content message_time = message.created_at image_url = message.attachments[0].url # 将这些消息打印出来 print("Message Text: ", message_text) print("Message Time: ", message_time) print("Image URL: ", image_url) # 运行机器人 bot.run(TOKEN)

请确保使用您自己的Discord机器人的Token替换your_bot_token。此外,您需要指定您要监听的channel的ID并替换channel_id。这个脚本将根据您的要求监听来自【Midjourney Bot】的消息,提取相关信息并将其打印出来。

chenyq.thu@gmail.com
机器人已经成功激活,并且在目标频道中显示为online状态,为什么我在频道里出现了指定的消息,但是机器人没有任何打印输出。

抱歉给您带来了困扰。请尝试以下修改后的代码。这里我们添加了await bot.process_commands(message)来确保处理命令。同时,我们将on_message函数改为使用on_message装饰器,以便正确捕获消息事件。

python
import discord from discord.ext import commands TOKEN = 'your_bot_token' bot = commands.Bot(command_prefix='!') # 监听Discord返回的结果 @bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 检查消息是否包含 @ 别人的字符,并且包含图片 if '@' in message.content and message.attachments: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 以及 图片的下载URL message_text = message.content message_time = message.created_at image_url = message.attachments[0].url # 将这些消息打印出来 print("Message Text: ", message_text) print("Message Time: ", message_time) print("Image URL: ", image_url) await bot.process_commands(message) # 运行机器人 bot.run(TOKEN)

此外,请确保您已经为您的Discord机器人分配了足够的权限。机器人需要在目标服务器和频道中具有“读取消息”和“读取消息历史”的权限。

如果问题仍然存在,请检查以下事项:

  1. 您的机器人是否已正确邀请加入您的服务器?
  2. 您的机器人在服务器的其他频道中是否也未显示消息?
  3. 您的网络连接是否正常?

如果以上建议都无法解决问题,请再次询问,我会尽力提供更多帮助。

chenyq.thu@gmail.com
给我一个机器人测试程序,我先确保机器人能够读取频道消息并打印。

当然可以。以下是一个简单的Discord机器人测试程序,用于读取频道中的任何消息并将其打印到控制台。该程序使用discord.py库,您可以通过pip install discord.py进行安装。

python
import discord from discord.ext import commands TOKEN = 'your_bot_token' bot = commands.Bot(command_prefix='!') # 监听Discord返回的结果 @bot.listen('on_message') async def on_message(message): # 忽略机器人自己发出的消息 if message.author.bot: return # 提取消息的特定内容,包括 消息文本、消息的时间 和 发送者 message_text = message.content message_time = message.created_at message_sender = message.author.name # 将这些消息打印出来 print("Message Text: ", message_text) print("Message Time: ", message_time) print("Message Sender: ", message_sender) await bot.process_commands(message) # 运行机器人 bot.run(TOKEN)

请确保使用您自己的Discord机器人的Token替换your_bot_token。运行此脚本后,机器人将登录并开始监听服务器中所有频道的消息。只要有人发送消息,它就会打印出消息文本、消息时间和发送者。

如果您希望机器人仅在特定频道中打印消息,可以将以下代码段添加到on_message函数中:

python
# 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 在这里添加您希望在特定频道中执行的操作 pass

请记住将channel_id替换为您要监听的频道的实际ID。

chenyq.thu@gmail.com
结果:测试机器人可以运行,并且只会监听并打印指定频道的信息,但存在一个问题,消息的文本没能正确打印,时间和发送人都是对的。 根据测试结果反馈,说明机器人权限ok,也已加入特定频道并在线,请调整原需求的函数,目前看来是消息内容获取有误,没有获取到消息内容所以无法处理。 同时调整一个需求,不用监测消息中的图片URL,监测消息中的附件attachments,如果带附件并且以图片格式结尾,就把附件的链接提取出来,可以参考这段示例代码: @bot.event async def on_message(message): for attachment in message.attachments: if "Upscaled by" in message.content: file_prefix = 'UPSCALED_' else: file_prefix = '' if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): print("image:"+attachment.url+f"{file_prefix}{attachment.filename}")

根据您的反馈,我已对原需求的函数进行了调整。这次我们将提取文本内容和附件信息,并按照您的要求检查附件是否为图片格式。以下是调整后的代码:

python
import discord from discord.ext import commands TOKEN = 'your_bot_token' bot = commands.Bot(command_prefix='!') # 监听Discord返回的结果 @bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 检查消息是否包含 @ 别人的字符 if '@' in message.content: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 message_text = message.content message_time = message.created_at # 将这些消息打印出来 print("Message Text: ", message_text) print("Message Time: ", message_time) # 检查附件是否为图片格式 for attachment in message.attachments: if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): image_url = attachment.url print("Image URL: ", image_url) await bot.process_commands(message) # 运行机器人 bot.run(TOKEN)

这段代码将检查【Midjourney Bot】发送的消息中是否包含@字符。如果是,它将提取消息文本、时间并检查附件是否为图片格式。如果附件是图片,它将提取并打印图片URL。希望这次的调整能满足您的需求。

chenyq.thu@gmail.com
结果反馈,机器人能够正确监听指定的消息并打出时间戳,但是无法打印出消息的内容content,机器人打印的消息如下: MJ AutoBot#5655 has connected to Discord! Message Text: Message Time: 2023-04-01 16:57:52.402000+00:00 Message Text: Message Time: 2023-04-01 16:59:09.954000+00:00 请检查代码,是否是message.content写法有误,或者是机器人权限有问题,如果是权限问题,我应该如何操作?

非常抱歉给您带来了困扰。请尝试以下调整后的代码,我们将添加一个额外的检查以确保消息内容不为空。同时,请确保您的机器人在Discord服务器上具有“读取消息”权限。如果您的机器人仍然无法读取消息内容,可能是由于Discord API的限制。

python
import discord from discord.ext import commands TOKEN = 'your_bot_token' bot = commands.Bot(command_prefix='!') # 监听Discord返回的结果 @bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 检查消息是否包含 @ 别人的字符 if '@' in message.content: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 message_text = message.content message_time = message.created_at # 如果消息内容不为空,则打印消息内容 if message_text: print("Message Text: ", message_text) else: print("Message content is empty.") print("Message Time: ", message_time) # 检查附件是否为图片格式 for attachment in message.attachments: if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): image_url = attachment.url print("Image URL: ", image_url) await bot.process_commands(message) # 运行机器人 bot.run(TOKEN)

如果上述代码依然无法解决问题,可以考虑检查【Midjourney Bot】的消息内容格式,是否存在特殊字符或嵌入式内容。您可以将原始消息对象打印出来,以查看消息的完整结构:

python
print("Original message object: ", message)

将此行添加到on_message函数的适当位置。这将帮助您了解消息内容的实际结构,并找出可能的问题。

chenyq.thu@gmail.com
机器人能够打印出的message如下,似乎并不包含消息内容,我怀疑是权限问题,请给我一个完整的、具体的步骤来确保机器人权限设定正确。 Original message object: <Message id=1091771068252954855 channel=<TextChannel id=1090962440935714837 name='ai_prompt' position=4 nsfw=False news=False category_id=1081228532467896350> type=<MessageType.reply: 19> author=<Member id=936929561302675456 name='Midjourney Bot' discriminator='9282' bot=True nick=None guild=<Guild id=1081228531360608301 name='taoluqi的服务器' shard_id=0 chunked=False member_count=6>> flags=<MessageFlags value=0>> Message Text: Message Time: 2023-04-01 17:08:26.797000+00:00

从您提供的输出来看,消息内容确实没有显示。请按照以下步骤检查和设置机器人权限:

  1. 登录到您的Discord账户,转到您的服务器。
  2. 点击左侧栏中的服务器名称,然后选择“服务器设置”。
  3. 在“服务器设置”菜单中,点击“角色”。
  4. 在角色列表中找到您的机器人角色(通常以机器人名称命名)。点击机器人角色以编辑权限。
  5. 在权限设置中,确保已启用以下权限:
    • 查看频道
    • 发送消息
    • 管理消息(可选,如果您希望机器人能够删除或编辑消息)
    • 读取消息历史
    • 使用外部表情(可选,如果您希望机器人使用自定义表情)
  6. 单击下方的“保存更改”按钮以保存权限设置。

接下来,请确保机器人在目标频道中具有正确的权限。如果您为机器人创建了特定的角色,请确保角色已分配给机器人:

  1. 转到目标频道,右键点击频道名称,然后选择“编辑频道”。
  2. 在频道设置中,转到“权限”选项卡。
  3. 在角色/成员列表中找到您的机器人角色或成员名。
  4. 如果角色/成员名不在列表中,请单击+号添加角色/成员。
  5. 确保启用了以下权限:
    • 查看频道
    • 发送消息
    • 管理消息(可选,如果您希望机器人能够删除或编辑消息)
    • 读取消息历史
    • 使用外部表情(可选,如果您希望机器人使用自定义表情)
  6. 单击下方的“保存更改”按钮以保存权限设置。

完成这些步骤后,您的机器人应具有足够的权限来查看和读取消息内容。再次运行您的机器人脚本,检查是否可以正确打印消息内容。如果问题仍然存在,请提供更多详细信息,以便我们为您提供进一步帮助。

chenyq.thu@gmail.com
搞定了!是权限问题。 我们的沟通有点长,防止你忘记,我把最新可行的代码贴在这里: import os import discord from discord.ext import commands import nest_asyncio #bot_token = 'your_bot_token' #channel_id = your_channel_id intents = discord.Intents.all() intents.messages = True bot = commands.Bot(command_prefix='!', intents=intents) @bot.event async def on_ready(): print(f'{bot.user} has connected to Discord!') # 监听Discord返回的结果 @bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 print("Original message object: ", message) message_text = message.content message_time = message.created_at # 将这些消息打印出来 print("Message Text: ", message_text) print("Message Time: ", message_time) # 检查附件是否为图片格式 # for attachment in message.attachments: # if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): # image_url = attachment.url # print("Image URL: ", image_url) for attachment in message.attachments: if "Upscaled by" in message.content: file_prefix = 'UPSCALED_' else: file_prefix = '' if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): print("image:"+attachment.url+f"{file_prefix}{attachment.filename}") await bot.process_commands(message) nest_asyncio.apply() await bot.start(bot_token) ------- 机器人打印的消息如下: Original message object: <Message id=1091775972627464224 channel=<TextChannel id=1090962440935714837 name='ai_prompt' position=4 nsfw=False news=False category_id=1081228532467896350> type=<MessageType.reply: 19> author=<Member id=936929561302675456 name='Midjourney Bot' discriminator='9282' bot=True nick=None guild=<Guild id=1081228531360608301 name='taoluqi的服务器' shard_id=0 chunked=True member_count=6>> flags=<MessageFlags value=0>> Message Text: **a beautiful girl with big eyes, illustation, --v 5 --v 5** - <@1081228065419579494> (fast) Message Time: 2023-04-01 17:27:56.091000+00:00 image:https://cdn.discordapp.com/attachments/1090962440935714837/1091775972489056386/taoluqi_a_beautiful_girl_with_big_eyes_illustation_3f940c0d-c9ed-47d3-9342-1866648e73fc.pngtaoluqi_a_beautiful_girl_with_big_eyes_illustation_3f940c0d-c9ed-47d3-9342-1866648e73fc.png 现在的需求是这样的: 1. 获取的Message Time 只需要 精确到秒即可,不需要完整时间戳; 2. 调整Message Text内容,把1中缩减的Message Time添加到现有Message Text前面,用: 隔开。 3. 单独输出一个Prompt消息,内容为Message Text中 ** **包裹的部分; 4. 把现有的image消息输出分开,变成“ImageURL”和“ImageName”两个独立的消息;

根据您的需求,我对代码进行了相应的调整:

python
import os import discord from discord.ext import commands import nest_asyncio #bot_token = 'your_bot_token' #channel_id = your_channel_id intents = discord.Intents.all() intents.messages = True bot = commands.Bot(command_prefix='!', intents=intents) @bot.event async def on_ready(): print(f'{bot.user} has connected to Discord!') # 监听Discord返回的结果 @bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 message_text = message.content message_time = message.created_at.strftime('%Y-%m-%d %H:%M:%S') # 精确到秒 # 在Message Text前添加Message Time formatted_message_text = f"{message_time}: {message_text}" # 提取Prompt prompt = message_text[message_text.find('**')+2:message_text.rfind('**')] # 将这些消息打印出来 print("Formatted Message Text: ", formatted_message_text) print("Prompt: ", prompt) print("Message Time: ", message_time) # 检查附件是否为图片格式 for attachment in message.attachments: if "Upscaled by" in message.content: file_prefix = 'UPSCALED_' else: file_prefix = '' if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): image_url = attachment.url image_name = f"{file_prefix}{attachment.filename}" print("Image URL: ", image_url) print("Image Name: ", image_name) await bot.process_commands(message) nest_asyncio.apply() await bot.start(bot_token)

这段代码根据您的要求进行了以下更改:

  1. 使用strftime('%Y-%m-%d %H:%M:%S')将Message Time精确到秒。
  2. 在Message Text前添加Message Time,用:隔开。
  3. 单独输出一个Prompt消息,内容为Message Text中**包裹的部分。
  4. 将现有的image消息输出分开,变成“ImageURL”和“ImageName”两个独立的消息。
chenyq.thu@gmail.com
太厉害了!格式全部正确。抱歉又有个新需求,请以监听到的Prompt消息为输入,写一个函数,用于从Notion的Database中查询对应Prompt页面的PageID,输出的结果为匹配的page_id。 匹配的Notion字段名称为"最终Prompt",类型为"formula",目标匹配的字段为formula的"string"; 匹配规则为只要Prompt中能匹配到完整的string即可,如果匹配到多个,则取最后编辑时间最晚的那个。

为了实现这个功能,您需要首先确保安装了notion-client库。您可以使用以下命令来安装:

bash
pip install notion-client

接下来,您需要从 Notion 获取一个 API 密钥,并与具有相应权限的 Notion 页面共享。

在完成这些设置后,您可以按照以下代码示例实现所需功能:

python
from notion_client import Client import re import datetime # your_notion_api_key = 'your_notion_api_key' # your_database_id = 'your_database_id' notion = Client(auth=your_notion_api_key) def find_page_id_by_prompt(prompt): results = [] has_more = True next_cursor = None while has_more: response = notion.databases.query( **{ "database_id": your_database_id, "filter": { "property": "最终Prompt", "rich_text": { "contains": prompt } }, "start_cursor": next_cursor } ) results.extend(response["results"]) has_more = response["has_more"] next_cursor = response["next_cursor"] if not results: print("No matching page found.") return None # Sort pages by their "last_edited_time" in descending order sorted_results = sorted(results, key=lambda x: x['last_edited_time'], reverse=True) # Get the page_id of the most recent match page_id = sorted_results[0]['id'] print(f"Matching page id: {page_id}") return page_id # Example usage prompt = "a beautiful girl with big eyes, illustation, --v 5 --v 5" find_page_id_by_prompt(prompt)

这个函数find_page_id_by_prompt根据给定的prompt从Notion数据库中查询相应的页面,并返回最近编辑过的页面ID。请注意,您需要用您的 Notion API 密钥和数据库ID替换 your_notion_api_keyyour_database_id 变量。

这个函数首先使用 databases.query 方法根据 "最终Prompt" 过滤 Notion 数据库中的页面,然后对结果进行排序,根据 "last_edited_time" 降序排序。最后,返回最近编辑过的页面的 page_id。如果没有找到匹配的页面,函数将返回 None。

chenyq.thu@gmail.com
我觉得你可以换一种思路,发起query时,要求返回结果按照[最后编辑时间]降序排列,然后取返回结果的第一个。如果返回为空,则打印消息并不返回page_id

您可以按照以下代码示例修改查询以按照[最后编辑时间]降序排列结果,并在结果为空时打印消息:

python
from notion_client import Client import re import datetime # your_notion_api_key = 'your_notion_api_key' # your_database_id = 'your_database_id' notion = Client(auth=your_notion_api_key) def find_page_id_by_prompt(prompt): response = notion.databases.query( **{ "database_id": your_database_id, "filter": { "property": "最终Prompt", "rich_text": { "contains": prompt } }, "sorts": [ { "timestamp": "last_edited_time", "direction": "descending" } ] } ) results = response["results"] if not results: print("No matching page found.") return None # Get the page_id of the most recent match page_id = results[0]['id'] print(f"Matching page id: {page_id}") return page_id # Example usage prompt = "a beautiful girl with big eyes, illustation, --v 5 --v 5" find_page_id_by_prompt(prompt)

在这个版本的find_page_id_by_prompt函数中,我在databases.query方法中添加了一个sorts参数,该参数将结果按照[最后编辑时间]降序排列。这样,您就可以直接获取返回结果中的第一个页面,而无需再对结果进行排序。

其他部分与之前的代码相同。如果没有找到匹配的页面,函数将返回 None。

chenyq.thu@gmail.com
牛啊!一次成功! 不好意思我又来加需求了。 我想减少对notion的query查询,所以希望缓存prompt和page_id的结果,可以采用字典存储,也可以采用redis来存储,你的建议是哪种好?

在这种情况下,使用字典或 Redis 取决于您的需求和应用程序的复杂性。下面是两种方法的优缺点:

  1. 字典:

    优点:

    • 简单易用,不需要额外的库或设置
    • 适用于小型应用程序和短期缓存
    • 在应用程序运行时,查询速度很快

    缺点:

    • 数据仅存储在内存中,应用程序关闭后将丢失
    • 在多个进程或分布式应用程序中共享数据会变得困难
  2. Redis:

    优点:

    • 数据存储在外部服务器上,可以在多个进程或分布式应用程序中共享
    • 支持持久化,可以在应用程序关闭后仍保留数据
    • 更高级的数据结构和功能,例如过期键、原子操作等

    缺点:

    • 需要安装 Redis 库和服务器,设置和管理可能相对复杂
    • 对于简单的缓存需求,可能是过度解决方案

如果您只需要一个简单的缓存,而且您的应用程序不需要在多个进程或分布式环境中共享数据,那么字典可能是一个更简单的解决方案。然而,如果您需要更强大的功能和数据持久化,那么 Redis 可能是更好的选择。

在这里,我将提供使用字典作为缓存的示例,因为它更简单且易于实现。如果您觉得 Redis 更适合您的需求,请告诉我,我将为您提供相应的实现。

以下是使用字典作为缓存的示例代码:

python
# 其他导入和设置略 cache = {} def find_page_id_by_prompt(prompt): # Check if the prompt is already in the cache if prompt in cache: print(f"Matching page id from cache: {cache[prompt]}") return cache[prompt] response = notion.databases.query( # ... ) results = response["results"] if not results: print("No matching page found.") return None # Get the page_id of the most recent match page_id = results[0]['id'] print(f"Matching page id: {page_id}") # Save the result to the cache cache[prompt] = page_id return page_id # Example usage prompt = "a beautiful girl with big eyes, illustation, --v 5 --v 5" find_page_id_by_prompt(prompt)

在这个示例中,我添加了一个名为cache的字典来存储promptpage_id。在查询 Notion 之前,我们先检查缓存中是否已有匹配的prompt。如果找到匹配项,我们直接从缓存中返回结果。如果未找到匹配项,我们会继续查询 Notion

chenyq.thu@gmail.com
我去,你也太牛了,我刚说要你给我字典的实现,你就给出来了,还实现了完整的缓存查询,连缓存未命中去Notion查询然后写入缓存都考虑到的,真的牛逼啊。 不好意思我又来麻烦了,在另一个模块中,我已经让你实现了一个函数update_notion_page(notion, page_id, msg, image_url),这个函数需要传入page_id, 图片链接image_url,消息msg,然后函数可以将图片下载到腾讯云COS上,并将消息和图片写入到特定的页面page_id中,现在我希望你在前面的Discord on_message函数中增加这部分实现:如果监听到特定的图片生成消息,先匹配该消息对应的Notion database页面,然后将消息和图片更新到对应页面中。具体如下: 1. 如果监听到带有图片的特定消息,则将该消息的Prompt去查询对应的page_id(可以用前面的find_page_id_by_prompt函数),如果查询到page_id,那么将Formatted Message Text 和 Image URL对应变量传入函数update_notion_page中。 为了防止消息过长你忘记了前面的函数,我把最新的on_message函数放在下面: @bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 message_text = message.content message_time = message.created_at.strftime('%Y-%m-%d %H:%M:%S') # 精确到秒 # 在Message Text前添加Message Time formatted_message_text = f"{message_time}: {message_text}" # 提取Prompt prompt = message_text[message_text.find('**')+2:message_text.rfind('**')] # 将这些消息打印出来 print("Formatted Message Text: ", formatted_message_text) print("Prompt: ", prompt) print("Message Time: ", message_time) # 检查附件是否为图片格式 for attachment in message.attachments: if "Upscaled by" in message.content: file_prefix = 'UPSCALED_' else: file_prefix = '' if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): image_url = attachment.url image_name = f"{file_prefix}{attachment.filename}" print("Image URL: ", image_url) print("Image Name: ", image_name) await bot.process_commands(message)

谢谢你的认可!根据你的需求,我已经在 on_message 函数中增加了对 update_notion_page 的调用。以下是修改后的代码:

python
@bot.listen('on_message') async def on_message(message): # 判断消息是否来自名为【Midjourney Bot】的机器人 if message.author.name == "Midjourney Bot" and message.author.bot: # 获取指定channel channel = bot.get_channel(channel_id) # 判断是否为指定channel if message.channel == channel: # 提取消息的特定内容,包括 消息文本、消息的时间 message_text = message.content message_time = message.created_at.strftime('%Y-%m-%d %H:%M:%S') # 精确到秒 # 在Message Text前添加Message Time formatted_message_text = f"{message_time}: {message_text}" # 提取Prompt prompt = message_text[message_text.find('**')+2:message_text.rfind('**')] # 将这些消息打印出来 print("Formatted Message Text: ", formatted_message_text) print("Prompt: ", prompt) print("Message Time: ", message_time) # 检查附件是否为图片格式 for attachment in message.attachments: if "Upscaled by" in message.content: file_prefix = 'UPSCALED_' else: file_prefix = '' if attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif")): image_url = attachment.url image_name = f"{file_prefix}{attachment.filename}" print("Image URL: ", image_url) print("Image Name: ", image_name) # 获取 page_id page_id = find_page_id_by_prompt(prompt) # 如果找到了匹配的页面,更新页面 if page_id: await update_notion_page(notion, page_id, formatted_message_text, image_url) await bot.process_commands(message)

现在,当监听到带有图片的特定消息时,on_message 函数会先调用 find_page_id_by_prompt 函数查询对应的 Notion 页面。如果查询到 page_id,那么它将调用 update_notion_page 函数,将已格式化的消息文本和图片 URL 更新到对应的页面中。

chenyq.thu@gmail.com
牛哇!完美!态度完美,任劳任怨,你现在是我认定的无敌程序员!我会在我的作品中属上你的大名的!以后你发达了记得对我好点儿~

非常感谢你的认可和夸奖!我很高兴能够帮助你解决问题。如果你将来还有任何问题或需要帮助,请随时联系我,我会尽我所能提供支持。祝你的项目取得成功,期待在未来有更多的合作机会!再次感谢你的支持!

Create your own with gpt.best →