Skip to content

Commit

Permalink
Replace deleted roles counter with list of deleted roles
Browse files Browse the repository at this point in the history
  • Loading branch information
rwbxd committed Oct 13, 2024
1 parent 821ea10 commit 87c9ae5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 11 additions & 9 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import discord
from util import errorAndRespond, logAndRespond, logError
from util import delete_role, errorAndRespond, logAndRespond


async def clean_empty_color_roles(ctx: discord.ApplicationContext):
try:
assert (
type(ctx.guild) is discord.Guild
), "Encountered an issue accessing the Discord guild"

empty_color_roles = [
role
for role in ctx.guild.roles
if role.name.startswith("#") and len(role.members) == 0
]
counter = 0
for role in empty_color_roles:
try:
await role.delete()
counter += 1
except:
logError(f"Failed to delete role {role.name}")
return await logAndRespond(ctx, f"Cleaned up {counter} empty color roles!")
deleted_roles: list[str] = [
role.name for role in empty_color_roles if delete_role(role)
]

return await logAndRespond(
ctx,
f"Cleaned up {len(deleted_roles)} empty color roles:\n"
+ "\n".join(deleted_roles),
)
except AssertionError as errorMessage:
return await errorAndRespond(ctx, errorMessage)
9 changes: 9 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ async def create_new_role(
return new_role


async def delete_role(role: discord.Role) -> bool:
try:
await role.delete()
return True
except:
logError(f"Failed to delete role {role.name}")
return False


async def errorAndRespond(ctx: discord.ApplicationContext, message: str):
logError(message)
return await ctx.respond(f"{message}, please try again in a few seconds")
Expand Down

0 comments on commit 87c9ae5

Please sign in to comment.