简体   繁体   English

AttributeError: 'Member' object 没有属性 'client'

[英]AttributeError: 'Member' object has no attribute 'client'

I am a new user of discord.py, and i'm facing an issue with a simple code:我是 discord.py 的新用户,我遇到了一个简单代码的问题:

from dotenv import load_dotenv

import discord
from discord.ext.commands import Bot
import os

load_dotenv()


class Bothoven:
    def __init__(self):
        self.version = 0.1
        self.client = discord.Client()
        self.bot = Bot(command_prefix='$')

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

        @self.bot.command()
        async def test(ctx):
            print(ctx)
            await ctx.send("oui maitre")

        self.bot.run(os.getenv("BOT_TOKEN"))

When I run it, everything seems good, the bot prints:当我运行它时,一切似乎都很好,机器人会打印:

We have logged in as Bothoven#9179

But when I type something in the guild, it rises an exception:但是当我在公会中输入内容时,它会出现异常:

Ignoring exception in on_message
Traceback (most recent call last):
  File "D:\PROJETS\Bothoven\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "D:\PROJETS\Bothoven\venv\lib\site-packages\discord\ext\commands\bot.py", line 942, in on_message
    await self.process_commands(message)
  File "D:\PROJETS\Bothoven\venv\lib\site-packages\discord\ext\commands\bot.py", line 935, in process_commands
    if message.author.client:
AttributeError: 'Member' object has no attribute 'client'

I understand the error, and I agree that author has no attribute client, but this statement is in bot.py, a library file.我理解这个错误,我同意作者没有属性客户端,但是这个声明在 bot.py 这个库文件中。

Have you some idea of how to proceed?你知道如何进行吗?

I'm runing python3.8 and discord.py 1.6我正在运行 python3.8 和 discord.py 1.6

EDIT :编辑

Despite pip was telling me all packages were up to date, deleting my venv and rebuilding it resolved my issue.尽管 pip 告诉我所有软件包都是最新的,删除我的 venv 并重建它解决了我的问题。

There are a few things wrong with your Cog setup:您的 Cog 设置有一些问题:

  • You have to use commands.Cog.listener() to exploit events您必须使用commands.Cog.listener()来利用事件
  • To create commands, use the commands.command() decorator要创建命令,请使用commands.command()装饰器
  • Your self.bot definition is wrong and you don't need self.client您的self.bot定义错误,您不需要self.client

Here how you would setup your cog correctly:在这里你将如何正确设置你的齿轮:

from os import environ
from discord.ext import commands 

bot = commands.Bot(command_prefix='$')

class Bothoven(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready():
        print('Logged in as {0.user}'.format(self.bot))

    @commands.command()
    async def test(ctx):
        print(ctx)
        await ctx.send("Oui maître")

bot.add_cog(Bothoven(bot))
bot.run(environ["BOT_TOKEN"])

You can find more about cogs here .您可以在此处找到有关齿轮的更多信息。

EDIT: Turns out it was a venv issue, re-building it solved the problem.编辑:原来这是一个 venv 问题,重新构建它解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM