Skip to main content

Buy command

this command would allow users to buy items from the shop.

  • /buy

Buy Example 1
Buy Example 2

Arguments

id

  • Description : id of item you want to buy
  • Type : Text
  • Argument Required : True

Get Stored Variable - 1

  • Variable : shop
  • Set Temporary Variable : shop

You would need another Get Stored Variable block for this command.

Get Stored Variable - 2

  • Variable : economy
  • Set Temporary Variable : users
Image Reference

Flow Image Reference

Calculate Value

  • Temporary Variable : item
Expression
let list = var('shop') ?? [];
let currency = "🪙";
let item = find(list, .id == arg('id'));

let balance = find(var('users'), .id == user.id)?.points ?? 0;

item != nil && !( item.role in user.role_ids ) && ( item.require in user.role_ids || item.require == nil ) && ( item.stock == nil || item.stock > 0 ) && balance >= item.price ?
item
:
[
"# ⚠️ Error",
"*Make sure the item exists, you have the required role, it's in stock, and you have enough points*",
"-# *and you cannot buy a role that you already have.*",
"- **Your Balance :** " + toJSON(balance) + " " + currency,
"- **Item Price :** " + (item != nil ? toJSON(item.price) + " " + currency : "N/A"),
"- **Item Stock :** " + (item != nil ? (item.stock != nil ? toJSON(item.stock) : "Unlimited") : "N/A"),
"- **Required Role :** " + (item != nil ? (item.require != nil ? "<@&" + item.require + ">" : "None") : "N/A"),
]

Comparison Condition

  • Base Value : {{type(var('item'))}}

Else

I've shown the else part before in this section as the setup for Match Condition is quite complex.

  • Create Response Message
Response Message
{{var('item') | join("\n")}}

Match Condition


Comparison ModeComparison Value
Equalmap

Handle Errors Block

  • Add a Handle Errors block after the match condition.
  • scroll down this page if you want an Image Reference

Handle Errors

⚠️Error Response Message
  • Add Create Response Message block after the "Handle Errors" option and paste the following text.
Create Response Message
# ⚠️ Error
- The bot doesn't have appropriate permissions, *or*
- Bot's highest role isn't ranked higher than the reward role, *or*
- The reward role has been deleted / doesn't exist anymore.

Try Blocks

🧩Add Role to Member
  • Target User : {{user}}
  • Target Role : {{var('item').role}}
💬Create Response Message
  • Create an embed
Embed Description
You have successfully bought the item : ** {{var('item').name}}**
*and received the role* - <@&{{var('item').role}}>

> You balance has been reduced by {{toJSON(var('item').price)}} 🪙
  • Field Name : Extra Notes :
Field Value
{{var('item').response ?? "none"}}
🛍️Updating item stock and member balance

The following blocks need to be adjacent to the CREATE RESPONSE MESSAGE block as shown in the image.

Image Reference

try blocks

🧮Calculate Value blocks
Expression
let list = var('users');
map(list, .id == user.id ?
{
"id": .id,
"points": .points - var('item').price
} : #)
📝Set Stored Variable blocks
  • Variable : economy
  • Operation : Overwrite
  • Value : {{result('CALCULATE_VALUE_BLOCK')}}

replace CALCULATE_VALUE_BLOCK with the the ID of the block just above this.