|
| 1 | +import discord |
| 2 | +from discord.ext import commands |
| 3 | +from discord import app_commands |
| 4 | +import logging |
| 5 | +import aiohttp |
| 6 | + |
| 7 | +from cogs.registration import DISCORD_BACKEND_URL |
| 8 | + |
| 9 | +logger = logging.getLogger('SCMarketBot.SubscribeCog') |
| 10 | + |
| 11 | + |
| 12 | +class AlertSubscriptions(commands.Cog): |
| 13 | + def __init__(self, bot): |
| 14 | + self.bot = bot |
| 15 | + |
| 16 | + subscribe_group = app_commands.Group(name="subscribe", description="Manage alert subscriptions") |
| 17 | + |
| 18 | + @subscribe_group.command(name="alerts", description="Subscribe this channel to order/offer alerts for an organization") |
| 19 | + @app_commands.describe(org_name="The spectrum ID of the organization") |
| 20 | + async def subscribe_alerts(self, interaction: discord.Interaction, org_name: str): |
| 21 | + await interaction.response.defer(ephemeral=True) |
| 22 | + try: |
| 23 | + async with aiohttp.ClientSession() as session: |
| 24 | + async with session.post( |
| 25 | + f"{DISCORD_BACKEND_URL}/alert-subscriptions", |
| 26 | + json={ |
| 27 | + "channel_id": str(interaction.channel_id), |
| 28 | + "guild_id": str(interaction.guild_id), |
| 29 | + "spectrum_id": org_name, |
| 30 | + "discord_id": str(interaction.user.id), |
| 31 | + }, |
| 32 | + ) as resp: |
| 33 | + data = await resp.json() |
| 34 | + if not resp.ok: |
| 35 | + await interaction.followup.send(f"❌ {data.get('error', 'Unknown error')}", ephemeral=True) |
| 36 | + return |
| 37 | + embed = discord.Embed( |
| 38 | + title="✅ Alert Subscription Active", |
| 39 | + description=f"This channel will now receive order and offer alerts for **{org_name}**.\n\nMembers with the **Claim Orders** permission can click the button on alerts to assign themselves.", |
| 40 | + color=0x10b881, |
| 41 | + ) |
| 42 | + await interaction.followup.send(embed=embed, ephemeral=True) |
| 43 | + except Exception as e: |
| 44 | + logger.error(f"Failed to subscribe alerts: {e}") |
| 45 | + await interaction.followup.send("❌ An error occurred.", ephemeral=True) |
| 46 | + |
| 47 | + @subscribe_group.command(name="alerts_user", description="Subscribe this channel to your personal order/offer alerts") |
| 48 | + async def subscribe_alerts_user(self, interaction: discord.Interaction): |
| 49 | + await interaction.response.defer(ephemeral=True) |
| 50 | + try: |
| 51 | + async with aiohttp.ClientSession() as session: |
| 52 | + async with session.post( |
| 53 | + f"{DISCORD_BACKEND_URL}/alert-subscriptions", |
| 54 | + json={ |
| 55 | + "channel_id": str(interaction.channel_id), |
| 56 | + "guild_id": str(interaction.guild_id), |
| 57 | + "discord_id": str(interaction.user.id), |
| 58 | + }, |
| 59 | + ) as resp: |
| 60 | + data = await resp.json() |
| 61 | + if not resp.ok: |
| 62 | + await interaction.followup.send(f"❌ {data.get('error', 'Unknown error')}", ephemeral=True) |
| 63 | + return |
| 64 | + embed = discord.Embed( |
| 65 | + title="✅ Alert Subscription Active", |
| 66 | + description="This channel will now receive your personal order and offer alerts.", |
| 67 | + color=0x10b881, |
| 68 | + ) |
| 69 | + await interaction.followup.send(embed=embed, ephemeral=True) |
| 70 | + except Exception as e: |
| 71 | + logger.error(f"Failed to subscribe user alerts: {e}") |
| 72 | + await interaction.followup.send("❌ An error occurred.", ephemeral=True) |
| 73 | + |
| 74 | + unsubscribe_group = app_commands.Group(name="unsubscribe", description="Remove subscriptions") |
| 75 | + |
| 76 | + @unsubscribe_group.command(name="alerts", description="Remove order/offer alert subscription from this channel") |
| 77 | + async def unsubscribe_alerts(self, interaction: discord.Interaction): |
| 78 | + await interaction.response.defer(ephemeral=True) |
| 79 | + try: |
| 80 | + async with aiohttp.ClientSession() as session: |
| 81 | + async with session.delete( |
| 82 | + f"{DISCORD_BACKEND_URL}/alert-subscriptions/{interaction.channel_id}", |
| 83 | + ) as resp: |
| 84 | + if resp.status == 404: |
| 85 | + await interaction.followup.send("❌ No alert subscription found for this channel.", ephemeral=True) |
| 86 | + return |
| 87 | + if not resp.ok: |
| 88 | + await interaction.followup.send("❌ Failed to remove subscription.", ephemeral=True) |
| 89 | + return |
| 90 | + await interaction.followup.send("✅ Alert subscription removed from this channel.", ephemeral=True) |
| 91 | + except Exception as e: |
| 92 | + logger.error(f"Failed to unsubscribe alerts: {e}") |
| 93 | + await interaction.followup.send("❌ An error occurred.", ephemeral=True) |
0 commit comments