我正在制作一个不和谐的机器人,目前正在开发一个调平系统。但是我一直在收到AttributeError:'LevelingSystem'对象没有属性'author'
这是它的代码
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))
您正在使用嵌齿轮,并且由于您的事件在类中,因此需要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
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句