AttributeError:“ LevelingSystem”对象没有属性“ author”

时间:2020-06-07 19:34:03

标签: python-3.x discord.py discord.py-rewrite

我正在制造一个不和谐的机器人,目前正在开发一个调平系统。但我不断得到 AttributeError:“ LevelingSystem”对象没有属性“作者”

这是它的代码

import discord
from discord.ext import commands
from discord.utils import get
import json
import random
import time


class LevelingSystem(commands.Cog):
    """ Leveling system for discord """

    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(message, guild):

        if message.author.bot:
            return
        else:
            with open("X:\\Code\\Projects\\Python\\AlphaWolf\\cogs\\levels.json", 'r') as f:
                levels = json.load(f)

            await update_data(levels, message.author.id)

        async def update_data(levels, user):
            for member in guild.members:
                if user not in levels:
                    levels[user] = {}
                    levels[user]["experience"] = 0
                    levels[user]["level"] = 1
                    print(" Registered {} to .json".format(user))
                    with open("X:\\Code\\Projects\\Python\\AlphaWolf\\cogs\\levels.json", 'w') as f:
                        json.dump(levels, f)

def setup(bot):
    bot.add_cog(LevelingSystem(bot))

1 个答案:

答案 0 :(得分:1)

您使用的是嵌齿轮,并且由于您的事件在类中,因此您需要在类方法中加入self关键字作为第一个arg。

class LevelingSystem(commands.Cog):
    """ Leveling system for discord """

    def __init__(self, bot): # self is used here
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(self, message, guild): # need it here as well
        # rest of the code

假设self关键字被称为message,因为它始终是类方法中的第一个参数。
因此,当您说message.author时,该程序实际上是在假设您的类中有一个名为author的属性-它会将message.author视为self.author