我创建了一个discord机器人,该机器人具有一个供用户升级的等级系统,他们可以检查其等级并从存储在json文件中的每条消息中获取exp。我想在此代码中添加一个函数,以在特定级别上更改用户角色。我了解我需要有一个涉及lvl_end变量的if语句,但是我不太确定这段代码的外观。这是我的代码,对此我真的非常感谢。
import discord
import json
import asyncio
import os
import time
import random
from discord.ext import commands
TOKEN =
client = commands.Bot(command_prefix = '!')
@client.event
async def on_ready():
print("Bot is online and ready to connect to server")
await client.change_presence(game=discord.Game(name='SHADOWSMAR is cool!'))
@client.command(pass_context=True)
async def clear(ctx, amount=100):
channel = ctx.message.channel
messages = []
async for message in client.logs_from(channel, limit=int(amount)):
messages.append(message)
await client.delete_messages(messages)
await client.say('Messages deleted')
@client.event
async def on_member_join(member):
role = discord.utils.get(member.server.roles, name='Tourist')
await client.add_roles(member, role)
with open('users.json', 'r') as f:
users = json.load(f)
await update_data(users, member)
with open('users.json', 'w') as f:
json.dump(users, f)
@client.event
async def on_message(message):
await client.process_commands(message)
with open('users.json', 'r') as f:
users = json.load(f)
upgrade = random.randint(5,10)
await update_data(users, message.author)
await add_experience(users, message.author, upgrade)
await level_up(users, message.author, message.channel)
with open('users.json', 'w') as f:
json.dump(users, f)
#Update data
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
#Get xp per message
async def add_experience(users, user, exp):
users[user.id]['experience'] += exp
#Level up
async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = users[user.id]['level']
lvl_end = int(experience ** 1.75)
if lvl_start < lvl_end and lvl_end == 5 or lvl_end == 10 or lvl_end == 15 or lvl_end == 20:
await client.send_message(channel, '{} has leveld up to level {}'.format(user.mention, lvl_end))
users[user.id]['level'] = lvl_end
#Rank check
@client.command(pass_context=True)
async def rank(ctx, user : discord.Member=None):
if user is None:
user = ctx.message.author
with open('users.json', 'r') as f:
users = json.load(f)
lvl_end = users[user.id]['level']
exp = users[user.id]['experience']
rank = discord.Embed(name="{}'s rank is".format(user.name), colour = 0xec134b)
rank.add_field(name="{}'s rank:".format(user.name), value="{}".format(lvl_end))
rank.add_field(name="total experience points:", value="{}".format(exp), inline=True)
rank.add_field(name="Highest role", value=user.top_role)
rank.set_footer(text="Thanks for being part of the community :D")
rank.set_thumbnail(url=user.avatar_url)
await client.say(embed=rank)
client.run(TOKEN)
答案 0 :(得分:0)
您可以设置一个词典,其中用户级别将与他们的等级相对应,并在他们每次写消息时检查该等级
例如这样的东西:
from discord.utils import get
@client.event
async def on_message(message):
user = message.author
role_dict = {1:"role#1_name",2:"role#2_name",3:"role#3_name"}
roles = message.server.roles
try:
for key,value in role_dict.items():
if value in [rl.name for rl in user.roles]:
await client.remove_roles(user,get(roles,name = value))
except discord.Forbidden:
print("Bot permissions aren't high enough to remove roles from this user")
with open('users.json', 'r') as f:
users = json.load(f)
await client.add_roles(user,get(roles,name = role_dict[users[user.id]['level']]))