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

Python Discord Bot无法加入语音频道,命令无响应求助

问题排查与修复方案

1. 核心问题:Cog加载方式错误(main.py)

你在main.py中使用asyncio.run(cogs[i].setup(client))加载Cog的方式是错误的。asyncio.run会创建全新的事件循环,而Discord Bot本身已经在运行独立的事件循环,这会导致Cog无法正确注册,所有相关命令自然不会响应。

替换main.py中的Cog加载代码:

import discord
from discord.ext import commands
import os
import requests
import json
import random
from replit import db
from stayAlive import keep_alive

client = commands.Bot(command_prefix='?', intents=discord.Intents.all())

@client.event
async def on_ready():
    # Bot就绪后加载音乐Cog
    await client.load_extension('music')
    print(f'已登录为 {client.user}')

keep_alive()
client.run('你的Bot令牌')  # 确保此处填写正确的Bot令牌

2. 修复join命令逻辑漏洞

你的join命令中,若用户不在语音频道,发送提示后未终止函数,后续代码会尝试访问ctx.author.voice.channel,触发AttributeError导致命令执行失败,甚至提示消息都无法正常发送。修改join命令:

@commands.command()
async def join(self, ctx):
    if ctx.author.voice is None:
        await ctx.send('You are not in a voice channel!')
        return  # 终止函数,避免后续报错
    voiceChannel = ctx.author.voice.channel
    if ctx.voice_client is None:
        await voiceChannel.connect()
    else:
        await ctx.voice_client.move_to(voiceChannel)

3. 额外检查项

  • 确认Bot已被邀请到目标服务器,且拥有语音连接权限和文本消息发送权限
  • 发送命令时严格使用设定的前缀?(例如?join)
  • 登录Discord开发者门户,确保Bot的Privileged Gateway Intents已全部手动开启(代码中使用Intents.all()但需后台配置生效)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 15:25:44