How to sync item additions between a website and Discord bot?

I’m working on a project where users can add items to their profiles through a website or a Discord bot. I want the bot to announce new additions in a specific channel. I’m not sure about the best way to set this up.

Should the bot call an API on the website when it gets a command? Or should the website somehow tell the bot to send messages?

Here’s a basic idea for the bot:

def handle_message(message):
    if message.content.startswith('!additem'):
        # Call website API
        response = api.add_item(message.author, item_details)
        if response.ok:
            channel.send(f'{message.author} added a new item!')

But how should the website part work? Should the bot be a separate app that the website can call? Or should I use something like message queues or webhooks?

I’m a bit lost on the best approach here. Any advice on a good system design for this would be super helpful!

I’ve worked on a similar integration where both the website and the Discord bot produced events that were sent into a message queue. In my experience, using a system like RabbitMQ or Apache Kafka helped to decouple the components so that the website and the bot did not have to directly depend on each other. Instead of writing lists, I prefer to explain that the website can push events upon item additions and the bot does the same. A dedicated consumer service then listens to the queue, handles any necessary database updates, and sends the notification to Discord. I also made sure to implement idempotency by tagging each event with a unique identifier to avoid processing duplicates. This approach allowed for easier scaling and maintenance. I hope this insight proves helpful.

From my experience, a webhook-based approach works well for this kind of integration. You could set up a webhook endpoint on your Discord bot server that listens for item addition events. When a user adds an item through the website, it can make a POST request to this webhook with the item details. The bot server then processes this and sends the announcement to the appropriate Discord channel.

For the Discord bot side, you can keep your current setup where it calls the website API when a user adds an item via command. This way, you maintain a single source of truth (your website database) while allowing both interfaces to trigger notifications.

I’ve found this method to be reliable and easier to maintain than setting up message queues, especially for smaller scale projects. It also allows for easy expansion if you decide to add more notification channels in the future.

hey, have u tried using websockets? they’re pretty cool for real-time stuff. ur bot could connect to a websocket server, and when someone adds an item on the website, it sends a message thru the socket. then ur bot can instantly announce it. its like, super fast and works both ways. just an idea tho