Add accent color option

This commit is contained in:
Andrew 2023-09-28 19:07:03 -04:00
parent c410650ae2
commit 6a81a5b337
9 changed files with 33 additions and 7 deletions

View File

@ -86,6 +86,7 @@ class Webhooks(BaseModel):
bot_name = CharField(default="Crafty Controller")
trigger = CharField(default="server_start,server_stop")
body = CharField(default="")
color = CharField(default="#005cd1")
enabled = BooleanField(default=True)
class Meta:
@ -541,6 +542,7 @@ class HelpersWebhooks:
Webhooks.url: create_data["url"],
Webhooks.bot_name: create_data["bot_name"],
Webhooks.body: create_data["body"],
Webhooks.color: create_data["color"],
Webhooks.trigger: create_data["trigger"],
Webhooks.enabled: create_data["enabled"],
}
@ -568,6 +570,7 @@ class HelpersWebhooks:
"bot_name": webhook.bot_name,
"trigger": webhook.trigger,
"body": webhook.body,
"color": webhook.color,
"enabled": webhook.enabled,
}
else:

View File

@ -192,8 +192,7 @@ class ServerInstance:
title=webhook["name"],
url=webhook["url"],
message=webhook["body"],
# color=webhook["color"],
# TODO implement frontend color picker
color=webhook["color"],
)
return res

View File

@ -1071,6 +1071,7 @@ class PanelHandler(BaseHandler):
page_data["webhook"]["bot_name"] = "Crafty Controller"
page_data["webhook"]["trigger"] = []
page_data["webhook"]["body"] = ""
page_data["webhook"]["color"] = "#005cd1"
page_data["webhook"]["enabled"] = True
page_data["providers"] = WebhookFactory.get_supported_providers()

View File

@ -22,6 +22,7 @@ new_webhook_schema = {
"bot_name": {"type": "string"},
"trigger": {"type": "array"},
"body": {"type": "string"},
"color": {"type": "string", "default": "#005cd1"},
"enabled": {
"type": "boolean",
"default": True,

View File

@ -23,6 +23,7 @@ webhook_patch_schema = {
"bot_name": {"type": "string"},
"trigger": {"type": "array"},
"body": {"type": "string"},
"color": {"type": "string", "default": "#005cd1"},
"enabled": {
"type": "boolean",
"default": True,
@ -177,7 +178,7 @@ class ApiServersServerWebhooksManagementIndexHandler(BaseApiHandler):
title="Test Webhook",
url=webhook["url"],
message=webhook["body"],
color=4915409, # Prestigious purple!
color="#4b00d1", # Prestigious purple!
)
except Exception as e:
self.finish_json(500, {"status": "error", "error": str(e)})

View File

@ -24,6 +24,10 @@ class DiscordWebhook(WebhookProvider):
current_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
)
# Convert the hex to an integer
sanitized_hex = color[1:] if color.startswith("#") else color
color_int = int(sanitized_hex, 16)
headers = {"Content-type": "application/json"}
payload = {
"username": self.WEBHOOK_USERNAME,
@ -32,7 +36,7 @@ class DiscordWebhook(WebhookProvider):
{
"title": title,
"description": message,
"color": color,
"color": color_int,
"author": {"name": server_name},
"footer": {"text": f"Crafty Controller v.{self.CRAFTY_VERSION}"},
"timestamp": formatted_datetime,
@ -54,7 +58,7 @@ class DiscordWebhook(WebhookProvider):
title (str): The title for the notification message.
url (str): The webhook URL to send the notification to.
message (str): The main content or body of the notification message.
color (int, optional): The color code for the embed's side stripe.
color (str, optional): The color code for the embed's side stripe.
Defaults to a pretty blue if not provided.
Returns:
@ -64,7 +68,7 @@ class DiscordWebhook(WebhookProvider):
Raises:
Exception: If there's an error in dispatching the webhook.
"""
color = kwargs.get("color", 23761) # Default to a color if not provided.
color = kwargs.get("color", "#005cd1") # Default to a color if not provided.
payload, headers = self._construct_discord_payload(
server_name, title, message, color
)

View File

@ -83,6 +83,10 @@
{{ data["webhook"]["body"] }}
</textarea>
</div>
<div class="form-group">
<label for="bot_name">{{ translate('webhooks', 'color' , data['lang']) }}</label>
<input type="color" class="form-control" name="color" id="color" value='{{data["webhook"]["color"]}}'>
</div>
<div class="form-check-flat">
<label for="enabled" class="form-check-label ml-4 mb-4">
<input type="checkbox" class="form-check-input" id="enabled" name="enabled" checked=""
@ -209,6 +213,9 @@
//We need to make sure these are sent regardless of whether or not they're checked
formDataObject.enabled = $("#enabled").prop('checked');
formDataObject.trigger = select_val;
if(formDataObject.webhook_type != "Discord"){
delete formDataObject.color
}
console.log(formDataObject);
// Format the plain form data as JSON
@ -236,6 +243,14 @@
});
window.onload(startup())
function hexToDiscordInt(hexColor) {
// Remove the hash at the start if it's there
const sanitizedHex = hexColor.startsWith('#') ? hexColor.slice(1) : hexColor;
// Convert the hex to an integer
return parseInt(sanitizedHex, 16);
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.10/js/bootstrap-select.min.js"></script>

View File

@ -13,6 +13,7 @@ def migrate(migrator, database, **kwargs):
bot_name=peewee.CharField(default="Crafty Controller"),
trigger=peewee.CharField(default="server_start,server_stop"),
body=peewee.CharField(default=""),
color=peewee.CharField(default=""),
enabled=peewee.BooleanField(default=True),
)
"""

View File

@ -637,8 +637,9 @@
"url": "Webhook URL",
"bot_name": "Bot Name",
"webhook_body": "Webhook Body",
"color": "Select Color Accent",
"areYouSureDel": "Are you sure you want to delete this webhook?",
"areYouSureRun": "Are you sure you want to test this websocket?",
"areYouSureRun": "Are you sure you want to test this webhook?",
"edit": "Edit",
"run": "Test Run Webhook",
"new": "New Webhook",