File size: 12,073 Bytes
d96fe7a
 
 
 
c5b79f1
d96fe7a
 
 
abeba9e
d96fe7a
 
 
 
9d2df5b
d96fe7a
9d2df5b
abeba9e
d96fe7a
c5b79f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d96fe7a
 
 
 
 
9583bb8
d96fe7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662810f
d96fe7a
 
abeba9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662810f
ee7725b
 
 
 
 
 
 
662810f
d96fe7a
 
662810f
 
d96fe7a
abeba9e
d96fe7a
 
 
 
ee7725b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662810f
 
 
 
d96fe7a
662810f
 
 
 
 
ee7725b
662810f
ee7725b
 
 
 
662810f
ee7725b
 
 
 
 
 
 
 
 
5f64ab2
ee7725b
 
 
 
 
 
 
 
 
 
5f64ab2
ee7725b
 
 
 
 
 
 
 
 
2a08072
ee7725b
 
 
 
 
 
 
 
2a08072
ee7725b
 
 
 
662810f
 
 
 
 
 
 
 
 
 
 
 
ee7725b
 
662810f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7725b
662810f
 
 
 
ee7725b
662810f
d96fe7a
 
 
abeba9e
 
 
cd3eca9
 
abeba9e
cd3eca9
 
abeba9e
 
cd3eca9
abeba9e
 
 
 
cd3eca9
 
abeba9e
 
cd3eca9
abeba9e
 
 
 
 
cd3eca9
abeba9e
 
c5b79f1
 
 
 
 
 
 
 
 
 
 
 
 
abeba9e
 
 
 
 
 
 
d96fe7a
 
 
 
 
 
 
 
 
4536ca1
7960b49
d96fe7a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import calendar
import os
import threading
from datetime import datetime, timedelta
from enum import Enum

import discord
import gradio as gr
import stripe
from discord.ext import commands


# HF GUILD SETTINGS
MY_GUILD_ID = int(os.environ.get("GUILD_ID"))
MY_GUILD = discord.Object(id=MY_GUILD_ID)
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN")
stripe.api_key = os.environ.get("STRIPE_API_KEY")

# ORDER DETAILS
class ReachyMiniType(Enum):
    Wireless = "Reachy-Mini (with Onboard Compute and battery)"
    Lite = "Reachy Mini (Lite version)"


def detect_reachy_types(inv) -> set[ReachyMiniType]:
    types = set()
    for line in inv.lines.data:
        desc = line.description or ""
        for rtype in ReachyMiniType:
            if rtype.value in desc:
                types.add(rtype)
    return types


def format_order_items(inv) -> str:
    items = []
    for line in inv.lines.data:
        items.append(f"β€’ {line.description} (x{line.quantity})")
    return "\n".join(items)


def get_invoice_digits(invoice_number: str) -> int:
    parts = invoice_number.split("-")
    return int(parts[-1]) if parts[-1].isdigit() else 0


def early_shipping_notes(types: set[ReachyMiniType], invoice_number: str, arrival_date: datetime) -> str:
    now = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
    notes = []

    if ReachyMiniType.Lite in types:
        if now < datetime(2026, 5, 1) and arrival_date > datetime(2026, 4, 30):
            notes.append(
                "πŸ“¬ All Reachy Mini Lite orders will be shipped **late April 2026** "
                "β€” yours might arrive before the expected date!"
            )

    if ReachyMiniType.Wireless in types:
        inv_num = get_invoice_digits(invoice_number)
        if inv_num < 7000:
            if now < datetime(2026, 4, 1) and arrival_date > datetime(2026, 3, 31):
                notes.append(
                    "πŸ“¬ Reachy Mini Wireless orders with invoice number before 7000 will be shipped **late March 2026/early April 2026** "
                    "β€” yours might arrive before the expected date!"
                )
        else:
            if now < datetime(2026, 7, 1) and arrival_date > datetime(2026, 6, 15):
                notes.append(
                    "πŸ“¬ Reachy Mini Wireless orders with invoice number 7000 and above will be shipped **early June 2026** "
                    "β€” yours might arrive before the expected date!"
                )
            elif now < datetime(2026, 7, 1) and arrival_date <= datetime(2026, 6, 1):
                notes.append(
                    "⚠️ There is a chance your Reachy Mini Wireless shipment might be delayed until **early June 2026**. "
                    "The Wireless version has been trickier to produce due to longer supply times "
                    "for some components (especially the Raspberry Pi). We apologize for the inconvenience."
                )

    notes.append(
        "πŸ”— You can manage your order through our Stripe customer portal:\n"
        "https://billing.stripe.com/p/login/7sY5kFal10614vB4W873G00"
    )

    return "\n\n".join(notes)


class Bot(commands.Bot):
    """This structure allows slash commands to work instantly."""

    def __init__(self):
        super().__init__(command_prefix="/", intents=discord.Intents.default())

    async def setup_hook(self):
        await self.tree.sync(guild=discord.Object(MY_GUILD_ID))
        print(f"Synced slash commands for {self.user}.")


client = Bot()


@client.event
async def on_ready():
    print(f"Logged in as {client.user} (ID: {client.user.id})")
    print("------")


MIN_DATE = datetime(2025, 7, 9)


def format_eta_result(purchase_date: datetime) -> str:
    arrival_date = purchase_date + timedelta(days=90)
    today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
    days_until = (arrival_date - today).days

    if days_until < -14:
        status = "βœ… Your Reachy Mini must already have been delivered! Check with your carrier or contact sales@pollen-robotics.com if you haven't received it."
    elif days_until < 0:
        status = "⏳ The delivery seems to have been delayed a bit. It should arrive any day now β€” hang tight!"
    elif days_until <= 7:
        status = "πŸŽ‰ The delivery is right around the corner! Your Reachy Mini will be there very soon."
    else:
        status = f"🚚 Your Reachy Mini is on its way! About **{days_until} days** to go."

    return (
        f"πŸ—“οΈ Purchase date: **{purchase_date.strftime('%B %d, %Y')}**\n"
        f"πŸ“¦ Expected Reachy Mini arrival: **{arrival_date.strftime('%B %d, %Y')}** (90 days after purchase)\n\n"
        f"{status}"
    )


class DayButton(discord.ui.Button):
    def __init__(self, day: int, row: int, disabled: bool = False):
        super().__init__(
            label=str(day),
            style=discord.ButtonStyle.secondary,
            row=row,
            disabled=disabled,
        )
        self.day = day

    async def callback(self, interaction: discord.Interaction):
        view: DatePickerView = self.view
        purchase_date = datetime(view.year, view.month, self.day)
        await interaction.response.edit_message(
            content=format_eta_result(purchase_date),
            view=None,
        )


def valid_months(year: int) -> list[int]:
    today = datetime.now()
    months = []
    for m in range(1, 13):
        if (year, m) < (MIN_DATE.year, MIN_DATE.month):
            continue
        if (year, m) > (today.year, today.month):
            continue
        months.append(m)
    return months


def valid_years() -> list[int]:
    today = datetime.now()
    return [y for y in range(MIN_DATE.year, today.year + 1)]


def clamp_month(year: int, month: int) -> int:
    months = valid_months(year)
    if month in months:
        return month
    if month < months[0]:
        return months[0]
    return months[-1]


def is_day_disabled(year: int, month: int, day: int) -> bool:
    today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
    dt = datetime(year, month, day)
    return dt < MIN_DATE or dt > today


class DatePickerView(discord.ui.View):
    FIRST_PAGE = 14
    MID_PAGE = 13

    def __init__(self):
        super().__init__(timeout=180)
        today = datetime.now()
        self.year = today.year
        self.month = today.month
        self.page = 0
        self.rebuild()

    def rebuild(self):
        self.clear_items()
        self._build_selects()
        self._build_day_buttons()

    def _build_selects(self):
        today = datetime.now()

        year_options = [
            discord.SelectOption(
                label=str(y), value=str(y), default=(y == self.year)
            )
            for y in valid_years()
        ]
        year_select = discord.ui.Select(placeholder="Year", options=year_options, row=0)
        year_select.callback = self._on_year
        self.add_item(year_select)

        months = valid_months(self.year)
        month_options = [
            discord.SelectOption(
                label=calendar.month_name[m], value=str(m), default=(m == self.month)
            )
            for m in months
        ]
        month_select = discord.ui.Select(placeholder="Month", options=month_options, row=1)
        month_select.callback = self._on_month
        self.add_item(month_select)

    async def _on_year(self, interaction: discord.Interaction):
        self.year = int(interaction.data["values"][0])
        self.month = clamp_month(self.year, self.month)
        self.page = 0
        self.rebuild()
        await interaction.response.edit_message(
            content=f"πŸ€– Select your Reachy Mini purchase date:",
            view=self,
        )

    async def _on_month(self, interaction: discord.Interaction):
        self.month = int(interaction.data["values"][0])
        self.page = 0
        self.rebuild()
        await interaction.response.edit_message(
            content=f"πŸ€– Select your Reachy Mini purchase date:",
            view=self,
        )

    def _build_day_buttons(self):
        num_days = calendar.monthrange(self.year, self.month)[1]

        if self.page == 0:
            start, end = 1, min(self.FIRST_PAGE, num_days)
        else:
            start = self.FIRST_PAGE + 1 + (self.page - 1) * self.MID_PAGE
            end = min(start + self.MID_PAGE - 1, num_days)

        has_prev = self.page > 0
        has_next = end < num_days

        for i, d in enumerate(range(start, end + 1)):
            disabled = is_day_disabled(self.year, self.month, d)
            self.add_item(DayButton(d, row=2 + i // 5, disabled=disabled))

        day_count = end - start + 1
        last_row = 2 + (day_count - 1) // 5
        last_row_used = day_count % 5 or 5
        nav_count = int(has_prev) + int(has_next)
        if nav_count > 0:
            if last_row_used + nav_count <= 5:
                nav_row = last_row
            else:
                nav_row = min(last_row + 1, 4)
            if has_prev:
                btn = discord.ui.Button(label="β—‚", style=discord.ButtonStyle.primary, row=nav_row)
                btn.callback = self._prev_page
                self.add_item(btn)
            if has_next:
                btn = discord.ui.Button(label="β–Έ", style=discord.ButtonStyle.primary, row=nav_row)
                btn.callback = self._next_page
                self.add_item(btn)

    async def _next_page(self, interaction: discord.Interaction):
        self.page += 1
        self.rebuild()
        await interaction.response.edit_message(view=self)

    async def _prev_page(self, interaction: discord.Interaction):
        self.page -= 1
        self.rebuild()
        await interaction.response.edit_message(view=self)


@client.tree.command(name="eta", description="Find out when your Reachy Mini will arrive", guild=MY_GUILD)
async def eta(interaction: discord.Interaction, invoice: str = None):
    if invoice:
        await interaction.response.defer(ephemeral=True)
        if invoice.isdigit():
            invoice = f"REACHYMINI-{invoice}"
        try:
            results = stripe.Invoice.search(query=f'number:"{invoice}"')
        except stripe.error.AuthenticationError:
            view = DatePickerView()
            await interaction.followup.send(
                "❌ Unable to verify invoice. Please select your purchase date manually:",
                view=view,
                ephemeral=True,
            )
            return

        if not results.data:
            view = DatePickerView()
            await interaction.followup.send(
                "❌ Invoice not found β€” make sure you're entering the **invoice number** (not the order number). Please select your purchase date manually:",
                view=view,
                ephemeral=True,
            )
            return

        inv = results.data[0]
        purchase_date = datetime.fromtimestamp(inv.created)
        purchase_date = purchase_date.replace(hour=0, minute=0, second=0, microsecond=0)
        arrival_date = purchase_date + timedelta(days=90)

        items_text = format_order_items(inv)
        types = detect_reachy_types(inv)
        shipping_notes = early_shipping_notes(types, invoice, arrival_date)

        message = f"πŸ”Ž Invoice number **{invoice}** found!\n\n"
        message += f"πŸ›’ **Ordered items:**\n{items_text}\n\n"
        message += format_eta_result(purchase_date)
        if shipping_notes:
            message += f"\n\n{shipping_notes}"

        await interaction.followup.send(message, ephemeral=True)
    else:
        view = DatePickerView()
        await interaction.response.send_message(
            "πŸ€– Select your Reachy Mini purchase date:",
            view=view,
            ephemeral=True,
        )


def run_bot():
    client.run(DISCORD_TOKEN)


threading.Thread(target=run_bot).start()
"""This allows us to run the Discord bot in a Python thread"""
with gr.Blocks() as demo:
    gr.Image("reachy-mailman.png")
demo.queue(default_concurrency_limit=100, max_size=100)
demo.launch()