Python + Flask + Discord: How to send a message through discord through a flask endpoint?

ise*_*arn 1 python flask discord.py

I'm trying to send a message with discord, activated through a Flask endpoint

I get the following error message when I call http://127.0.0.1:5000/send

RuntimeError: There is no current event loop in thread 'Thread-4'.
Run Code Online (Sandbox Code Playgroud)

I have the following (minimal) code

import discord
from flask import Flask, jsonify

async def my_background_task():

  for message in ['a', 'b']:
    await client.wait_until_ready()
    channel = client.get_channel(CHANNEL_ID)
    await channel.send(message)

  await client.close() 

def sendMessages():
  client = discord.Client()
  client.loop.create_task(my_background_task())
  client.run('SECRET')

app = Flask(__name__)


@app.route('/send')
def send():
  sendMessages()
Run Code Online (Sandbox Code Playgroud)

Abd*_*ziz 5

也许您应该考虑使用 webhooks 而不是机器人。这是一个简单的例子,你应该为它实现烧瓶。

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("Payload delivered successfully, code {}.".format(result.status_code))

#result: https://i.imgur.com/DRqXQzA.png
Run Code Online (Sandbox Code Playgroud)


小智 5

有时使用 webhook 无法提供所需的功能。

如果您可以从烧瓶切换到夸脱,您可以使用对异步方法的直接支持来控制事件循环,就像您在给定示例中打算做的那样。

以下代码片段在与 quart 服务器相同的事件循环中启动不和谐机器人。

@app.before_serving
async def before_serving():
    loop = asyncio.get_event_loop()
    await client.login(TOKEN) # this could be done outside of this method

    # do not use client.run
    loop.create_task(client.connect())
Run Code Online (Sandbox Code Playgroud)

一个完整的最小工作示例如下所示

import discord
import asyncio
from quart import Quart

app = Quart(__name__)
client = discord.Client()


@app.before_serving
async def before_serving():
    loop = asyncio.get_event_loop()
    await client.login(TOKEN)
    loop.create_task(client.connect())


@app.route("/send", methods=["GET"])
async def send_message():
    # wait_until_ready and check for valid connection is missing here
    channel = client.get_channel(CH_ID)
    await channel.send('XYZ')
    return 'OK', 200


app.run()
Run Code Online (Sandbox Code Playgroud)

客户端的连接可以由方法本身触发,但重要的是不要在每个请求时创建新任务/send