首页 > 解决方案 > 为什么我不能使用命令前缀?

问题描述

每次我尝试使用commands.Bot(command_prefix=''),程序都会将其读取为错误。例如,使用下面的代码,它会出现

忽略命令无异常:discord.ext.commands.errors.CommandNotFound:找不到命令“-ping” 忽略命令无异常:discord.ext.commands.errors.CommandNotFound:找不到命令“ping”

并且在我想让机器人说的话(Pong!)之前重复了几次,在服务器中发送了 2 次或更多次......

\我认为它可能是循环的?我不确定,但我让它工作了一次,但我等待的时间越长,每次使用它发送更多响应的命令?-我上次尝试时发送了 16 'Pong's...对此我能做些什么吗?\

我怎样才能解决这个问题?

from discord.ext import commands
client = commands.Bot(command_prefix='-')

@client.event
async def on_ready():
    print("Bot is ready for use...")

@client.command()
async def ping(ctx):
    await ctx.send('Pong')

client.run('TOKEN')

标签: pythoncommanddiscorddiscord.pydiscord.py-rewrite

解决方案


问题不是来自你的前缀,你只是在client.command装饰器之后忘记了括号:

from discord.ext import commands
client = commands.Bot(command_prefix='-')

@client.event
async def on_ready():
    print("Bot is ready for use...")

@client.command()
async def ping(ctx):
    await ctx.send('Pong')

client.run('TOKEN')

装饰器client.event没有任何参数,所以你不需要括号,但client.command()可以有像name=, brief=, description=, aliases, ... 这样的参数,所以你需要括号。^^


推荐阅读