我创建了一个不和谐级别的机器人,但是当我使用 $rank
时,它会出错,说 get_point
未定义
出租车有人帮我吗?
import discord,time,json
from discord.ext import commands
class Lvl(commands.Cog):
def __init__(self,bot):
self.bot = bot
try:
with open("users.json") as fp:
users = json.load(fp)
except Exception:
users = {}
def save_users():
with open("users.json", "w+") as fp:
json.dump(users, fp, sort_keys=True, indent=4)
def add_points(user: discord.User, points: int):
id = user.id
if id not in users:
users[id] = {}
users[id]["points"] = users[id].get("points", 0) + points
save_users()
def get_points(user: discord.User):
id = user.id
if id in users:
return users[id].get("points", 0)
return 0
@commands.Cog.listener()
async def on_message(self , message):
if message.author == self.bot.user:
return
add_points(message.author, 1)
@commands.command()
async def rank(ctx,self):
await ctx.send(f"you have {get_points(message.author)} point")
def setup(bot):
bot.add_cog(Lvl(bot))
答案 0 :(得分:0)
你的方法在一个类中,所以在定义时使用 self
def get_points(self, user: discord.User):
id = user.id
if id in users:
return users[id].get("points", 0)
return 0
在调用 get 点时,您也需要使用 self
@commands.command()
async def rank(self, ctx):
await ctx.send(f"you have {self.get_points(message.author)} point")