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

Discord Bot无报错但不响应任何命令与事件问题求助

问题

我的Discord Bot运行时无任何报错,但无法响应!join、!leave、!play等命令,也不触发任何事件。该Bot昨日运行完全正常,今日突然出现此问题。

Bot代码

import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
from better_profanity import profanity
import random
import os

intents = discord.Intents.all()
intents.members = True

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

import apikeys as ak
import rapidAPI_Joker as rj

@bot.event
async def on_ready():
    print("--------------------------------")
    print("The bot is now ready for use!")
    print("--------------------------------")

@bot.event
async def on_member_join(member):
    channel = bot.get_channel(1161221526150987807)
    await channel.send(f"Hello {member.display_name}! Welcome to my discord server!")
    await channel.send(rj.setup)
    await channel.send(rj.punchline)

@bot.event
async def on_member_remove(member):
    channel = bot.get_channel(1161221526150987807)
    await channel.send(f"Goodbye {member.display_name}!")

@bot.event
async def on_message(message):
    greeting = ['Are you daft?', 'With that mouth you kiss your mother?', 'Imagine you told that to your girlfriend!']
    if profanity.contains_profanity(message.content):
        await message.delete()
        await message.channel.send(random.choice(greeting))

@bot.command(pass_context=True)
async def join(ctx):
    if ctx.author.voice:
        channel = ctx.message.author.voice.channel
        voice = await channel.connect()
        source = FFmpegPCMAudio('BO2REE.mp3')
        player = voice.play(source, after=lambda x=None: check_queue(ctx, ctx.message.guild.id))
        print('Join')
    else:
        await ctx.send('You are not in a voice channel!')

@bot.command(pass_context=True)
async def leave(ctx):
    if ctx.voice_client:
        await ctx.guild.voice_client.disconnect()
        await ctx.send('I left the voice channel!')
    else:
        await ctx.send('I\'m not in a voice channel!')

@bot.command(pass_context=True)
async def pause(ctx):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    if voice.is_playing():
        voice.pause()
    else:
        await ctx.send('There\'s no audio playing!')

@bot.command(pass_context=True)
async def resume(ctx):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    if voice.is_paused():
        voice.resume()
    else:
        await ctx.send('No audio paused!')

@bot.command(pass_context=True)
async def stop(ctx):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    voice.stop()

queues = {}

def check_queue(ctx, id):
    if queues.get(id):
        if queues[id] != []:
            voice = ctx.guild.voice_client
            source = queues[id].pop(0)
            player = voice.play(source)

@bot.command(pass_context=True)
async def play(ctx, audio):
    voice = ctx.guild.voice_client
    audio_file = audio + '.mp3'
    if not os.path.isfile(audio_file):
        await ctx.send('The specified audio file does not exist.')
        return
    source = FFmpegPCMAudio(audio_file)
    player = voice.play(source, after=lambda x=None: check_queue(ctx, ctx.message.guild.id))
    await ctx.send('Now playing: ' + audio)

@bot.command(pass_context=True)
async def queue(ctx, audio):
    voice = ctx.guild.voice_client
    audio_file = audio + '.mp3'
    if not os.path.isfile(audio_file):
        await ctx.send('The specified audio file does not exist.')
        return
    source = FFmpegPCMAudio(audio_file)
    guild_id = ctx.message.guild.id
    if guild_id in queues:
        queues[guild_id].append(source)
    else:
        queues[guild_id] = [source]
    await ctx.send(f'Added {audio_file} to queue')

bot.run(ak.bot_token)

已尝试的排查操作

  • 在Discord开发者平台重置令牌并重新粘贴到代码中;
  • 开关所有特权网关意图(Privileged Gateway Intents);
  • 在Discord开发者平台创建新Bot并沿用相同配置;
  • 重新安装discord.py包;
  • 逐个重新安装所有依赖包;
  • 确认自定义模块apikeys.py和rapidAPI_Joker.py昨日可正常工作。

解决方案

核心修复:恢复命令处理逻辑

你的on_message事件函数没有调用await bot.process_commands(message),这会直接阻止discord.py处理任何命令。自定义on_message时必须手动添加这行代码,否则命令系统会完全失效。修改后的on_message代码如下:

@bot.event
async def on_message(message):
    greeting = ['Are you daft?', 'With that mouth you kiss your mother?', 'Imagine you told that to your girlfriend!']
    if profanity.contains_profanity(message.content):
        await message.delete()
        await message.channel.send(random.choice(greeting))
    # 关键:让Bot处理命令
    await bot.process_commands(message)

额外排查项

  1. 权限验证:确认Bot在服务器中拥有发送消息、连接语音频道、语音活动等必要权限,避免因角色权限变更导致功能失效。
  2. 意图确认:在Discord开发者平台中,确保服务器成员意图和消息内容意图已启用;代码中可明确添加intents.message_content = True(即使使用Intents.all(),明确设置可避免潜在问题)。
  3. 前缀冲突测试:临时修改命令前缀(比如改为!!),测试是否因其他Bot使用相同前缀导致冲突。
  4. 自定义模块隔离:临时注释掉apikeys.py和rapidAPI_Joker.py的导入与调用代码,验证是否因模块加载异常导致Bot功能阻塞。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 04:49:56