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

如何用Discord.py向指定频道发外部消息并解决会话关闭报错

解决Discord机器人重复调用报错的方案

核心问题在于你每次调用trigger时都重复启动并关闭机器人,导致第二次调用时Bot实例已处于Session closed状态,无法重新运行。而且bot.run()是阻塞方法,不关闭的话会卡住同步脚本。正确的做法是让机器人保持在线,通过异步队列解耦消息发送逻辑,避免重复启停。

修正后的完整代码

import discord
from discord.ext import commands
import asyncio
import threading

# 替换为你的机器人令牌和必要的Intents配置
TOKEN = "你的机器人令牌"
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())

# 创建异步队列,用于接收外部传来的消息任务
message_queue = asyncio.Queue()

async def message_processor():
    """后台协程,持续从队列中取出消息并发送到指定频道"""
    await bot.wait_until_ready()
    while not bot.is_closed():
        try:
            channel_id, content = await message_queue.get()
            channel = bot.get_channel(channel_id)
            if channel:
                await channel.send(content)
            # 标记任务完成
            message_queue.task_done()
        except Exception as e:
            print(f"发送消息失败: {str(e)}")
            message_queue.task_done()

@bot.event
async def on_ready():
    print(f"机器人已登录: {bot.user.name}")
    # 启动消息处理协程
    bot.loop.create_task(message_processor())

def trigger(channel_id, message_content):
    """供外部同步脚本调用的函数,将消息任务加入队列"""
    # 把频道ID和消息内容放入队列,通过线程安全的方式提交给Bot的事件循环
    asyncio.run_coroutine_threadsafe(
        message_queue.put((channel_id, message_content)),
        bot.loop
    )

def start_bot_in_background():
    """在后台线程启动机器人,避免阻塞同步脚本"""
    bot.run(TOKEN)

# 启动机器人后台线程
threading.Thread(target=start_bot_in_background, daemon=True).start()

使用说明

  • 初始化机器人:脚本启动时,机器人会在后台线程登录并保持在线,不会阻塞你的同步业务逻辑。
  • 发送消息:当API收到消息需要转发时,直接调用trigger(目标频道ID, 消息内容),函数会把消息任务加入异步队列,由机器人后台协程处理发送。
  • 多频道支持:只需传入不同的频道ID,就能用同一个机器人向多个频道推送消息,完全符合你的需求。

方案优势

  • 机器人只启动一次,彻底避免重复调用bot.run()和bot.close()导致的Session错误。
  • 异步队列实现了同步脚本和Bot异步逻辑的解耦,不会出现卡住同步脚本的情况。
  • 后台线程运行Bot,确保主业务流程不受Discord机器人逻辑的阻塞。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 19:55:21