discord.py提示CommandNotFound 命令rs未找到错误怎么解决
问题排查与修复方案
核心错误原因
- 命令未被正确注册到Bot实例
你使用的
@commands.command()装饰器仅适用于Cog扩展类内部的命令注册,直接挂载到全局Bot实例的命令必须使用@client.command()装饰器,你的rs命令没有被Bot识别到,所以触发CommandNotFound报错。
- 命令函数参数不符合规范
discord.py的命令函数第一个参数固定为
Context上下文对象(通常命名为ctx),你将参数命名为message会导致后续调用逻辑出错,发送消息需要调用ctx.send()而非message.send()。
- 存在其他运行隐患
- 你使用的
contatore5、tag5、support5等全局变量未提前初始化,运行时会抛出NameError- 冗余导入
from discord import client和你定义的Bot实例变量client重名,会产生命名冲突- discord.py v2.0+版本需要手动开启消息内容意图,否则机器人无法读取消息识别命令
修正后完整代码
import discord from discord.ext import commands from datetime import datetime print("holaaaaaa") token = "替换为你的机器人token" # 开启消息内容意图,需同时在Discord开发者后台Bot设置页开启Message Content Intent开关 intents = discord.Intents.default() intents.message_content = True client = commands.Bot(command_prefix=".", intents=intents) # 提前初始化所有全局变量 contatore1 = 0 redstar = "@everyone Vorrei fare una rs " partecipanti = "\n Partecipanti: " support = "0/4" contatore5 = 0 tag5 = "" support5 = "0/4" contatore6 = 0 tag6 = "" support6 = "0/4" @client.event async def on_ready(): print(f"{client.user} ONLINE (ID {client.user.id})") # 替换为client.command()装饰器 @client.command() async def rs(ctx): if ctx.channel.id == 605465213956915218: global redstar, contatore5, tag5, support5, partecipanti redstar = "@everyone Vorrei fare una " + ctx.channel.name tag5 = "\n" + ctx.author.mention contatore5 = 1 if contatore5 == 1: support5 = "1/4" await ctx.send(redstar + partecipanti + support5 + tag5) if ctx.channel.id == 605465543134150657: global contatore6, tag6, support6 redstar = "@everyone Vorrei fare una " + ctx.channel.name tag6 = "\n" + ctx.author.mention contatore6 = 1 if contatore6 == 1: support6 = "1/4" await ctx.send(redstar + partecipanti + support6 + tag6) client.run(token)
内容的提问来源于stack exchange,提问作者Giovanni Evangelista
相关产品推荐
相关产品推荐

