discordarchitecturemessage-queuesystem-design

Discord Bot and a website communication, what should be the system design


I'm building a website that lets users add new items in their profiles. Users can also do the same using Discord bot's commands. In both cases the Discord bot sends a message New item added by @user in a certain channel.

I'm thinking of how to design the application. Should I have the POST addItem endpoint on the website's server which the bot would call?

onNewDiscordMessage():
  if (addItemCommand):
    sendPostRequest(websiteUrl, data)
  sendMessageInChannel("New item added!")

But then the website would also need to have the message be sent in the Discord channel. Should I make the Discord bot a web app too? Have the bot be imported as a dependency and be manageable using POST endpoint?

POST("send_discord_message")
void sendMessage(String message)

Or should I use RabbitMQ/Kafka queues for this? Or websockets/webhooks?


Solution

  • Addition to profile

    Server has an API POST /profile/items which adds an item to the profile.
    You can call the same API from the website or Discord bot, it'll work.

    Recommendation: Expose separate APIs for both clients (you can call the same function internally), because

    So keeping them separate would be cleaner.

    POST /profile/items for web, POST discord/profile/items for Discord.

    Notification on additions to profile

    I don't think you need RabbitMQ/Kafka or websockets for this. Unless you're doing it at scale, hitting >1k calls a minute. Sending a notification is just one HTTP webhook call.

    Implementation in order of scale:

    1. Low scale: Just make an async HTTP call after adding the item
    2. Medium scale: Add a common async queue in your server (in your programming language, internal. Like ThreadPool), and queue up notifications there and let it make the HTTP calls.
    3. High scale: Move the queue out of your server, which is Kafka etc.

    In summary, as you scale: start with async requests, add an internal queue, move the queue outside the server.


    Fwiw I already send Discord notifications for my product, it's only a few lines of Python code:

    def __send_discord_message(*, message: str):
      webhook_url = "https://discord.com/api/webhooks/xxx/xxx"
      payload = {"content": message}
      response = requests.post(webhook_url, json=payload)
      if response.status_code < 200 or response.status_code > 299:
        raise ValueError(f"Couldn't send Discord msg. {response.text=}, {response.status_code=}")