在烧瓶内运行不和谐机器人

时间:2021-03-29 16:30:19

标签: flask discord.py bots flask-sqlalchemy

我想要一个访问 Flask 数据库 (Flask SQLAlchemy) 的冲突机器人。我想知道是否有办法让 Discord bot 通过 Flask Web 应用程序运行和管理,如何连接两者以便它们可以相互交互 谢谢,问候

这是我的烧瓶配置

# bot Client Config
client = commands.Bot(command_prefix='!', case_insensitive=True)

# Load Token Config
load_dotenv('.env')

# Token Config
TOKEN = os.getenv('TOKEN')

# Picture Extension
pict_ext = ('.jpg','.png','.jpeg')
#

# Flask Configuration
def create_app(script_info=None):
    # environtment configurations

    # module import
    from projects.dashboard.views import dashboard_blueprint

    # initialize app
    app = Flask(__name__, static_url_path='')

    # register blueprint
    app.register_blueprint(dashboard_blueprint)

    return app

1 个答案:

答案 0 :(得分:0)

使用像这样的线程模块在单独的线程中运行 discord bot

# thread function
def flask_thread(func):
    thread = Thread(target=func)
    print('Start Separate Thread From Bot')
    thread.start()

然后将其添加到主文件中

# main file

# function run flask with separate Thread
def run():
    app.run(host='0.0.0.0', port=10000, use_reloader=False)


if __name__ == '__main__':
    flask_thread(func=run)
    client.run(os.getenv('TOKEN'))

使用命令运行

python main.py

并在浏览器中查看 http://localhost:10000