0.3 #146

Merged
edraft merged 359 commits from 0.3 into master 2023-01-12 07:04:40 +01:00
3 changed files with 56 additions and 0 deletions
Showing only changes of commit 8aee72856c - Show all commits

View File

@ -8,6 +8,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.level.level_seeder import LevelSeeder
from modules.level.service.level_service import LevelService
class LevelModule(ModuleABC):
@ -23,3 +24,4 @@ class LevelModule(ModuleABC):
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_transient(LevelSeeder)
services.add_transient(LevelService)

View File

@ -0,0 +1 @@
# imports

View File

@ -0,0 +1,53 @@
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.container import Guild, Role, Member
from cpl_discord.service import DiscordBotServiceABC
from bot_data.model.level import Level
from bot_data.model.user import User
from bot_data.service.level_repository_service import LevelRepositoryService
from bot_data.service.user_repository_service import UserRepositoryService
class LevelService:
def __init__(
self,
logger: LoggerABC,
db: DatabaseContextABC,
levels: LevelRepositoryService,
users: UserRepositoryService,
bot: DiscordBotServiceABC
):
self._logger = logger
self._db = db
self._levels = levels
self._users = users
self._bot = bot
def get_level(self, user: User) -> Level:
levels = self._levels.get_levels_by_server_id(user.server.server_id).order_by(lambda l: l.min_xp)
return levels.where(lambda l: l.min_xp > user.xp).first()
async def set_level(self, user: User):
level_names = self._levels.get_levels_by_server_id(user.server.server_id).select(lambda l: l.name)
guild: Guild = self._bot.guilds.where(lambda g: g.id == user.server.discord_server_id).single()
level_role: Role = guild.roles.where(lambda r: r.name == self.get_level(user).name).single()
member: Member = guild.members.where(lambda m: m.id == user.discord_id).single()
for role in member.roles:
if role.name not in level_names.to_list():
continue
try:
self._logger.debug(__name__, f'Try to remove role {role.name} from {member.name}')
await member.remove_roles(role)
self._logger.info(__name__, f'Removed role {role.name} from {member.name}')
except Exception as e:
self._logger.error(__name__, f'Removing role {role.name} from {member.name} failed!', e)
try:
self._logger.debug(__name__, f'Try to add role {level_role.name} to {member.name}')
await member.add_roles(level_role)
self._logger.info(__name__, f'Add role {level_role.name} to {member.name}')
except Exception as e:
self._logger.error(__name__, f'Adding role {level_role.name} to {member.name} failed!', e)