Improved logic to create roles for levels by default #25

This commit is contained in:
Sven Heidemann 2022-11-06 19:36:15 +01:00
parent 861a847088
commit 8118d4edc3

View File

@ -25,7 +25,7 @@ class LevelSeeder(DataSeederABC):
async def _create_level(self, level: Level, guild: Guild, server: Server):
try:
if guild.roles.where(lambda r: r.name == level.name).count() != 0:
if guild.roles.where(lambda r: r.name == level.name).count() > 0:
return
await guild.create_role(name=level.name, colour=Colour(int(level.color, 16)), hoist=False, mentionable=True, permissions=Permissions(level.permissions))
@ -36,50 +36,42 @@ class LevelSeeder(DataSeederABC):
self._logger.error(__name__, f'Creating level failed', e)
async def seed(self):
created_default = False
# create levels
for guild in self._bot.guilds:
created_default = False
if guild.roles.where(lambda r: r.name == '___ Level ___').count() == 0:
await guild.create_role(name='___ Level ___')
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
continue
levels = self._levels.find_levels_by_server_id(server.server_id)
if levels is not None and levels.count() > 0:
# create levels from db
for level in levels:
await self._create_level(level, guild, server)
self._logger.debug(__name__, f'Seeded levels')
else:
# create default levels
for level in self._default_levels:
created_default = True
await self._create_level(level, guild, server)
self._logger.debug(__name__, f'Seeded default levels')
if created_default:
continue
for level in self._default_levels:
created_default = True
await self._create_level(level, guild, server)
self._logger.debug(__name__, f'Seeded default levels')
if created_default:
return
for guild in self._bot.guilds:
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
continue
levels = self._levels.find_levels_by_server_id(server.server_id)
roles: List[Role] = List(Role)
position_above_levels = guild.roles.where(lambda r: r.name == '~~~ Level ~~~').single().position
for role in guild.roles.order_by_descending(lambda r: r.position):
if levels.where(lambda l: l.name == role.name).count() == 0:
continue
roles.append(role)
levels = levels.order_by(lambda l: l.min_xp)
position_below_levels = roles.where(lambda r: r.name == levels.order_by(lambda l: l.min_xp).first().name).single().position - 1
for role in roles:
new_position = position_below_levels + (levels.index(levels.where(lambda l: l.name == role.name).single()) - 1)
new_position = position_above_levels - (levels.index(levels.where(lambda l: l.name == role.name).single()) + 1)
if new_position <= 0:
above_role: Role = roles.where(lambda r: r.position == 1).single()
await above_role.edit(position=above_role.position + 1)
new_position = 1
try:
self._logger.debug(__name__, f'Moved {role.name} from {role.position} to {new_position}')
await role.edit(position=new_position)