1. <tfoot id='VNk16'></tfoot>
      • <bdo id='VNk16'></bdo><ul id='VNk16'></ul>
      <legend id='VNk16'><style id='VNk16'><dir id='VNk16'><q id='VNk16'></q></dir></style></legend>

      <small id='VNk16'></small><noframes id='VNk16'>

      <i id='VNk16'><tr id='VNk16'><dt id='VNk16'><q id='VNk16'><span id='VNk16'><b id='VNk16'><form id='VNk16'><ins id='VNk16'></ins><ul id='VNk16'></ul><sub id='VNk16'></sub></form><legend id='VNk16'></legend><bdo id='VNk16'><pre id='VNk16'><center id='VNk16'></center></pre></bdo></b><th id='VNk16'></th></span></q></dt></tr></i><div id='VNk16'><tfoot id='VNk16'></tfoot><dl id='VNk16'><fieldset id='VNk16'></fieldset></dl></div>

      Python + Flask + Discord:如何通过flask端点通过discord发

      时间:2023-10-10
        <legend id='ptiPF'><style id='ptiPF'><dir id='ptiPF'><q id='ptiPF'></q></dir></style></legend>
        <tfoot id='ptiPF'></tfoot>

        <small id='ptiPF'></small><noframes id='ptiPF'>

        • <bdo id='ptiPF'></bdo><ul id='ptiPF'></ul>
          <i id='ptiPF'><tr id='ptiPF'><dt id='ptiPF'><q id='ptiPF'><span id='ptiPF'><b id='ptiPF'><form id='ptiPF'><ins id='ptiPF'></ins><ul id='ptiPF'></ul><sub id='ptiPF'></sub></form><legend id='ptiPF'></legend><bdo id='ptiPF'><pre id='ptiPF'><center id='ptiPF'></center></pre></bdo></b><th id='ptiPF'></th></span></q></dt></tr></i><div id='ptiPF'><tfoot id='ptiPF'></tfoot><dl id='ptiPF'><fieldset id='ptiPF'></fieldset></dl></div>
              <tbody id='ptiPF'></tbody>

                本文介绍了Python + Flask + Discord:如何通过flask端点通过discord发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试发送一条不和谐的消息,通过 Flask 端点激活

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

                我在调用 http://127.0.0.1:5000/send

                RuntimeError: There is no current event loop in thread 'Thread-4'.
                

                我有以下(最少的)代码

                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()
                

                推荐答案

                也许您应该考虑使用 webhook 而不是机器人.这是一个简单的示例,您应该对其实现烧瓶.

                Maybe you should consider using webhooks instead of a bot. Here is a simple example you should implement flask to it.

                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
                

                这篇关于Python + Flask + Discord:如何通过flask端点通过discord发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Discord.py Bot 重命名 VoiceChannel 有时只能工作 下一篇:NameError: name 'client' is not defined 我该如何解

                相关文章

                最新文章

                  <bdo id='0VzLO'></bdo><ul id='0VzLO'></ul>

                  1. <small id='0VzLO'></small><noframes id='0VzLO'>

                    <legend id='0VzLO'><style id='0VzLO'><dir id='0VzLO'><q id='0VzLO'></q></dir></style></legend>
                  2. <tfoot id='0VzLO'></tfoot>

                  3. <i id='0VzLO'><tr id='0VzLO'><dt id='0VzLO'><q id='0VzLO'><span id='0VzLO'><b id='0VzLO'><form id='0VzLO'><ins id='0VzLO'></ins><ul id='0VzLO'></ul><sub id='0VzLO'></sub></form><legend id='0VzLO'></legend><bdo id='0VzLO'><pre id='0VzLO'><center id='0VzLO'></center></pre></bdo></b><th id='0VzLO'></th></span></q></dt></tr></i><div id='0VzLO'><tfoot id='0VzLO'></tfoot><dl id='0VzLO'><fieldset id='0VzLO'></fieldset></dl></div>