我正在制造一个不和谐的机器人,目前正在开发一个调平系统。但我不断得到 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))
答案 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
。