-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
75 lines (58 loc) · 2.25 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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),
),
)
log_info(f"Successfully created new role {color.get_hex()}")
except (discord.Forbidden, discord.HTTPException, discord.InvalidArgument) as e:
log_error(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:
log_error(f"Failed to move role {color.get_hex()} with {e.text}")
return new_role
async def delete_role(role: discord.Role) -> bool:
try:
await role.delete()
return True
except:
log_error(f"Failed to delete role {role.name}")
return False
async def update_role(
role: discord.Role, name: str = None, color: discord.Colour = None
) -> bool:
try:
log_info(
f"Editing role {role.name} with name: {name or role.name} and color: {color or role.color}"
)
await role.edit(name=name or role.name, color=color or role.color)
return True
except:
log_error(
f"Failed to update role {role.name} with name: {name or role.name} and color: {color or role.color}"
)
return False
async def error_and_respond(ctx: discord.ApplicationContext, message: str):
log_error(message)
return await ctx.respond(f"{message}, please try again in a few seconds")
async def log_and_respond(ctx: discord.ApplicationContext, message: str):
log_info(message)
return await ctx.respond(message)
def log_error(message: str):
print(f"ERROR: {message}")
def log_info(message: str):
print(f"INFO: {message}")
def get_current_color(user: discord.Member | discord.User) -> discord.Role | None:
return next((role for role in user.roles if role.name.startswith("#")), None)