Discord.py命令无响应:创建频道功能失效求助
问题原因及修复方案
核心问题:客户端实例冲突且未启动命令Bot
你同时初始化了discord.Client和commands.Bot两个客户端对象,但最终只调用client.run(Token)启动了普通客户端,而$hack命令是注册在bot这个命令客户端实例上的,导致Bot完全不会响应该命令。
次要问题:错误调用Guild类方法
你直接使用discord.Guild.create_text_channel,这是类方法,必须通过具体的Guild实例(比如上下文里的ctx.guild)调用,否则会触发错误。
修复后的完整代码
import os import discord from keep_alive import keep_alive from discord.ext import commands intents = discord.Intents.default() intents.message_content = True intents.members = True # 只保留commands.Bot实例,无需discord.Client bot = commands.Bot(command_prefix='$', intents=intents) @bot.event async def on_ready(): print("I'm in as " + str(bot.user)) @bot.command(name="hack") async def court_create(ctx, msg): print("Received: $hack") # 控制台调试输出 channel_name = "hack-" + str(msg) await ctx.send(channel_name) # 频道内反馈 # 使用上下文关联的服务器实例创建频道 channel = await ctx.guild.create_text_channel(name=channel_name) keep_alive() Token = os.environ['Token'] bot.run(Token) # 启动命令Bot实例
额外注意事项
- 确保在Discord开发者门户中开启Server Members Intent和Message Content Intent,否则会出现权限不足问题。
- 确认Bot的服务器角色已配置「管理频道」权限,且角色排序高于目标频道的权限设置。
内容的提问来源于stack exchange,提问作者SpaceBar2702
相关产品推荐
相关产品推荐

