简体   繁体   English

Discord Bot GCP 云函数

[英]Discord Bot GCP Cloud Functions

I'd like to combine GCP Cloud Scheduler and Cloud Functions to send a message to a Discord Channel every week.我想结合GCP Cloud SchedulerCloud Functions每周向 Discord 频道发送消息。

Ideally the Scheduler will use a HTTP trigger and the Cloud Function will then run, sending the message to the specific channel.理想情况下,调度程序将使用 HTTP 触发器,然后云 Function 将运行,将消息发送到特定通道。

main.py:主要.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")

I've included discord in requirements.text, however, I'm getting the following errors when I test the function: error messages :我在 requirements.text 中包含了 discord,但是,当我测试 function 时出现以下错误:错误消息

  1. RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'., RuntimeError:线程'ThreadPoolExecutor-0_0'中没有当前事件循环。,
  2. RuntimeError: set_wakeup_fd only works in main thread RuntimeError: set_wakeup_fd 仅在主线程中有效
  3. TypeError: bot_function() takes 0 positional arguments but 1 was given TypeError: bot_function() 采用 0 位置 arguments 但给出了 1

It is better if you would consider webhooks with a schedule like this.如果您考虑使用类似这样的时间表的 webhook,那就更好了。

Get the webhook url from your server settings从您的服务器设置中获取 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