|
| 1 | +import os |
| 2 | + |
| 3 | +from discord import Embed, Interaction, app_commands |
| 4 | +from discord.ext import commands |
| 5 | +from discord.utils import get |
| 6 | + |
| 7 | +from bridger.gateway import GatewayError, GatewayManagerEMQX, emqx |
| 8 | +from bridger.log import logger |
| 9 | + |
| 10 | +BRIDGER_ADMIN_ROLE = os.getenv("BRIDGER_ADMIN_ROLE", "Bridger Admin") |
| 11 | + |
| 12 | + |
| 13 | +def check_gateway_owner(interaction: Interaction) -> bool: |
| 14 | + node_id = None |
| 15 | + gateway_manager = GatewayManagerEMQX(emqx) |
| 16 | + |
| 17 | + logger.debug(f"Interaction data: {interaction.data}") |
| 18 | + |
| 19 | + if "options" in interaction.data: |
| 20 | + # Look through nested options |
| 21 | + for option in interaction.data["options"]: |
| 22 | + if "options" in option: |
| 23 | + for sub_option in option["options"]: |
| 24 | + if sub_option["name"] == "node_id": |
| 25 | + node_id = sub_option["value"] |
| 26 | + break |
| 27 | + |
| 28 | + if not node_id: |
| 29 | + raise ValueError("node_id not found in the command options") |
| 30 | + |
| 31 | + logger.debug(f"Node ID: {node_id}") |
| 32 | + |
| 33 | + try: |
| 34 | + gateway = gateway_manager.get_gateway(node_id) |
| 35 | + owner = interaction.client.get_user(gateway.owner_id) |
| 36 | + except ValueError as e: |
| 37 | + raise app_commands.AppCommandError(f"Error retrieving gateway: {e}") |
| 38 | + |
| 39 | + logger.debug(f"Gateway owner: {owner}") |
| 40 | + logger.debug(f"Interaction user: {interaction.user}") |
| 41 | + |
| 42 | + compared = owner == interaction.user |
| 43 | + |
| 44 | + logger.debug(f"Owner and interaction user compared: {compared}") |
| 45 | + |
| 46 | + return owner == interaction.user |
| 47 | + |
| 48 | + |
| 49 | +def is_bridger_admin_or_owner(interaction: Interaction): |
| 50 | + bridger_admin_role = get(interaction.guild.roles, name=BRIDGER_ADMIN_ROLE) |
| 51 | + if bridger_admin_role in interaction.user.roles or check_gateway_owner(interaction): |
| 52 | + return True |
| 53 | + return False |
| 54 | + |
| 55 | + |
| 56 | +class MQTTCog(commands.GroupCog, name="bridger-mqtt"): |
| 57 | + delete_after = None |
| 58 | + |
| 59 | + def __init__(self, bot: commands.Bot, gateway_manager: GatewayManagerEMQX): |
| 60 | + self.bot = bot |
| 61 | + self.gateway_manager = gateway_manager |
| 62 | + |
| 63 | + async def cog_app_command_error(self, interaction: Interaction, error: app_commands.AppCommandError): |
| 64 | + # Log the type of error and error message |
| 65 | + logger.debug(f"App command error: {type(error)}: {error}") |
| 66 | + |
| 67 | + if isinstance(error, app_commands.errors.CommandInvokeError): |
| 68 | + if isinstance(error.original, GatewayError): |
| 69 | + await interaction.response.send_message( |
| 70 | + f"Gateway already exists: {error.original.gateway.node_hex_id}", |
| 71 | + ephemeral=True, |
| 72 | + delete_after=self.delete_after, |
| 73 | + ) |
| 74 | + else: |
| 75 | + await interaction.response.send_message( |
| 76 | + f"Command invoke error: {error.original}", ephemeral=True, delete_after=self.delete_after |
| 77 | + ) |
| 78 | + elif isinstance(error, (app_commands.errors.MissingRole, app_commands.errors.CheckFailure)): |
| 79 | + await interaction.response.send_message( |
| 80 | + f"Check failure: {error}", ephemeral=True, delete_after=self.delete_after |
| 81 | + ) |
| 82 | + elif isinstance(error, ValueError): |
| 83 | + await interaction.response.send_message(f"Value error: {error}", ephemeral=True, delete_after=self.delete_after) |
| 84 | + else: |
| 85 | + await interaction.response.send_message( |
| 86 | + f"Unknown error: {error}", ephemeral=True, delete_after=self.delete_after |
| 87 | + ) |
| 88 | + |
| 89 | + @app_commands.command(name="request-account", description="Request a new MQTT account") |
| 90 | + @app_commands.describe( |
| 91 | + node_id="The hex node ID to request an account for. With or without the preceding ! such as !cbaf0421 or cbaf0421" |
| 92 | + ) |
| 93 | + async def request_account(self, ctx: Interaction, node_id: str): |
| 94 | + gateway, password = self.gateway_manager.create_gateway_user(node_id, ctx.user) |
| 95 | + message = f"Gateway created: {gateway.node_hex_id} with password: {password}" |
| 96 | + |
| 97 | + await ctx.response.send_message(message, ephemeral=True) |
| 98 | + |
| 99 | + @app_commands.checks.has_role(BRIDGER_ADMIN_ROLE) |
| 100 | + @app_commands.command(name="delete-account", description="Delete MQTT account") |
| 101 | + async def delete_account(self, ctx: Interaction, node_id: str): |
| 102 | + if self.gateway_manager.delete_gateway_user(node_id, ctx.user): |
| 103 | + await ctx.response.send_message(f"Gateway deleted: {node_id}", ephemeral=True, delete_after=self.delete_after) |
| 104 | + else: |
| 105 | + await ctx.response.send_message(f"Gateway not found: {node_id}", ephemeral=True, delete_after=self.delete_after) |
| 106 | + |
| 107 | + @app_commands.checks.has_role(BRIDGER_ADMIN_ROLE) |
| 108 | + @app_commands.command(name="list-accounts", description="List all MQTT accounts") |
| 109 | + async def list_accounts(self, ctx: Interaction): |
| 110 | + gateways = self.gateway_manager.list_gateways() |
| 111 | + |
| 112 | + if not gateways: |
| 113 | + await ctx.response.send_message(content="There are no provisioned gateways in the system.", ephemeral=True) |
| 114 | + return |
| 115 | + |
| 116 | + embed = Embed(description="Currently provisioned gateways:", color=0x6CEB94) |
| 117 | + |
| 118 | + for gateway in gateways: |
| 119 | + owner = self.bot.get_user(gateway.owner_id) |
| 120 | + |
| 121 | + embed.add_field( |
| 122 | + name="Gateway", |
| 123 | + value=f"ID: **{gateway.node_hex_id}**\nOwner: **{owner.name}**", |
| 124 | + inline=True, |
| 125 | + ) |
| 126 | + |
| 127 | + await ctx.response.send_message( |
| 128 | + content=f"There are {len(gateways)} provisioned gateways in the system.", |
| 129 | + embed=embed, |
| 130 | + ephemeral=True, |
| 131 | + ) |
| 132 | + |
| 133 | + @app_commands.check(check_gateway_owner) |
| 134 | + @app_commands.command(name="reset-password", description="Reset MQTT account password") |
| 135 | + async def reset_password(self, ctx: Interaction, node_id: str): |
| 136 | + gateway, password = self.gateway_manager.reset_gateway_password(node_id, ctx.user) |
| 137 | + |
| 138 | + await ctx.response.send_message( |
| 139 | + f"Gateway password reset: {gateway.node_hex_id} with new password: {password}", |
| 140 | + ephemeral=True, |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +async def setup(bot): |
| 145 | + gateway_manager = GatewayManagerEMQX(emqx) |
| 146 | + await bot.add_cog(MQTTCog(bot, gateway_manager)) |
0 commit comments