You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用discord.py实现Discord Bot检测其他Python编写的Discord Bot运行状态

检测Discord Bot运行状态的几种实现方式

方法1:通过Discord用户状态判断

Discord Bot本质是平台上的用户账号,可直接获取目标Bot的用户对象,查看其在线状态来判断是否运行。

实现步骤:

  1. 在Discord开发者门户开启Presence Intent和Server Members Intent,并在代码中配置对应intents。
  2. 使用discord.Client或commands.Bot实例获取目标Bot的用户对象。
  3. 检查用户的status属性判断在线状态。

代码示例:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.presences = True
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents)

TARGET_BOT_ID = 123456789012345678  # 替换为目标Bot的ID

@bot.command()
async def check_bot(ctx):
    try:
        target_bot = await bot.fetch_user(TARGET_BOT_ID)
        # 状态选项:online/idle/dnd/offline
        if target_bot.status != discord.Status.offline:
            await ctx.send(f"目标Bot当前状态:{target_bot.status}(正常运行)")
        else:
            await ctx.send("目标Bot已离线")
    except discord.NotFound:
        await ctx.send("未找到目标Bot账号")

bot.run("你的Bot令牌")

方法2:发送心跳命令验证

如果目标Bot提供了!ping这类响应式命令,可以定时发送该命令并监听回复,以此判断是否在线。

代码示例:

import discord
from discord.ext import commands, tasks

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)

TARGET_BOT_ID = 123456789012345678
CHECK_CHANNEL_ID = 987654321098765432  # 用于发送检测消息的频道ID

@tasks.loop(minutes=5)  # 每5分钟检测一次
async def check_bot_status():
    channel = bot.get_channel(CHECK_CHANNEL_ID)
    if not channel:
        print("未找到指定频道")
        return
    
    msg = await channel.send("!ping")
    try:
        # 等待目标Bot回复,超时10秒
        reply = await bot.wait_for('message', timeout=10.0, 
                                  check=lambda m: m.author.id == TARGET_BOT_ID)
        if "Pong" in reply.content:  # 根据目标Bot的实际回复内容调整
            print("目标Bot运行正常")
        else:
            print("目标Bot回复异常")
    except TimeoutError:
        print("目标Bot无响应,可能已离线")

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")
    check_bot_status.start()

bot.run("你的Bot令牌")

方法3:检查Bot是否在指定频道(适用于常驻频道的Bot)

如果目标Bot会常驻某个语音/文本频道,可以检查频道成员列表中是否存在该Bot。

代码示例(语音频道检查):

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.voice_states = True

bot = commands.Bot(command_prefix="!", intents=intents)

TARGET_BOT_ID = 123456789012345678
TARGET_VOICE_CHANNEL_ID = 112233445566778899

@bot.command()
async def check_voice_bot(ctx):
    voice_channel = bot.get_channel(TARGET_VOICE_CHANNEL_ID)
    if not voice_channel:
        await ctx.send("指定语音频道不存在")
        return
    
    bot_in_channel = any(member.id == TARGET_BOT_ID for member in voice_channel.members)
    if bot_in_channel:
        await ctx.send("目标Bot在语音频道中,运行正常")
    else:
        await ctx.send("目标Bot不在语音频道中,可能已离线")

bot.run("你的Bot令牌")

注意事项:

  • 确保你的Bot拥有足够权限,比如读取成员列表、发送消息等。
  • 使用Presence Intent时,必须在Discord开发者门户的Bot页面开启该权限,否则无法获取用户状态。
  • 定时检测不要设置过短间隔,避免触发Discord API频率限制。

内容的提问来源于stack exchange,提问作者Willfa10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:01:01