Simple KV Economy App
First Off
This app and README will allow you to learn the basics of Autocode's KV or Key-Value storage feature. It is highly recommended that you read through this README and take a while to examine the code, which contains many useful comments to help you along your journey to learn KV. If you just want this app for its functionalities, feel free to download it and come back to this if you get curious in the future.
Introduction
Key-Value storage is an epic functionality in Autocode that allows you to store values accross all of your endpoints by using unique Key and Value pairs. Basically, imagine a document that is password-protected, in that case, the password is the key, and the document contents are the value.
Prerequisites
What You Will Learn
- How to set a KV pair
- How to get a KV pair
- How to clear a KV pair
- How to make 3 commands that use all of the above
Step One: The Setup
Creating a project and linking a bot
- Make a new Autocode project.
- Link your Discord bot account.
- All of this information was covered in Scott's Guide.
Step Two: The Basics
No need to write any code just yet. This segment of the tutorial is going to guide you throughout the basic KV statements that we'll use for the 3 commands at the end.
Setting a Key-Value pair
So we can all agree that the Pixel Wizard is the best emoji ever right? Well by using the code below, we have just set PixelWizard as the value associated with the key: BestEmojiEver
await lib.utils.kv['@0.1.16'].set({
key: `BestEmojiEver`,
value: `PixelWizard`,
Getting A Key-Value pair
Now that we've set the PixelWizard value with the BestEmojiEver key, we can access it throughout all of our endpoints by getting it from the database. As you can see below, we are retrieving/getting the value for the key BestEmojiEver, which we know is PixelWizard. Notice how in the code, we've made it so that Emoji = OUR GET STATEMENT
, this means that now, Emoji is the value of the key we've retrieved. So Emoji = PixelWizard. The defaultValue
statement at the end makes it so that if no value is set for the key BestEmojiEver, it'll default to "What's your favorite emoji?". If you're wondering how to clear a value once you've set it, don't fret, we'll talk about it in the next section.
let Emoji = await lib.utils.kv['@0.1.16'].get({
key: `BestEmojiEver`,
defaultValue: `What is your favorite emoji?`
});
Clearing A Key-Value pair
What if we've now decided that we don't like the PixelWizard emoji anymore, and that our favorite emoji is now CartoonRobot? Well, we could just set it again and change the value, but that's no fun. What we're going to do is clear the value before setting it again. Nothing more simple to do, all we have to do is clear the value for our key, BestEmojiEver, and then it'll default back to the value we defined above.
await lib.utils.kv['@0.1.16'].clear({
key: `BestEmojiEver`
});
Step Three: Putting it all together
Now that we've learned all of this, we can put it all together to make 3 awesome commands:
!set-emoji
!my-emoji
!clear-emoji
First Command
- Make a new message.create endpoint in your new project. This was also covered in Scott's Guide
- Replace the code in the endpoint with this.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content.startsWith(`!set-emoji`)) {
let FavEmoji = context.params.event.content.split(' ')[1];
await lib.utils.kv['@0.1.16'].set({
key: `BestEmojiEver_${context.params.event.author.id}`,
value: `${FavEmoji}`
});
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Nice! You set your favorite emoji as ${FavEmoji}`,
})
}
Explanation
- 1st Line: Authenticating us with the API standard library
- 3rd Line: Checking if the message matches a certain content, if it does, it'll run the rest of the code.
- 4th Line: Telling the bot that the content after the command string !set-emoji, will be our favorite emoji
- 6th to 9th Line: Setting the favorite emoji for the user that used the command. The reason for the
${context.params.event.author.id}
part after BestEmojiEver_
is to make sure no 2 keys are ever the same, since every user's ID is unique.
- 11th to 15th Line: Sending a confirmation message that tells the user what emoji they've set as their favorite
Second Command
- Make another message.create endpoint. This is just for the sake of organization and neatness
- Replace the current content of the endpoint with this.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content.startsWith(`!my-emoji`)) {
let FavEmoji = await lib.utils.kv['@0.1.16'].get({
key: `BestEmojiEver_${context.params.event.author.id}`,
defaultValue: `What's your favorite emoji?`
});
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Your current favorite emoji is ${FavEmoji}!`,
})
}
Explanation
- 1st Line: Authenticates us with the API standard library
- 3rd Line: Checks if the message starts with a certain content. If it does, it'll run our code.
- 5th to 8th Line: Gets the user's current favorite emoji. Also tells the bot that if the user does not have a favorite emoji yet, it should ask them what their favorite emoji is.
- 10th to 14th Line: Sends the message that tells the user what their current favorite emoji is.
Third Command
- Just like the other ones, make a new message.create endpoint in your project.
- Replace the code in the endpoint with the code below
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content.startsWith(`!clear-emoji`)) {
await lib.utils.kv['@0.1.16'].clear({
key: `BestEmojiEver_${context.params.event.author.id}`
});
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Successfully cleared your favorite emoji`,
})
}
Explanation
- 1st Line: Authenticates us with the API standard library
- 3rd Line: Checks if the message starts with a certain content. If it does, it'll run our code.
- 5th to 7th Line: Clears the user's current favorite emoji value
- 9th to 13th Line: Sends the confirmation message that tells the user that they've cleared their favorite emoji.
KV Fun Facts
- Sadly, the amount of KV pairs isn't infinite, you can only store a maximum of 1024 Key-Value pairs at a time.
- Keys can be up to 1kb in size
- Values can be up to 18kb.
Conclusion
Overall, using KV can seem daunting or confusing, but when you dig a bit deeper, you'll find it quite simple, and most of all, extremely useful. In this app we've learned how to: set a Key-Value pair, get a value using its corresponding key, and also how to clear a value using its key. Using this, we've also managed to make 3 epic commands that all use kv! Hopefully, this goes to show you how much fun coding and learning new things can be! If you need help, support, or have any questions, check out the support category at the bottom of this app README.
The App
Now that you've learned all about KV, you have this app at your disposal to continue learning more advanced KV usage. To make learning even easier, this app feature extensive commenting to make sure you understand absolutely everything in the code! There are even some blank spots in certain comments so that you can test your knowledge!
Setup
- Install the app
- It'll ask you for a prefix, enter the one you want
- That's it!
Commands
- Flip: Gives a random number of pancakes between 1 & 3. This amount increases as you buy pans of increasing rarity. Eg. An Epic Pan gives you from 1-12 pancakes for every flip.
- Daily: Gives 50 pancakes once every day.
- Use-Lottery: Gives a random number of pancakes from 1 to 5000. Have to buy a lottery ticket from the shop first.
- Bal: Returns how much money you have in your bank currently.
- Inv: Returns all the items you have in your inventory currently.
- Profile: Displays an embed with the balance, inventory, and more information about the user who used the command.
- Shop: Returns a list of all the items available for purchase and their respective prices
- Buy-item-name: Purchases a specific item. Make sure to put
-
between all words
Support
Other