python - 显示用户级别系统 Discord.py

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

我正在开发添加级别系统

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。开发者社区 ^)

最佳答案

从另一个线程回答。

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")

`

关于python - 显示用户级别系统 Discord.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54896139/

相关文章:

Python:有选择地重载属性?

python - Discord python 解禁成员

python - 如何使 rest_framework 序列化程序不允许多余的字段?

python - Pandas 绘图错误地对图表上的分箱值进行排序

python-3.x - 如何使我的 speech_recognition 暂停阈值正常工作

python - 更新附加组件需要重新安装 Orange3

javascript - puppeteer .match,如何让控制台记录字符串的所有内容而不仅仅是我的搜索

reactjs - 让用户以编程方式加入不和谐服务器

python - 禁用 django 消息框架后出现问题

python - 流派和类型过滤器在 Soundcloud API 中不起作用?