Store the message count for each member
You need to store the number of messages each member posts. To make sure your code works even if your Bot restarts, you should use the Root API's KeyValueStore type. Root automatically backs up and restores the key-value store, so your data is persistent.
1. Store the count
-
Open the file
src/example.ts. -
Locate the
onMessagefunction. -
Remove the
echofunctionality (listening for/echoand responding). -
Add the following lines of code to
onMessageafter the check for system messages. TheappDataproperty is theKeyValueStoreobject. Theupdatemethod is perfect for this use case, here's how it works:- Updates a value associated with a key.
- You must include a transformation function to perform the update: the old value is retrieved, the function is called with the old value, the function's return value is used as the new value in the key-value store.
- If the key does not exist, this conceptually becomes a set operation. The transformation function is applied to the default and the result becomes the value in the set operation.
const count: number = await rootServer.dataStore.appData.update(evt.userId, (val:number) => val + 1, 0);
console.log(evt.userId + " " + count);
2. Test and clean up
-
Build and run your Bot.
-
Use the Root native client to post messages to community channels. Verify that the
console.logmessage shows 1 on your first message, 2 on your second, etc. This indicates the storage andupdatemethod are working correctly. -
Stop your Bot.
-
Delete the
rootsdk.sqlite3file from your Bot's project folder. The file contains the key-value store data, so deleting the file will reset the counts.