Discord Bot GCP Cloud Functions

Viewed 503

I'd like to combine GCP Cloud Scheduler and Cloud Functions to send a message to a Discord Channel every week.

Ideally the Scheduler will use a HTTP trigger and the Cloud Function will then run, sending the message to the specific channel.

main.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:

  1. RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.,
  2. RuntimeError: set_wakeup_fd only works in main thread
  3. TypeError: bot_function() takes 0 positional arguments but 1 was given
1 Answers

It is better if you would consider webhooks with a schedule like this.

Get the webhook url from your server settings

enter image description here

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
Related