Replit部署Discord机器人>hello命令无响应及无法连接语音求助
问题根因
- 你使用的discord.py v2.x及以上版本要求显式开启
message_content权限才能识别前缀指令,你当前只开启了members意图,机器人无法读取消息内容自然响应不了指令 - keep_alive函数未提前导入,代码运行会直接抛出异常无法正常启动
pass_context是discord.py旧版本参数,新版本已经默认传递上下文,冗余参数不影响运行但可以删除- 你还需要在Discord开发者后台的机器人设置页,开启特权网关意图中的
Message Content Intent和Server Members Intent开关,否则代码中声明的意图不会生效
修复步骤
- 补全缺失的意图声明,添加
message_content开启配置 - 导入keep_alive函数(如果是replit平台的保活脚本,需要提前在同目录下写好keep_alive.py文件)
- 移除冗余的
pass_context参数 - 确认Discord开发者后台对应特权开关已开启
修复后完整代码
import discord import os from discord.ext import commands # 导入保活函数 from keep_alive import keep_alive intents = discord.Intents.default() intents.members = True # 新增消息内容意图开启 intents.message_content = True client = commands.Bot(command_prefix = '>', intents=intents) @client.event async def on_ready(): print('Log in as {0.user}'.format(client)) print('-------------------') @client.command() async def hello(ctx): await ctx.send("Hello Im Deemo Bot") @client.command() async def join(ctx): if ctx.author.voice: channel = ctx.message.author.voice.channel await channel.connect() else: await ctx.send('You are not in a voice channel') @client.command() async def leave(ctx): if ctx.voice_client: await ctx.guild.voice_client.disconnect() await ctx.send('Deemo left voice') else: await ctx.send('Deemo is not in voice') keep_alive() client.run(os.environ['TOKEN'])
内容的提问来源于stack exchange,提问作者Aina
相关产品推荐
相关产品推荐

