Skip to content

Commit

Permalink
[CustomRoles] remove custom role on user block
Browse files Browse the repository at this point in the history
  • Loading branch information
noahkw committed Feb 2, 2024
1 parent ebc5f1d commit 1bde2b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
24 changes: 18 additions & 6 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, CustomRoleSettings
from models import CustomRole, CustomRoleSettings, BlockedUser
from views import RoleCreatorView, RoleCreatorResult, CustomRoleSetup

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -144,7 +144,19 @@ async def creation_callback(result: RoleCreatorResult):
view = RoleCreatorView(creation_callback, None)
await ctx.send(view=view, embed=embed)

async def _delete_custom_role(self, session, custom_role: CustomRole) -> None:
@commands.Cog.listener()
async def on_user_blocked(self, blocked_user: BlockedUser):
async with self.bot.Session() as session:
custom_role = await db.get_user_custom_role_in_guild(
session, blocked_user._user, blocked_user._guild
)

if custom_role is None:
return

await self.delete_custom_role(session, custom_role)

async def delete_custom_role(self, session, custom_role: CustomRole) -> None:
try:
logger.info(
"removing custom role from %s (%d) in %s (%d)",
Expand All @@ -153,10 +165,10 @@ async def _delete_custom_role(self, session, custom_role: CustomRole) -> None:
str(custom_role.guild),
custom_role._guild,
)
if custom_role.roles is not None:
if custom_role.role is not None:
await custom_role.role.delete(
reason=f"Member {custom_role.member} ({custom_role._user}) no longer has required role,"
f" removing custom role"
reason=f"Member {custom_role.member} ({custom_role._user}) no longer has required role"
f" or was blocked, removing custom role"
)
await db.delete_custom_role(session, custom_role._guild, custom_role._user)
except discord.Forbidden:
Expand Down Expand Up @@ -194,7 +206,7 @@ async def _role_removal_loop(self) -> None:
):
# member no longer has the guild's required role, add to deletion list
role_deletion_tasks.append(
self._delete_custom_role(session, custom_role)
self.delete_custom_role(session, custom_role)
)

await asyncio.gather(*role_deletion_tasks)
Expand Down
3 changes: 2 additions & 1 deletion cogs/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def guild_id_autocomplete(

class Main(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot: BotwBot = bot
self.oauth_url = self.bot.config["discord"]["oauth_url"]

@commands.command(brief="Sets the prefix for the current guild")
Expand Down Expand Up @@ -205,6 +205,7 @@ async def block(self, ctx: commands.Context, member: discord.Member):
await session.commit()

self.bot.blocked_users.setdefault(ctx.guild.id, set()).add(member.id)
self.bot.dispatch("user_blocked", blocked_user)

@commands.command(brief="Unblock a user from using the bot")
@commands.has_permissions(administrator=True)
Expand Down

0 comments on commit 1bde2b7

Please sign in to comment.