【问题标题】:discordpy: Why is the bot keep going offline in FastAPI?discordpy:为什么机器人在 FastAPI 中一直离线?
【发布时间】:2021-10-14 14:13:53
【问题描述】:

我在 FastAPI 中使用 discord bot 只是为了检查当前某些语音频道中的成员。

我在 AWS EC2 中使用 nohup python server.py & 运行它

但是一段时间后机器人刚刚离线(不是 fastapi),我想检查机器人是否离线以重新连接以及实时消息但 on_message 从未调用过

提前致谢。

import asyncio

import discord
import uvicorn
from discord.ext import commands
from fastapi import FastAPI


app = FastAPI()

# Discord
TOKEN = ""
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="#", intents=intents)
names = ["~", "~", "~"]
channels = ["~", "~", "~"]

@app.on_event("startup")
async def startUp():
    asyncio.create_task(bot.start(TOKEN))

def getDiscordUsers():
    json = {name: [] for name in names}

    for i in range(0, 3):
        channel = bot.get_channel(channels[i])
        members = channel.members
        for member in members:
            selfMuted = member.voice.self_mute
            if member.bot:
                continue
            if member.nick == None:
                json[names[i]].append(f"{member.name}")
            else:
                json[names[i]].append(member.nick)

    return json

@bot.event
async def on_message(message):
    print(message)  # it seems never called becuz of fastapi coroutine?


@app.get("/discord")
async def get_users():
    # if not bot.is_ready():
    #     print("called")
    #     asyncio.create_task(bot.connect())
    return getDiscordUsers()


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")

【问题讨论】:

  • 我知道 discord.py,但对快速 API ext 知之甚少,但我很确定如果你自己托管机器人,它会做同样的事情,你必须 ping 机器人或有某种请求机器人或来自机器人,以使其不会脱机

标签: python discord.py


【解决方案1】:

在使用 Discord.py 和 FastAPI 时,在机器人的循环中调用 serve() 似乎更好

我什至可以使用 bot.command,事件的东西

import discord
from discord.ext import commands
from fastapi import FastAPI
from uvicorn import Config, Server

TOKEN = "TOKEN"
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="#", intents=intents)

@bot.event
async def on_message(message):
    if message.content.startswith("["):
        print("yes")


@bot.event
async def on_ready():
    print("Ready)")


async def getDiscordUsers():
    json = {name: [] for name in names}

    for i in range(0, 3):
        channel = bot.get_channel(channels[i])
        members = channel.members
        for member in members:
            # selfMuted = member.voice.self_mute
            if member.bot:
                continue
            if member.nick == None:
                json[names[i]].append(member.name)
            else:
                json[names[i]].append(member.nick)

    return json


app = FastAPI()


@app.get("/discord")
async def send_msg():
    return await getDiscordUsers()


config = Config(app=app, host="0.0.0.0", port=8000, log_level="critical")
server = Server(config)

bot.loop.create_task(server.serve())
bot.run(TOKEN)

【讨论】:

    猜你喜欢
    • 2021-06-01
    • 2021-06-01
    • 2011-09-09
    • 2019-04-17
    • 2021-12-21
    • 2020-07-13
    • 2021-08-01
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多