|
1 | 1 | # This project is licensed under the terms of the GPL v3.0 license. Copyright 2024 Cyteon
|
2 | 2 |
|
| 3 | +import io |
| 4 | + |
3 | 5 | from asteval import Interpreter
|
4 | 6 | aeval = Interpreter()
|
5 | 7 |
|
|
11 | 13 |
|
12 | 14 | from utils import Checks
|
13 | 15 |
|
| 16 | +from PIL import ImageColor, Image |
| 17 | + |
14 | 18 | class Utility(commands.Cog, name="⚡ Utility"):
|
15 | 19 | def __init__(self, bot) -> None:
|
16 | 20 | self.bot = bot
|
@@ -143,5 +147,48 @@ async def translate(self, context: Context, language, *, text: str) -> None:
|
143 | 147 | embed.set_footer(text=f"Translated to {language}")
|
144 | 148 | await context.send(embed=embed)
|
145 | 149 |
|
| 150 | + @commands.hybrid_command( |
| 151 | + name="color", |
| 152 | + description="Get information about a color.", |
| 153 | + usage="color <color>" |
| 154 | + ) |
| 155 | + @commands.check(Checks.is_not_blacklisted) |
| 156 | + @commands.check(Checks.command_not_disabled) |
| 157 | + @app_commands.allowed_installs(guilds=True, users=True) |
| 158 | + @app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True) |
| 159 | + async def color(self, context: Context, color: str) -> None: |
| 160 | + try: |
| 161 | + rgb = ImageColor.getrgb(color) |
| 162 | + rgba = ImageColor.getcolor(color, "RGBA") |
| 163 | + hex = hex_value = "#{:02x}{:02x}{:02x}".format(*rgb) |
| 164 | + |
| 165 | + |
| 166 | + embed = discord.Embed( |
| 167 | + title="Color Information", |
| 168 | + description="\n".join( |
| 169 | + [ |
| 170 | + f"Color: **{color}**", |
| 171 | + f"Hex: **{hex}**", |
| 172 | + f"RGB: **{rgb}**", |
| 173 | + f"RGBA: **{rgba}**", |
| 174 | + ] |
| 175 | + ), |
| 176 | + color=0xBEBEFE, |
| 177 | + ) |
| 178 | + |
| 179 | + img = Image.new("RGB", (100, 100), rgb) |
| 180 | + |
| 181 | + with io.BytesIO() as image_binary: |
| 182 | + img.save(image_binary, "PNG") |
| 183 | + image_binary.seek(0) |
| 184 | + |
| 185 | + file = discord.File(fp=image_binary, filename="color.png") |
| 186 | + embed.set_image(url="attachment://color.png") |
| 187 | + |
| 188 | + await context.send(embed=embed, file=file) |
| 189 | + except ValueError: |
| 190 | + await context.send("Invalid color") |
| 191 | + return |
| 192 | + |
146 | 193 | async def setup(bot) -> None:
|
147 | 194 | await bot.add_cog(Utility(bot))
|
0 commit comments