-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- chore: Split functions into separate files - chore: replace JS-like filters and maps with list comprehensions - chore: make better logging and error handling - fix: Add members intent to avoid deleting in-use roles
- Loading branch information
Showing
6 changed files
with
135 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.venv | ||
.venv | ||
.github | ||
.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import discord | ||
import re | ||
import colour | ||
from util import create_new_role, errorAndRespond, logInfo | ||
|
||
|
||
async def color_user( | ||
ctx: discord.ApplicationContext, user: discord.Member | discord.User, color: str | ||
): | ||
try: | ||
assert ( | ||
type(ctx.guild) is discord.Guild | ||
), "Encountered an issue accessing the Discord guild" | ||
|
||
color = color.strip().replace(" ", "") | ||
if re.search("^[a-f0-9]{3}$|^[a-f0-9]{6}$", color, re.IGNORECASE): | ||
color = "#" + color | ||
|
||
try: | ||
role_color = colour.Color(color) | ||
except ValueError: | ||
return await ctx.respond( | ||
f"Didn't recognize color: {color} - try something else, or a specific hex code!" | ||
) | ||
|
||
logInfo( | ||
f"Coloring {user.name} the color {role_color.web} ({role_color.get_hex()})" | ||
) | ||
|
||
all_color_roles: list[discord.Role] = [ | ||
role for role in ctx.guild.roles if role.name.startswith("#") | ||
] | ||
desired_color_role = next( | ||
(role for role in all_color_roles if role.name == role_color.get_hex()), | ||
None, | ||
) or await create_new_role(ctx.guild, role_color) | ||
if desired_color_role is None: | ||
return await errorAndRespond( | ||
ctx, f"Failed to create new role for color: {color}" | ||
) | ||
|
||
await user.remove_roles(*all_color_roles) | ||
await user.add_roles(desired_color_role) | ||
return await ctx.respond(f"Colored you {role_color.web}!") | ||
|
||
except AssertionError as errorMessage: | ||
return await errorAndRespond(ctx, errorMessage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import discord | ||
from util import errorAndRespond, logAndRespond, logError | ||
|
||
|
||
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!") | ||
except AssertionError as errorMessage: | ||
return await errorAndRespond(ctx, errorMessage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import discord | ||
import colour | ||
|
||
|
||
async def create_new_role( | ||
guild: discord.Guild, color: colour.Color | ||
) -> discord.Role | None: | ||
try: | ||
new_role = await guild.create_role( | ||
name=color.get_hex(), | ||
color=discord.Color.from_rgb( | ||
int(color.get_red() * 255), | ||
int(color.get_green() * 255), | ||
int(color.get_blue() * 255), | ||
), | ||
) | ||
logInfo(f"Successfully created new role {color.get_hex()}") | ||
except (discord.Forbidden, discord.HTTPException, discord.InvalidArgument) as e: | ||
logError(f"Failed to create role {color.get_hex()} with {e.text}") | ||
return None | ||
|
||
try: | ||
return await new_role.edit( | ||
position=max([role.position for role in guild.me.roles]) - 2 | ||
) | ||
except (discord.Forbidden, discord.HTTPException, discord.InvalidArgument) as e: | ||
logError(f"Failed to move role {color.get_hex()} with {e.text}") | ||
return new_role | ||
|
||
|
||
async def errorAndRespond(ctx: discord.ApplicationContext, message: str): | ||
logError(message) | ||
return await ctx.respond(f"{message}, please try again in a few seconds") | ||
|
||
|
||
async def logAndRespond(ctx: discord.ApplicationContext, message: str): | ||
logInfo(message) | ||
return await ctx.respond(message) | ||
|
||
|
||
def logError(message: str): | ||
print(f"ERROR: {message}") | ||
|
||
|
||
def logInfo(message: str): | ||
print(f"INFO: {message}") |