I am trying to make a Discord bot that listens for a webhook and sends messages in a discord channel when it receives a ping from the webhook. It seems that due to the way threads work in python, I cannot run both the Quart server and the Discord.py bot on the main thread. I am trying to migrate both the bot and server onto their own threads. Quart is an async implementation of Flask, so that thread would need to be async. Does anybody know how I can accomplish this? I have tried several methods none of which work
Asked
Active
Viewed 1,652 times
1 Answers
2
Here's a short bit of code I wrote. It should all work. If you don't use .env files you can just copy-paste your token in. However, I would strongly recommend using .env files for secret things.
import discord
import os
from discord.ext import commands
from dotenv import load_dotenv
from quart import Quart
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
PREFIX = "!"
bot = commands.Bot(command_prefix=PREFIX)
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
@bot.command(name="hi")
async def hi_command(ctx):
await ctx.channel.send("hello")
app = Quart(__name__)
@app.route("/")
async def hello():
return "hello world"
bot.loop.create_task(app.run_task())
bot.run(TOKEN)

Bento Bot
- 78
- 1
- 5
-
1I am not able to kill this server. ctrl+c isn't breaking it – Shubhank Saxena Jan 17 '22 at 11:06