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

时间:2021-03-22 01:22:01

标签: python discord discord.py

我正在尝试将我的机器人从使用 @client.event 切换到也使用 @client.command 问题是在运行命令 .commandtest 时,没有任何反应。我在代码中添加了一个打印语句,以查看问题是否出在 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'))

3 个答案:

答案 0 :(得分:0)

这应该有效:

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')

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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