discord.py @client.command() 似乎不起作用



我正试图将我的bot从对所有内容使用@client.event切换为同时使用@client.命令问题是运行命令.commandtest时,什么都不会发生。我在代码中放了一个print语句,看看问题是否在discord通道中写入,但它也没有打印到终端。


from discord.ext import commands
import os
import requests
import asyncio
import ctx

from keep_alive import keep_alive
client = discord.Client()
client = commands.Bot(command_prefix=".")
client.remove_command('help')

@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

@client.command()
async def commandtest(ctx):
print("command worked?")
await ctx.send('this is a test for commandtext')


keep_alive()
client.run(os.getenv('TOKEN'))

问题是,我认为我们需要阅读文档,其中写道on_message会阻止命令工作,所以我们应该在client.event的末尾使用await client.process_commands(message),它肯定会工作

这应该有效:

import discord
client = discord.Client()

当机器人准备好时,它会打印出他已登录

@client.event
async def on_ready():
print('Logged in as ' + str(client.user))

如果你收到一条不和谐的信息,就会以"."开头,发送测试消息

@client.event
async def on_message(message):
if message.content.startswith('.'):
await message.channel.send('this is a test for commandtext')

client.run('YOUR TOKEN')

您不需要client = discord.Client()client = commands.Bot(command_prefix=".")。您必须删除client = discord.Client()才能使命令工作。此外,您不需要import ctx,因为它已经在from discord.ext import commands中导入。这应该是代码的问题所在。如果你遇到任何其他问题,你可以回复这个线程。

最新更新