diff --git a/nlabosesamebot/commands/sesame.py b/nlabosesamebot/commands/sesame.py index fbf5701..f357c52 100644 --- a/nlabosesamebot/commands/sesame.py +++ b/nlabosesamebot/commands/sesame.py @@ -6,6 +6,7 @@ from pysesameos2.chsesame2 import CHSesame2 from pysesameos2.chsesamebot import CHSesameBot from pysesameos2.helper import CHSesame2MechStatus, CHSesameBotMechStatus +from pysesameos2.const import CHSesame2Status import discord from discord import Interaction from discord.ext import commands @@ -16,21 +17,55 @@ latest_interaction: Interaction = None debug_mode: bool = True +def on_sesame_statechanged(device: Union[CHSesame2, CHSesameBot]) -> None: + device_status = device.getDeviceStatus() + doorlock_status["is_locked"] = (device_status == CHSesame2Status.Locked) + + status_text = f"Device status: {device_status}\n" + if debug_mode: + mech_status = device.getMechStatus() + if mech_status is not None: + status_text += f"Battery: {mech_status.getBatteryPercentage()}%\n" + status_text += f"Battery Voltage: {mech_status.getBatteryVoltage():.2f}V\n" + status_text += f"isInLockRange: {mech_status.isInLockRange()}\n" + status_text += f"isInUnlockRange: {mech_status.isInUnlockRange()}\n" + + notification_channel_id = int(os.getenv('DISCORD_CHANNEL')) + channel = client.get_channel(notification_channel_id) + + if channel: + embed = discord.Embed( + title="Device Status Update", + description=status_text, + color=discord.Color.blue() + ) + event_loop = asyncio.get_event_loop() + asyncio.ensure_future(channel.send(embed=embed, silent=True), loop=event_loop) + asyncio.ensure_future(update_lock_status_message(), loop=event_loop) + async def send_embed_notification(interaction: Interaction, action: str, color: discord.Color): notification_channel_id = int(os.getenv('DISCORD_CHANNEL')) channel = client.get_channel(notification_channel_id) if channel: - action_text = "unlocked" if action == "🔓 Unlocked" else "locked" - emoji = "🔓" if action == "🔓 Unlocked" else "🔒" + if "Unlocked" in action: + author_text = "Unlocked" + action_text = "unlocked" + emoji = "🔓" + else: + author_text = "Locked" + action_text = "locked" + emoji = "🔒" embed = discord.Embed( description=f"{emoji} **{interaction.user.display_name} has {action_text} the door**", color=color ) - embed.set_author(name=f"{interaction.user.display_name} used {action_text.capitalize()}", icon_url=interaction.user.avatar.url) - - await channel.send(embed=embed) + embed.set_author( + name=author_text, + icon_url=interaction.user.display_avatar.url + ) + await channel.send(embed=embed, silent=True) async def send_status_embed(interaction: Interaction): device = handler.device @@ -62,28 +97,30 @@ async def send_status_embed(interaction: Interaction): description=status_text, color=discord.Color.blue() ) - await channel.send(embed=embed) + await channel.send(embed=embed, silent=True) async def update_lock_status_message(): global info_message button_channel_id = int(os.getenv('DISCORD_BUTTON_CHANNEL')) button_channel = client.get_channel(button_channel_id) + if button_channel: - status = "🔒 **Locked**" if doorlock_status["is_locked"] else "🔓 **Unlocked**" - content = f"**Doorlock status:** {status}" + lock_status = "🔒 **Locked**" if doorlock_status["is_locked"] else "🔓 **Unlocked**" + debug_status = "ON" if debug_mode else "OFF" + content = f"**Doorlock status:** {lock_status}\n**Debug mode:** {debug_status}" if info_message: try: await info_message.edit(content=content) except discord.errors.NotFound: - info_message = await button_channel.send(content) + info_message = await button_channel.send(content, silent=True) else: - info_message = await button_channel.send(content) + info_message = await button_channel.send(content, silent=True) -async def send_message_to_channel(message: str, channel_id: int, silent: bool = False): +async def send_message_to_channel(message: str, channel_id: int): channel = client.get_channel(channel_id) if channel: - await channel.send(content=message, silent=silent) + await channel.send(content=message, silent=True) class SesameControlView(View): def __init__(self): @@ -101,37 +138,31 @@ async def unlock_button(self, interaction: discord.Interaction, button: discord. await interaction.response.defer() global latest_interaction latest_interaction = interaction - doorlock_status["is_locked"] = False try: await handler.unlock() await send_embed_notification(interaction, "🔓 Unlocked", discord.Color.green()) - await send_status_embed(interaction) await update_lock_status_message() except Exception as e: notification_channel_id = int(os.getenv('DISCORD_CHANNEL')) await send_message_to_channel( f'## **Error** \n{type(e)}\n{e}\n### **Stack Trace**\n{traceback.format_exc()}', - notification_channel_id, - silent=True + notification_channel_id ) - + @discord.ui.button(label="Lock", style=discord.ButtonStyle.red, row=0) async def lock_button(self, interaction: discord.Interaction, button: discord.ui.Button): await interaction.response.defer() global latest_interaction latest_interaction = interaction - doorlock_status["is_locked"] = True try: await handler.lock() await send_embed_notification(interaction, "🔒 Locked", discord.Color.red()) - await send_status_embed(interaction) - await update_lock_status_message() + await update_lock_status_message() except Exception as e: notification_channel_id = int(os.getenv('DISCORD_CHANNEL')) await send_message_to_channel( f'## **Error** \n{type(e)}\n{e}\n### **Stack Trace**\n{traceback.format_exc()}', - notification_channel_id, - silent=True + notification_channel_id ) @discord.ui.button(label="Init", style=discord.ButtonStyle.gray, row=1) @@ -141,14 +172,13 @@ async def init_button(self, interaction: discord.Interaction, button: discord.ui latest_interaction = interaction try: await handler.connect() - await interaction.followup.send("🔄 Device has been initialized.", ephemeral=True) + await interaction.followup.send("🔄 Device has been initialized.", ephemeral=True, silent=True) await send_status_embed(interaction) except Exception as e: notification_channel_id = int(os.getenv('DISCORD_CHANNEL')) await send_message_to_channel( f'## **Error** \n{type(e)}\n{e}\n### **Stack Trace**\n{traceback.format_exc()}', - notification_channel_id, - silent=True + notification_channel_id ) @discord.ui.button(label="Toggle Debug", style=discord.ButtonStyle.gray, row=1) @@ -157,10 +187,7 @@ async def toggle_debug_button(self, interaction: discord.Interaction, button: di debug_mode = not debug_mode status = "ON" if debug_mode else "OFF" await interaction.response.send_message(f"Debug mode: {status}", ephemeral=True) - button_channel_id = int(os.getenv('DISCORD_BUTTON_CHANNEL')) - button_channel = client.get_channel(button_channel_id) - if button_channel: - await button_channel.send(f"Debug mode is now {status}.") + await update_lock_status_message() @client.event async def on_ready(): @@ -172,5 +199,5 @@ async def on_ready(): button_channel = client.get_channel(button_channel_id) if button_channel: view = SesameControlView() - info_message = await button_channel.send("Use the buttons below to control the door.", view=view) + info_message = await button_channel.send(view=view, silent=True) await update_lock_status_message()