首页 > 解决方案 > 为什么我的机器人在加入语音时没有错误?不和谐.py

问题描述

if message.content == "%join":
      @client.command()
      async def join(ctx):
        channel = ctx.author.voice.channel
        await channel.connect()

当我输入 %join 时出现错误:

    await coro(*args, **kwargs)
  File "main.py", line 102, in on_message
    @client.command()
AttributeError: 'Client' object has no attribute 'command'

我加入了语音频道,但仍然出现同样的错误。discord.Client() 被重命名为 client 所以它 client = discord.Client() 我做了

标签: pythondiscorddiscord.pybots

解决方案


您应该单独使用 @client.command 而不是在 if 语句中。这是你应该如何做的。

##--Voice Chat Functions--##
@client.command(aliases=['jvc'])
async def joinvc(ctx):
    try:
        channel = ctx.message.author.voice.channel
    except AttributeError:
        await ctx.send("You're Not in a Voice Chat Lmao :joy:"
                       )
    global voice
    voice = get(client.voice_clients, guild=ctx.guild)
    voice = await channel.connect(timeout=3600)


@client.command(aliases=["lvc"])
async def leavevc(ctx):
    global voice
    channel = ctx.message.author.voice.channel
    voice = await voice.disconnect()

如果您在使用 on_message 和命令时遇到问题,请在 on_message 末尾添加await client.process_commands(message)

@client.event
async def on_message(message):
    if message.content == "hi":
        await message.channel.send("Hi!!!!")
    await client.process_commands(message)

@client.command()
...
...
...

推荐阅读