Adding persistence storage to our Discord Bot

In my previous article i created a simple discord bot with Api gateway and Lambda function, however we might want our Discord bot to have a bit more functionalities like the ability to store data persistently which is why in this article we are going to explore adding DynamoDB to our stack which will store our data for our Rock Paper Scissor game.


What is DynamoDB:

DynamoDB is a serverless, NoSQL, fully managed database service which allows us to store data without having to provision a separate database server. But of course, you can always use traditional SQL databases like MySQL, PostgreSQL etc, and some of them do have serverless solutions in aws. But for simplicity sake we are just going to use DynamoDB.

Demonstration:

There are several things unpack here:

  • First when the user types “/rps <username>” into the chat, it sends a request to our api gateway then redirects it to our lambda function.
  • Our lambda function reads the request and update the DynamoDB table with both user’s Discord ID ,it also generates a unique_id to differentiate every rock paper scissor game from each other alt_text
  • Then it sends an ephemeral message with buttons asking the user to choose a move. alt_text (yes i did misspelt scissor and I am regretting it now)
  • The user can then choose a move, let's say “Siccor” for example.
  • It will send another request to our lambda and it will update the table to the user_1_move column like this: alt_text
  • It then sends a normal message with buttons (It's not possible to use ephemeral messages here for security reason) asking the opponent to choose a move. alt_text
  • The opponent can then choose a move by clicking a button lets say “Rock” for example
  • It sends the request to our lambda and updates the table to the use_2_move column (it technically doesn't need to do that as it can just compare the move directly from the request to the user_1_move column, but it looks cooler that way.)
  • Then it compares the move and sends another message announcing the winner and update the table with the winner.

    alt_text