我已经为我的机器人创建了一个升级系统,我希望它在您达到某个特定级别时添加一个角色。我还希望通过命令设置角色和应用它们的级别。 这是我的代码
import itertools
import json
import random
import time
import traceback
import datetime
from functools import partial
import discord
from async_timeout import timeout
from discord.ext import commands
from discord.utils import get
from random import randint
class Levels(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.loop.create_task(self.save_users())
with open('users.json', 'r') as f:
self.users = json.load(f)
async def save_users(self):
await self.bot.wait_until_ready()
while not self.bot.is_closed():
with open('users.json', 'w') as f:
json.dump(self.users, f, indent=4)
await asyncio.sleep(5)
def lvl_up(self, author_id, guild_id):
cur_xp = self.users[guild_id][author_id]['exp']
cur_lvl = self.users[guild_id][author_id]['level']
if cur_xp >= round(2 * ((0.0002 * (cur_lvl ** 4)) - (0.0801 * (cur_lvl ** 3)) + (11.221 * (cur_lvl ** 2)) + (64.6 * cur_lvl) + 130.09)):
self.users[guild_id][author_id]['level'] += 1
return True
else:
return False
@commands.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
return
if message.content.startswith('s.') is not True:
if message.guild != None:
f = open("users.json")
self.users = json.load(f)
if str(f"{message.guild.id}") not in self.users:
self.users[f"{message.guild.id}"] = {}
print(f"Guild file has been created for {message.guild.id}/{message.guild.name}")
author_id = str(message.author.id)
guild_id = str(message.guild.id)
if str(f"{message.author.id}") not in self.users[f"{message.guild.id}"]:
self.users[f"{message.guild.id}"][f"{message.author.id}"] = {}
self.users[guild_id][author_id]['level'] = 0
self.users[guild_id][author_id]['exp'] = 0
self.users[guild_id][author_id]["last_message"] = 0
if time.time() - self.users[guild_id][author_id]["last_message"] > 5:
self.users[guild_id][author_id]['exp'] += random.randint(3,7)
self.users[guild_id][author_id]["last_message"] = time.time()
if self.lvl_up(author_id, guild_id):
await message.channel.send(f"""{message.author.mention} has leveled up to level {self.users[guild_id][author_id]['level']}!""")
@commands.command()
async def rank(self, ctx, member: discord.Member = None):
member = ctx.author if not member else member
member_id = str(member.id)
guild_id = str(ctx.guild.id)
if not member_id in self.users[guild_id]:
await ctx.send(f"That user has no experience")
else:
embed = discord.Embed(color=member.color, timestamp=ctx.message.created_at)
embed.set_author(name=f"Rank - {member}", icon_url=self.bot.user.avatar_url)
embed.add_field(name="Level", value=self.users[guild_id][member_id]['level'])
embed.add_field(name="Experience", value=self.users[guild_id][member_id]['exp'])
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Levels(bot))
print('Levels Loaded')
对此我还很陌生,我尝试制作一个.json文件来存储每个服务器的设置角色和级别,但是没有用。我也环顾四周,似乎找不到任何与即时通讯相关的信息,因此我们将不胜感激。
答案 0 :(得分:0)
我尚未对其进行测试,但您正在寻找的外观如下:
# You can save this as a class variable
roles = {1: "Noobie", 10: "Experienced", 30: "master"}
# Add this to the function that checks if the user has leveled up
for level, role in roles.items():
if user_level == level:
await user.add_roles(role, reason=None, atomic=True)
# Adds the desired role
我不确定这是否是最好的方法,但是您当然可以尝试。