【问题标题】:Discord API messaging when another function returns a value另一个函数返回值时的 Discord API 消息传递
【发布时间】:2020-10-31 07:44:58
【问题描述】:

我正在使用 python discord API https://discordpy.readthedocs.io/en/latest/api.html,我希望有一个永远循环(如 while 循环),用函数检查网站上是否有新内容(我已经写了一个 scraper)。换句话说:如果 scraper 看到一个新帖子,它将返回一个值(链接)。我想将它与 discord 连接起来,当该函数有输出时,它将被发送到文本通道。我完全不知道该怎么做。 scraper 函数是异步的。我想到的就是创建第二个线程并登录,然后通过 selenium 手动发送消息

【问题讨论】:

  • ‘Scrapper’是指把东西扔掉的人。你是 scraper

标签: python selenium discord python-asyncio discord.py


【解决方案1】:

使用webhooks

创建一个webhook,然后每当您的爬虫获取新数据时,向您创建的 webhook url 提交一个 POST 请求,并将内容参数设置为您的数据。

例子:

import requests

WEBHOOK_URL = "https://discordapp.com/api/webhooks/123456789/qWerRYtyuqwfq" # Example webhook url

def sendToDiscord(webhookUrl, data):
    return requests.post(webhookUrl, json={'content': data})
    
data = myScraper.get_data() # Whenever there is data from your scraper
sendToDiscord(WEBHOOK_URL, data) # Send it to Discord

注意:您可以使用appropriate 参数来格式化您的消息、添加图像等...

【讨论】:

  • 真的谢谢你! @FluidLight 以某种方式将您添加到不和谐/任何时候很酷吗?我真的很想和你谈谈,如果需要的话,我会问一些问题。
猜你喜欢
  • 2021-08-14
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 2021-04-10
  • 2010-09-15
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
相关资源
最近更新 更多