簡體   English   中英

Discord Bot GCP 雲函數

[英]Discord Bot GCP Cloud Functions

我想結合GCP Cloud SchedulerCloud Functions每周向 Discord 頻道發送消息。

理想情況下,調度程序將使用 HTTP 觸發器,然后雲 Function 將運行,將消息發送到特定通道。

主要.py:

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 中包含了 discord,但是,當我測試 function 時出現以下錯誤:錯誤消息

  1. RuntimeError:線程'ThreadPoolExecutor-0_0'中沒有當前事件循環。,
  2. RuntimeError: set_wakeup_fd 僅在主線程中有效
  3. TypeError: bot_function() 采用 0 位置 arguments 但給出了 1

如果您考慮使用類似這樣的時間表的 webhook,那就更好了。

從您的服務器設置中獲取 webhook url

在此處輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM