首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不和谐的Bot GCP云函数

不和谐的Bot GCP云函数
EN

Stack Overflow用户
提问于 2021-05-22 11:08:22
回答 1查看 567关注 0票数 2

我希望将、GCP云调度器、云函数结合起来,每周向一个不和谐的频道发送一条消息。

理想情况下,Scheduler将使用HTTP触发器,然后云函数将运行,将消息发送到特定的通道。

main.py:

代码语言:javascript
复制
import discord

def bot_function():
  client = discord.Client()
  channel_id = "CHANNEL_ID"
  @client.event
  async def on_ready():
      await client.get_channel(channel_id).send("TEST MESSAGE")
      
  client.run("API_KEY")

但是,我在requirements.text中包含了一些不一致的地方,但是,在测试函数时,我得到了以下错误:错误信息

  1. RuntimeError:线程‘ThreadPoolExecutor-0_0’中没有当前事件循环。
  2. RuntimeError: set_wakeup_fd只在主线程中工作
  3. TypeError: bot_function()接受0的位置参数,但给出了1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-22 12:26:05

如果您考虑使用像这样的时间表的webhooks,会更好。

从服务器设置中获取web钩子url

代码语言:javascript
复制
import requests #dependency

url = "<your url>" #webhook url, from here: https://i.imgur.com/aT3AThK.png

data = {}
#for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
data["content"] = "message content"
data["username"] = "custom username"

#leave this out if you dont want an embed
data["embeds"] = []
embed = {}
#for all params, see https://discordapp.com/developers/docs/resources/channel#embed-object
embed["description"] = "text in embed"
embed["title"] = "embed title"
data["embeds"].append(embed)

result = requests.post(url, json=data, headers={"Content-Type": "application/json"})

try:
    result.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)
else:
    print(f"Payload delivered successfully, code {result.status_code}.")

#result: https://i.imgur.com/DRqXQzA.png
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67648803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档