python - Discord.py 编辑的消息记录器故障
问题描述
此代码允许将已编辑的消息记录到 discord 服务器上的另一个通道中,但有时它会发送 GIF 并将其计为已编辑的消息。即使我认为我告诉它忽略嵌入,我该如何解决?
@commands.Cog.listener()
async def on_message_edit(self, message_before, message_after):
if message_before.author == self.client.user: #Checks to see if the author of the edited message wasn't BenBot.
return
elif message_before.author.bot == True: #Checks to see if the author of the edited messages wasn't a bot
return
if len(message_after.embeds)>1: #Checks to see if the message hasn't been edited with an embed.
return
now = datetime.now() #Grabs the current time
current_time=now.strftime("%H:%M:%S") #Formats the current time into a readable form.
current_date=now.strftime("%d/%m/%Y") #Formats the current date into a readable form.
edit_embed=discord.Embed(title = f"Message Edited", description= f'**User:** <@{message_before.author.id}>\n**Channel:** <#{message_before.channel.id}>\n**Server:** {message_before.guild}\n**Message (Before Edit):** \n{message_before.content}\n**Message (After Edit):** \n{message_after.content}\n\n[Jump to This Message]({message_before.jump_url})', color=0x00ff00)
edit_embed.set_footer(text=f"Message ID: {message_before.id}\nDate: {current_date} • Time: {current_time}")
edit_embed.set_author(name =f"{message_before.author}", icon_url=f"{message_before.author.avatar_url}")
archive_edit=self.client.get_channel(789929258184474675) #Grabs the ID of the Edited Messages Channel where the archive is to be stored.
try:
edit_embed.set_image(url=message_after.attachments[0].proxy_url)
except IndexError:
pass
await archive_edit.send(embed=edit_embed) #Sends the edited message
解决方案
是的,我也有这个问题,当我发送链接时,discord 会在该链接中添加一个嵌入,这将被视为一次编辑。但是,内容仍然保持不变。所以我用这个条件修复了它:
if message_before.content == message_after.content:
return
因此,您的活动将如下所示:
@commands.Cog.listener()
async def on_message_edit(self, message_before, message_after):
if message_before.author == self.client.user: # Checks to see if the author of the edited message wasn't BenBot.
return
elif message_before.author.bot == True: # Checks to see if the author of the edited messages wasn't a bot
return
elif message_before.content == message_after.content: # Checks to see if there is an actual edit
return
now = datetime.now() # Grabs the current time
current_time = now.strftime("%H:%M:%S") # Formats the current time into a readable form.
current_date = now.strftime("%d/%m/%Y") # Formats the current date into a readable form.
edit_embed = discord.Embed(title = f"Message Edited", description = f'**User:** <@{message_before.author.id}>\n**Channel:** <#{message_before.channel.id}>\n**Server:** {message_before.guild}\n**Message (Before Edit):** \n{message_before.content}\n**Message (After Edit):** \n{message_after.content}\n\n[Jump to This Message]({message_before.jump_url})', color = 0x00ff00)
edit_embed.set_footer(text = f"Message ID: {message_before.id}\nDate: {current_date} • Time: {current_time}")
edit_embed.set_author(name = f"{message_before.author}", icon_url = f"{message_before.author.avatar_url}")
archive_edit = self.client.get_channel(789929258184474675) # Grabs the ID of the Edited Messages Channel where the archive is to be stored.
try:
edit_embed.set_image(url = message_after.attachments[0].proxy_url)
except IndexError:
pass
await archive_edit.send(embed = edit_embed) #Sends the edited message
推荐阅读
- python - django viewset 扩展 mixins.UpdateModelMixin 但邮递员通过补丁方法更新模型返回'细节:不允许方法补丁'
- python - 我在这个带有 pool.map 的文件中做错了什么,导致什么都没有出现,我必须重新启动 shell?
- html - 第一行和第二行之间有间隙的自定义单选按钮
- vue.js - 注销时清除 nuxt 存储和所有子模块
- c# - 卡在 HttpPost 方法上,试图输入 JSON 文本,但对象应该是“空的”
- aws-lambda - 如何根据请求主体变量/项目执行不同的 aws lambda(aws api 网关)
- bing - 需要防止网站链接显示在 Bing 搜索结果中
- bash - 识别备份目录中的最大目录名称和最大时间戳
- python - 带有 Flask 的 Windows 服务仅在 DEBUG 模式下运行。普通模式似乎崩溃
- firebase - 如何通过 http-get restful API 访问 Firestore 数据库?