Skip to content

Commit

Permalink
[CustomRoles] fix position of new custom roles
Browse files Browse the repository at this point in the history
  • Loading branch information
noahkw committed Feb 1, 2024
1 parent 98d5339 commit 74fad22
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions cogs/CustomRoles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import db
from cogs import AinitMixin, CustomCog
from models import CustomRole
from models import CustomRole, CustomRoleSettings
from views import RoleCreatorView, RoleCreatorResult, CustomRoleSetup

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,6 +44,7 @@ def __init__(self, bot):
super().__init__(bot)

CustomRole.inject_bot(bot)
CustomRoleSettings.inject_bot(bot)

self._role_removal_loop.start()

Expand Down Expand Up @@ -110,6 +111,13 @@ async def creation_callback(result: RoleCreatorResult):
name=result.name,
color=Color.from_str("#" + result.color),
)
custom_role_settings = await db.get_custom_role_settings(
session, ctx.guild.id
)
await ctx.guild.edit_role_positions(
{role: custom_role_settings.role.position},
reason=f"Moving new custom role above {custom_role_settings.role}",
)
await member.add_roles(role, reason="Custom role added")

custom_role = CustomRole(
Expand Down Expand Up @@ -151,7 +159,7 @@ async def _delete_custom_role(self, session, custom_role: CustomRole) -> None:
custom_role._user,
)

@tasks.loop(minutes=1)
@tasks.loop(hours=24)
async def _role_removal_loop(self) -> None:
logger.info("running role removal task")
async with self.bot.Session() as session:
Expand Down
6 changes: 5 additions & 1 deletion models/custom_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from models.role import RoleMixin


from discord import Member
from discord import Member, Role


class CustomRole(RoleMixin, Base):
Expand All @@ -26,3 +26,7 @@ class CustomRoleSettings(GuildSettingsMixin, Base):

"""The role that gets to create custom roles/is the insertion point"""
_role = Column(BigInteger, nullable=False)

@hybrid_property
def role(self) -> Role:
return self.guild.get_role(self._role)

0 comments on commit 74fad22

Please sign in to comment.