【发布时间】:2021-01-05 20:13:38
【问题描述】:
需要做什么?:
一个相当简单的脚本从 RSS 提要中提取信息,并通过 discord_webhook 在不和谐频道中发布。这工作正常。但是,条目必须在一定时间后“更新”。因此,旧的 webhook 消息被删除,新的消息被发送或编辑。
有什么问题?:
问题是该消息将被删除,但是在“重新发布”时它也发布了前一个消息。我对此进行了研究并找到了一些答案,但没有任何东西可以从代码意义上解决这个问题。 (我对编码不是太有经验)。
我试过了:
从逻辑角度查看我的代码,它似乎不是由代码本身引起的,而是更像是 discord_webhook.py 的事情,我在发送多条消息时做得不对(循环)。 正如我所说,我已经看到了一些解决方案,但无法使用它们来更改我的代码或获取需要更改的内容。
代码:
from discord_webhook import DiscordWebhook, DiscordEmbed
import feedparser
from time import sleep
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Vars
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
webhook = DiscordWebhook(url='')
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Functies
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
def main(webhook):
# Main function , calling other functions
# Send first message
message_send(webhook)
# Time to wait before refresh
sleep(10)
# Remove the old message
message_remove(webhook)
def message_send(webhook):
# Send message thru webhook
# Get the info from the RSS feed function
entry = info_get()
# Create embed message for the webhook
embed = DiscordEmbed(title="Report", description="Repeatable report", color="14177041")
embed.set_author(name='NCSC')
embed.set_footer(text='https://www.ncsc.nl/')
for i in entry:
embed.add_embed_field(name=i[0], value=i[1], inline=False)
embed.set_timestamp()
webhook.add_embed(embed)
# Send message
response = webhook.execute()
def message_remove(webhook):
# Removes the old message
webhook.delete(webhook)
def info_get():
list = []
# Get the information thru RSS feed
info = feedparser.parse("https://advisories.ncsc.nl/rss/advisories")
# Use the first 4 entries
entry = info.entries[: 4]
# List only the title and link, rest is not needed
for i in entry:
list.append([i.title, i.link])
return list
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Runs
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Loop the main function
while True:
main(webhook)
第一次刷新后不和谐的结果:
这是不断堆叠消息的脚本的结果。
注意:sleep(10) 通常是 sleep(3600)
https://cdn.discordapp.com/attachments/490928227163045888/795927426122121266/unknown.png
如果您有任何想法,请告诉我,即使是可以改进我的代码的事情,因为我正在努力学习。
谢谢,
您好。
【问题讨论】:
-
你这里没有使用官方的 discord.py 库,为什么要标记它?
-
我没想到,StackOverflow 建议的,谢谢你通知我。我已经改了。