title: Expose discord bot to API (Flask, FASTAPI)
body: <p>I'm building a discord bot to take commands from multiple systems and programs. I'm wanting to expose certain actions of my discord bot to REST endpoints and then execute said actions in one spot.</p>
  <pre><code>import uvicorn
  from fastapi import FastAPI
  from pydantic import BaseModel
  from typing import Optional
  from discord.ext import commands
  
  app = FastAPI()
  
  TOKEN = 'MY_TOKEN'
  
  bot = commands.Bot(command_prefix='&gt;')
  
  class Item(BaseModel):
      name: str
      description: Optional[str] = None
      price: float
      tax: Optional[float] = None
  
  @app.get(&quot;/&quot;)
  def hello():
      return {&quot;message&quot;:&quot;Hello&quot;}
  
  @app.post(&quot;/items/&quot;)
  async def create_item(item: Item):
      await send_message()
      return item
  
  @bot.event
  async def on_ready():
      print(f'{bot.user.name} has connected to Discord!')
  
  async def send_message():
      user = await bot.fetch_user(USER_ID)
      await user.send('')
  
  if __name__ == &quot;__main__&quot;:
      bot.run('BOT_TOKEN')
      uvicorn.run(app, host='0.0.0.0')
  </code></pre>
  <p>When I try to run this, I'm only seeing the bot active. I'm a little newer to python but a veteran programmer. Is this due to python's &quot;lack&quot; of multithreading? Or port usage?</p>
  <p>The end goal is to call the &quot;/items/&quot; endpoint and see a message on discord sent to me</p>
  <p><strong>EDIT</strong></p>
  <p>I tried all the answers and coming up with some of my own. The problem is multi-threading. I got frustrated with it and ended up just moving this piece to Node.js. It doesn't technically fulfill this question but was far easier than navigating python multithreading.</p>
  <p>server.js:</p>
  <pre><code>var express = require('express');
  var app = express();
  const Discord = require('discord.js');
  const client = new Discord.Client();
  
  app.get('/listUsers', function (req, res) {
  
      dm_user();
  
      res.send('hello');
  })
  
  client.on('ready', () =&gt; {
    console.log(`Logged in as ${client.user.tag}!`);
  });
  
  client.on('message', msg =&gt; {
    if (msg.content === 'ping') {
      msg.reply('pong');
    }
  });
  
  async function dm_user(id){
      var my_user = await client.users.fetch('USER_ID');
      console.log(my_user);
  }
  
  var server = app.listen(8081, function () {
     var host = server.address().address
     var port = server.address().port
     console.log(&quot;Example app listening at http://%s:%s&quot;, host, port)
     client.login('TOKEN');
  })
  </code></pre>