Skip to content

Commit 66f6d5a

Browse files
committed
feat: add /subscribe and /unsubscribe commands for order alerts
Single AlertSubscriptions cog with: - /subscribe alerts <org_name> - subscribe channel to org alerts - /subscribe alerts_user - subscribe channel to personal alerts - /unsubscribe alerts - remove subscription from channel
1 parent e6f34ab commit 66f6d5a

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

cogs/subscribe.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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)

main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from cogs.order import order
1818
from cogs.registration import Registration
1919
from cogs.stock import stock
20+
from cogs.subscribe import AlertSubscriptions
2021

2122
from util.config import Config
2223
from util.result import Result
@@ -41,6 +42,7 @@ async def setup_hook(self):
4142
await self.add_cog(Lookup(self))
4243
await self.add_cog(order(self))
4344
await self.add_cog(stock(self))
45+
await self.add_cog(AlertSubscriptions(self))
4446

4547
await self.tree.sync()
4648

0 commit comments

Comments
 (0)