我的discord机器人能够运行其on_message代码,但是当我尝试运行命令%ping时,它出错并说“ discord.ext.commands.errors.CommandNotFound:找不到命令“ ping””。 / p>
我已经导入了所有必需的模块,在底部写了client.run(token)并定义了我的令牌和客户端对象,我认为没有必要包含这些代码行。
我已经包含了一个client.process_commands(message)并转移了代码,我不知道该怎么办。
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
if online:
await client.change_presence(activity=discord.Game(name="Command prefix: " + compref))
else:
await client.change_presence(status=discord.Status("offline"))
@client.event
async def on_message(message):
#print(message.author)
#print(client.user)
if message.author == client.user:
await client.process_commands(message)
return
else:
#print(message.channel.name)
try:
# Create target Directory
os.mkdir(str(message.author.guild.name))
#print("Guild msg Directory Created ")
except FileExistsError:pass
#print("Guild msg Directory already exists")
try:
os.mkdir(str(message.author.guild.name) + "\\message_logs")
except FileExistsError:pass
#print("Guild msg Directory already exists")
with open(str(message.author.guild.name) + "\\message_logs\\" + str(message.channel.name) + "_message_log.json", "a") as c:
ec = (time.asctime(time.localtime(time.time())) + "; " + str(message.author.name) + ": " + message.content).encode("utf-8")
#print(ec)
c.write(str(ec))
c.write("\n")
c.close()
await client.process_commands(message)
@client.command
async def ping(ctx):
await ctx.send(f"pong {round(client.latency * 1000)}ms")
server = ctx.message.author.guild
server_name = server.name
server_id = server.id
server_owner = server.owner.name
print("")
print("hi")
print("server name: {}\n"
"server id: {}\n"
"server owner: {}"
.format(server_name, server_id, server_owner))
print("")
try:
# Create target Directory
os.mkdir(str(server.name))
print("Guild Directory ", str(server.name), " Created ")
except FileExistsError:pass
#print("Guild Directory ", str(server.name), " already exists")
with open(str(server.name) + "\\" + str(server.name) + "_info.json", "w") as s:
e = "Server name: " + server.name + "\n" + "Server ID: " + str(server_id) + "\n" + "Server Owner: " + server_owner
#print(e)
s.write(e)
s.close()
await client.process_commands(message)
答案 0 :(得分:0)
Bot.command
是一个返回装饰器的函数,而不是装饰器本身。您需要调用它:
@client.command()
async def ping(ctx):
await ctx.send(f"pong {round(client.latency * 1000)}ms")
server = ctx.message.author.guild
server_name = server.name
server_id = server.id
server_owner = server.owner.name
print("")
print("hi")
print("server name: {}\n"
"server id: {}\n"
"server owner: {}"
.format(server_name, server_id, server_owner))
print("")
try:
# Create target Directory
os.mkdir(str(server.name))
print("Guild Directory ", str(server.name), " Created ")
except FileExistsError:pass
#print("Guild Directory ", str(server.name), " already exists")
with open(str(server.name) + "\\" + str(server.name) + "_info.json", "w") as s:
e = "Server name: " + server.name + "\n" + "Server ID: " + str(server_id) + "\n" + "Server Owner: " + server_owner
#print(e)
s.write(e)
s.close()
# Remove the process_commands, we're already in a command