From 548fdfebaa3f186e8f71e1045b03f5562aeea406 Mon Sep 17 00:00:00 2001 From: JakeBreath Date: Sat, 22 Aug 2026 09:56:48 -0500 Subject: [PATCH] Made embedded colors work logically --- main.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index c2547e3..ee2acfe 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,23 @@ import config log = logging.getLogger('healthbot') +def _health_color(results): + """Embed color on a green -> yellow -> red gradient based on the share of + critical services that are down (0% down = green, 50% = yellow, 100% = red).""" + critical = [r for r in results if r.get('critical', True)] + if not critical: + return discord.Color.from_rgb(0, 255, 0) + down = sum(1 for r in critical if not r['up']) + ratio = down / len(critical) + if ratio <= 0.5: + t = ratio / 0.5 + red, green, blue = round(255 * t), 255, 0 + else: + t = (ratio - 0.5) / 0.5 + red, green, blue = 255, round(255 * (1 - t)), 0 + return discord.Color.from_rgb(red, green, blue) + + class HealthBot(discord.Client): def __init__(self): intents = discord.Intents.default() @@ -61,8 +78,7 @@ class HealthBot(discord.Client): return await checks.run_checks(session, config.CHECK_TIMEOUT) def build_embed(self, results): - all_up = all(r['up'] for r in results if r.get('critical', True)) - color = discord.Color.green() if all_up else discord.Color.red() + color = _health_color(results) embed = discord.Embed( title='Service Health', color=color,