-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorCommands.py
93 lines (83 loc) · 3.17 KB
/
colorCommands.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import csv
import random
import discord
import botCommands
import namedColors
# Returns the index position of the Bots top role in the guild's list of roles
def getBotTopRoleNum(ctx):
botMember = ctx.guild.me
botRoles = botMember.roles
topBotRole = botRoles[len(botRoles) - 1]
return ctx.guild.roles.index(topBotRole)
async def assignColor(ctx, red, green, blue):
color = discord.Color.from_rgb(int(red), int(green), int(blue))
user = ctx.message.author
topRole = user.top_role
for role in user.roles:
if str(role).startswith("#"):
await user.remove_roles(role)
break
needsNewRole = True
for role in ctx.guild.roles:
if role.name == str(color):
print(f"{role.name} is {str(color)}")
await user.add_roles(role)
needsNewRole = False
break
if needsNewRole:
role = await ctx.guild.create_role(name = str(color), color = color)
await user.add_roles(role)
# The top color role will be the one directly under the bot's own top role
topColorRoleNum = getBotTopRoleNum(ctx) - 1
try:
await ctx.guild.edit_role_positions(positions = {role: topColorRoleNum})
except:
await ctx.send(f"Something went wrong moving {str(color)} to position {topColorRoleNum}")
return color
async def cleanupColors(ctx):
rolesDeleted = 0
for role in ctx.guild.roles:
if str(role).startswith("#"):
if len(role.members) == 0:
await role.delete(reason = "Unused")
rolesDeleted += 1
message = f"Deleted {rolesDeleted} roles."
await ctx.send(message)
# Given a color name, this function will search the colors given in the
# colorNames.csv file for that name. If found, it will return the red, green,
# blue values pertaining to that color; if not, the user is notified. CSV from
# meodai's repo on GitHub.
# (https://github.com/meodai/color-names/blob/master/dist/colornames.csv)
def colorByName(name):
# Replace any underscores in the name with spaces and make it lowercase
name = name.replace("_", " ")
name = name.lower()
colorHex = namedColors.findNamedColorHex(name)
red, green, blue = botCommands.hexToRGB(colorHex)
return red, green, blue
def countColorRoles(ctx):
roleCount = 0
for role in ctx.guild.roles:
if str(role).startswith("#"):
roleCount += 1
return roleCount
# Returns a message string containing the specified user's color. If the user
# does not have a color role, then return a message with such.
def getUserColor(ctx, user):
for role in user.roles:
if str(role).startswith("#"):
return f"**{user.nick}'s** color is {str(role)}"
return f"**{user.nick}** does not have a color role"
# Chooses three random numbers, each 0-255 and returns them
def randomColor():
random.seed()
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
return red, green, blue
async def removeColorRole(ctx):
user = ctx.message.author
for role in user.roles:
if str(role).startswith("#"):
await user.remove_roles(role)
break