【问题标题】:show user level system Discord.py显示用户级系统 Discord.py
【发布时间】:2019-07-20 14:58:37
【问题描述】:

我正在开发添加级别系统

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import json
import os
import asyncio
import time
import random
from discord import Game
import math, time

Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()

os.chdir(r'C:\Users\Hema\Desktop\Level')

@client.event
async def on_ready():
print('lvl ready')

@client.event
async def on_message(message):

with open('users.json', 'r') as f:
    users = json.load(f)

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



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


if message.content == 'system lvl':
    await client.send_message(message.channel, '{} level up {}'.format(message.author.mention, lvl_end))
    users[message.author.id]['level'] = lvl_end

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

#code

await update_data(users, member)

with open('users.json','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(users[message.author.id]["experience"] ** (1/4))

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


client.run("Token")

完美工作^)

但是当我想知道用户级别时出现问题,通过键入“system lvl” 显示此错误

=========== RESTART: C:\Users\Darzy\Desktop\Level\Bot_lvl.py ===========

lvl ready
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Darzy\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\Darzy\Desktop\Level\Bot_lvl.py", line 31, in on_message
await level_up(users, message.author, message.channel)
File "C:\Users\Darzy\Desktop\Level\Bot_lvl.py", line 67, in level_up
lvl_end = int(users[message.author.id]["experience"] ** (1/4))
NameError: name 'message' is not defined.

如果可以解决这个问题,我只是想解决这个问题,我尝试了很多方法,但都没有奏效: 除此之外,只是为用户修改了作者,仍然是同样的问题。 我相信stackoverflow。开发者社区^)

【问题讨论】:

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


    【解决方案1】:

    据我从您的代码中可以看出,通道未在该函数中定义。现在,我对 Python discord 库并不熟悉,但您似乎可以使用 message.channel 来响应发送消息的频道。

    【讨论】:

    • ,出现错误:NameError: name 'user' is not defined
    • 同样,user 没有定义在函数的范围内,所以你会想要使用message.author.mention。此外,user.id 未定义,因此您需要使用 message.author.id
    • Oki 最后一个:NameError: name 'lvl_end' is not defined ,我累了:message.author.lvl_end
    • 对于lvl_end,您似乎将其定义为int(users[user.id]["experience"] ** (1/4)),因此您应该能够将其用作int(users[message.author.id]["experience"] ** (1/4))
    • 另外,您需要将with 块移动到if 块上方,因为users 尚未定义。
    【解决方案2】:

    从另一个线程回答。

    Experience (XP) not working for all users JSON Discord.PY

    import json
    import discord
    
    client = discord.Client()
    
    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]["level"] = users[id].get("level", 0) + points
    #  print("{} now has {} level".format(user.name, users[id]["level"]))
    save_users()
    
    def get_points(user: discord.User):
    id = user.id
    if id in users:
        return users[id].get("level", 0)
    return 0
    
    @client.event
    async def on_message(message):
    if message.author == client.user:
        return
    # print("{} sent a message".format(message.author.name))
    # if message.content.lower().startswith("!points"):
    #     msg = "You have {} points!".format(get_points(message.author))
    #     await client.send_message(message.channel, msg)
    add_points(message.author, 0.01)
    
    if message.content == '!Lvl':
        msg = "Your Lvl {}!".format(get_points(message.author))
        await client.send_message(message.channel, msg)        
    
    
    client.run("token")
    

    `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2021-06-12
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      相关资源
      最近更新 更多