如何使我的不和谐用户能够使用机器人查看他们的等级

时间:2019-02-18 14:31:50

标签: python discord.py

当前,我的discord僵尸程序能够向在服务器中键入消息的用户提供exp。我希望观众能够找到自己的等级,并且还能找到机器人中经验最多的人的排行榜。

async def lvl(ctx):
     lvl = users[user.id]['lvl_start']

@client.event
async def on_message(message):
    if message.content.upper().startswith('MYLEVEL'):
        await client.send_message(message.channel, "Your level: {}".format(lvl))

尝试这样做,但我只得到十六进制数字作为回报

from discord.ext import commands
import discord
import json
import os
Client = discord.Client()
client = commands.Bot(command_prefix = "!")


@client.event
async def on_ready():
    print('Bot is coochie')

@client.event
async def on_member_join(member):
    with open('user.json.py', 'r') as f:
        users = json.load(f)

    await update_data(users, member)

    with open('user.json.py', 'w') as f:
        json.dump(users, f)


@client.event
async def on_message(message):
    with open('user.json.py', 'r') as f:
        users = json.load(f)

    await update_data(users, message.author)
    await add_experience(users, message.author,5)
    await level_up(users, message.author, message.channel)

    with open('user.json.py', 'w') as f:
        json.dump(users, f)

async def update_data(users, user):
    if not user.id in users:
        users[user.id] = {}
        users[user.id]['experience'] = 0
        users[user.id]['level'] = 1

async def add_experience(users, user, exp):
    users[user.id]['experience'] += exp

async def level_up(users, user, channel):
    experience = users[user.id]['experience']
    lvl_start = users[user.id]['level']
    lvl_end = int(experience ** (1/10))


    if lvl_start < lvl_end:
        await client.send_message(channel, '{} has leveled up to level {}' .format(user.mention, lvl_end))
        users[user.id]['level'] = lvl_end

0 个答案:

没有答案