architecture - Discord Bot and a website communication, what should be the system design - Stack Overflow

I'm building a website that lets users add new items in their profiles. Users can also do the same

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?

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?

Share Improve this question asked Mar 9 at 17:21 parsecerparsecer 5,15020 gold badges86 silver badges167 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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

  • Authentication is different for the website (JWT/session) vs the Discord bot (maybe an authToken)
  • List of allowed APIs for the website vs the Discord bot will vary.

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/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=}")
  

Lets break it down step by step.

  1. Let's define some operation xyz that the user can perform on the website.

  2. You want a user to do xyz operation via a bot.

  3. You want a sort of acknowledgement that xyz operation has been completed

    1. [Help me understand]: How "bot" sends the notif that xyz is succesful? It should rather be the point where xyz operation has been performed, right?

    2. I will make an assumption that your website [referred to as server now onwards] will send the notif to channel.

  4. Now how exactly should we make it?


  1. You do not want your bot to do any sort of heavy lifting. For example, checking if there did exist an error, is not a responsibility of bot. Why?

    1. Because the bot is not a "client". Its just someone who communicates to your server "Hey, I received xyz message".

    2. All sort of parsing shall happen within the server.

    3. Another reason is, this makes the bot extensible, you do not need to change code of the bot to perform abc operation.

  2. In my belief [Design is subjective]. The addition of profiles should be async. So, kafka/rmq over webhook/websocket. Why?

    1. I am not sure, how retry-ability will work with webhook/socket. But I am sure its better with kafka and rmq, since they have dead-letter-queue. Retry-ability is important since, in production environments, connections are prone to break or operation is prone to fail etc. You do not want to loose a message with no logs.

    2. The nature of business is async. Once your bot communicates, "hey something happened". It has no business to know what happened with "hey something".

  3. Any further clarifications are welcome.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744864597a4597905.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信