【问题标题】:Im not able to automate discord webhook python我无法自动化不和谐 webhook python
【发布时间】: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 建议的,谢谢你通知我。我已经改了。

标签: python discord


【解决方案1】:

如果你想使用官方的discord.py库。

注意:很遗憾,您不能使用 webhook 删除消息,最简单的方法是编辑消息(您需要 discord.py v. 1.6 或更高版本)

import aiohttp
import discord
from discord.ext import tasks

intents = discord.Intents.default() # Enabling default intents, change it accordingly to your needs
client = discord.Client(intents=intents) # If you also want commands use `commands.Bot`
previous_message_id = None # Just a placeholder

@tasks.loop(seconds=10) # Or whatever interval you want
async def bg_task(webhook):
    global previous_message_id
    """Will edit a message every 10 seconds (if there is one)"""
    if previous_message_id is None:
        # Sending a new message
        message = await webhook.send('Hello World', username='Foo', wait=True)
        previous_message_id = message.id

    else:
        # Editing the previous message sent by the webhook
        await webhook.edit_message(previous_message_id, content='Whatever')


@client.event
async def on_ready():
    await client.wait_until_ready()
    print('Logged in as {0.user}'.format(client))
    # Creating a session and getting the webhook
    client.session = aiohttp.ClientSession() 
    webhook = discord.Webhook.from_url('url-here', adapter=discord.AsyncWebhookAdapter(client.session))
    # Starting the task
    bg_task.start(webhook)


if __name__ == '__main__':
    client.run('TOKEN')

参考:

【讨论】:

  • 该死的你工作快:O,我正想问,如何删除以前的消息。
  • 哈哈,如果有帮助,记得接受答案,还有——只要机器人重启,之前的消息属性就会重置,你将无法删除它
  • 首先,谢谢你,我什至没有这样想。其次我得到这个错误。 AttributeError:“Webhook”对象没有属性“previous_message”。你知道我为什么会收到这个错误吗?
  • 嗯,这很奇怪,也很抱歉 - 我刚刚意识到 webhook 不能删除消息,只能编辑(在 d.py 1.6 中)如果你愿意,我会更新我的答案。
  • 没有问题,就像我说的,现在我有一些工作要做:)
猜你喜欢
  • 2021-04-30
  • 2021-04-07
  • 2021-07-07
  • 2020-11-05
  • 2021-07-29
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
相关资源
最近更新 更多