Не знаю хорошо ли это или плохо но я сделал вот так:
import database
import db_levels
import time
from disnake import Embed, Colour
from disnake.ext import commands
def msg_delay(user_id, self, current_time):
if user_id in self.last_xp_update:
time_since_last_update = current_time - self.last_xp_update[user_id]
if time_since_last_update < self.xp_delay:
return False
return True
async def get_role_ids(user_id, user_name, db_levels, database):
lvl = database.get_lvl(user_id, user_name)
id_add_role = db_levels.get_id_add_role(lvl) if db_levels.get_id_add_role(lvl) else None
id_delete_role = db_levels.get_id_delete_role(lvl) if db_levels.get_id_delete_role(lvl) else None
return id_add_role, id_delete_role
async def manage_roles(member, guild, id_add_role, id_delete_role):
if id_add_role:
add_role = guild.get_role(id_add_role)
if add_role:
await member.add_roles(add_role)
if id_delete_role:
del_role = guild.get_role(id_delete_role)
if del_role:
await member.remove_roles(del_role)
class XpController(commands.Cog):
def __init__(self, bot: commands.Bot, guilds, xp_delay):
self.bot = bot
self.guilds = guilds
self.xp_delay = xp_delay
self.last_xp_update = {}
@commands.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
return
user_id = message.author.id
user_name = message.author.name
xp_amount = 0
current_time = time.time()
if message.channel.id == 1233498806881095824:
if message.attachments:
xp_amount = 150
else:
xp_amount = 100
if not msg_delay(user_id, self, current_time):
await message.channel.send("Ожидайте перед следующим обновлением опыта.")
return
self.last_xp_update[user_id] = current_time
database.increase_xp(user_id, user_name, xp_amount)
guild_id = message.guild.id
guild = self.bot.get_guild(guild_id)
member = guild.get_member(user_id)
id_add_role, id_delete_role = await get_role_ids(user_id, user_name, db_levels, database)
if member:
await manage_roles(member, guild, id_add_role, id_delete_role)