我在为discord工作的bot项目,我不需要启动,我将命令写在另一个文件中,但是当我启动bot并运行命令时,显示如下:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "nameCommand" is not found
我的命令所在的文件是:
class Comandos:
def cumprimento(self):
return self.channel.send("Olá,sou Fego! O Robô espacial!")
def historia(self):
return self.channel.send("Eu existo há apenas alguns dias!")
bot运行的主文件是:
import discord
from discord.ext import commands
from comandos import Comandos
bot = discord.Client()
bot = commands.Bot(command_prefix='fg.')
cmd = Comandos()
@bot.event
async def on_ready():
print('Logged as {0.user}'.format(bot))
@bot.on_message
async def on_message(message):
message.channel.content = message.channel.content.lower()
if message.author.bot:
return
if message.content.startswith("fg."):
try:
if message.content('cumprimento'):
await message.channel.send(cmd.cumprimento)
elif message.content('historia'):
await message.channel.send(cmd.historia)
except:
await message.channel.send('Comando não existe!')
bot.run(token)
我尝试了许多替代方法,比如在命令文件中放入@bot.commands,或者在文件中迭代循环并将arg与每个函数的名称进行比较,我真的不知道该怎么做。
为了注册一个命令,你必须在每个函数之前添加@bot.command装饰器。这将命令注册到bot。
另外,我对你的机器人有一些其他的建议,但不是你目前的问题。我认为每个函数必须在def
关键字之前有async
,以便使函数异步运行。例子:
@bot.command()
async def cuprimento(self):
return self.channel.send("Olá,sou Fego! O Robô espacial!")
此外,当您调用await message.channel.send(cmd.historia)
时,这将转换为await message.channel.send(self.channel.send("Eu existo há apenas alguns dias!"))
。基本上,当你调用函数时你已经发送了消息,所以你不需要发送那个。您可以通过只返回文本或只调用函数来解决这个问题。
从函数发送消息:
@bot.command()
def cuprimento(self):
return "Olá,sou Fego! O Robô espacial!"
#existing code
if message.content('cuprimento'):
await message.channel.send(cmd.cuprimento())
从函数返回文本:
@bot.command()
def cuprimento(self):
await self.channel.send("Olá,sou Fego! O Robô espacial!")
#existing code
if message.content('cuprimento'):
cmd.cuprimento()