Source code for events.temperature_event

import re

import discord
from bot.weakauras_bot import WeakAurasBot
from utils.logging import get_logger

logger = get_logger(__name__)


[docs] def setup_temperature_event(bot: WeakAurasBot): """Setup temperature conversion event handler""" @bot.event async def on_message(message: discord.Message): # Don't respond to bot messages if message.author.bot: return # Only process messages in guilds if not message.guild: return # Check server-specific configuration if not await bot.is_event_enabled(message.guild.id, "temperature"): logger.debug( f"Temperature event disabled for guild {message.guild.name} ({message.guild.id})" ) return # Regex patterns to match temperature formats # Matches: 75F, 75°F, 75 F, 75 degrees F, 23C, 23°C, 23 C, 23 degrees C temp_pattern = r"(-?\d+(?:\.\d+)?)\s*(?:degrees?\s*)?([°]?)([FfCc])\b" matches = re.findall(temp_pattern, message.content) if not matches: return # Get channel name safely (DM channels don't have names) channel_name = getattr(message.channel, "name", "DM") logger.info( f"Temperature conversion triggered by {message.author.name} ({message.author.id}) in guild {message.guild.name} ({message.guild.id}) channel #{channel_name} ({message.channel.id}) with matches: {matches}" ) conversions = [] for temp_str, _, temp_unit in matches: try: temp = float(temp_str) unit = temp_unit.upper() if unit == "F": # Fahrenheit to Celsius celsius = (temp - 32) * 5 / 9 conversions.append(f"{temp}°F = {celsius:.1f}°C") logger.debug(f"Converted {temp}°F to {celsius:.1f}°C") elif unit == "C": # Celsius to Fahrenheit fahrenheit = (temp * 9 / 5) + 32 conversions.append(f"{temp}°C = {fahrenheit:.1f}°F") logger.debug(f"Converted {temp}°C to {fahrenheit:.1f}°F") except ValueError: logger.warning(f"Failed to parse temperature value: {temp_str}") continue if not conversions: return # Send conversion as a simple text reply conversion_text = " | ".join(conversions) logger.info(f"Sending temperature conversion reply: {conversion_text}") await message.reply(f"🌡️ {conversion_text}")