Build a player market: /gather gives a random item so there is something to sell, /market sell lists an item from your inventory at a price in coins, /market browse shows the listings, /market buy takes the coins and hands the item over, and two buyers can never get the same listing
A player-driven marketplace where members can gather random items, list them for sale at their own prices, browse active listings, and buy from each other. Uses MEE6 coins for transactions.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
✨ You found: ⚔️ Sword! You now have 1 of these.
📋 Listed ⚔️ Sword for 150 coins!
✨ You found: ⚔️ Sword! You now have 1 of these.
📋 Listed ⚔️ Sword for 120 coins!
2 items available
Seller: Alex
ID: listing_2
Seller: builder
ID: listing_1
✅ Purchased ⚔️ Sword for 150 coins!
⚔️ Sword × 1
❌ That listing no longer exists!
// Player Market: gather items, list them for sale, and trade with other members
const ITEMS = [
'⚔️ Sword',
'🛡️ Shield',
'🪓 Axe',
'🏹 Bow',
'🗡️ Dagger',
'🔮 Crystal',
'💎 Diamond',
'🌟 Star Fragment',
'🍄 Mushroom',
'🌿 Herb',
'📿 Amulet',
'👑 Crown',
'🧪 Potion',
'📜 Scroll',
'🔑 Key',
];
/** Whether a call was refused because the member has left the server. */
function hasLeft(err: unknown): boolean {
return String(err).includes('is not in this server');
}
// Random item selection
function randomItem(): string {
return ITEMS[Math.floor(Math.random() * ITEMS.length)];
}
// Gather command - get a random item. The very first gather is always a
// sword, so a newcomer has something to list right away; luck after that.
mee6.command('gather', { description: 'Gather a random item' }, async (ctx) => {
const m = mee6.store.member(ctx.author.id);
const gathered = await m.get<number>('gathered', 0);
const item = gathered === 0 ? ITEMS[0] : randomItem();
// Get current inventory
const inventory = await m.get<Record<string, number>>('inventory', {});
inventory[item] = (inventory[item] || 0) + 1;
await m.set('inventory', inventory);
await m.set('gathered', gathered + 1);
await ctx.reply(`✨ You found: **${item}**!\nYou now have ${inventory[item]} of these.`);
});
// Market sell - list an item for sale
mee6.command(
'market sell',
{
description: 'List an item from your inventory for sale',
options: [
{ name: 'item', type: 'string', description: 'Item name (e.g., Sword)', required: true },
{ name: 'price', type: 'integer', description: 'Price in coins', required: true },
],
},
async (ctx) => {
const itemInput = ctx.option<string>('item');
const price = ctx.option<number>('price');
if (price <= 0) {
await ctx.reply('❌ Price must be positive!');
return;
}
// Find matching item in inventory (case-insensitive, partial match)
const m = mee6.store.member(ctx.author.id);
const inventory = await m.get<Record<string, number>>('inventory', {});
const matchedItem = Object.keys(inventory).find((item) => item.toLowerCase().includes(itemInput.toLowerCase()));
if (!matchedItem || inventory[matchedItem] <= 0) {
await ctx.reply(`❌ You don't have any **${itemInput}** in your inventory!`);
return;
}
// Remove item from inventory
inventory[matchedItem] -= 1;
if (inventory[matchedItem] <= 0) {
delete inventory[matchedItem];
}
await m.set('inventory', inventory);
// Create listing — numbered in order, so an id is short enough to type
const number = await mee6.store.incr('next_listing');
const listingId = `listing_${number}`;
await mee6.store.set(
listingId,
{
item: matchedItem,
price: price,
seller: ctx.author.id,
sellerName: ctx.author.username,
timestamp: Date.now(),
},
{ ttl: 604800 }
); // 7 days
await ctx.reply(`📋 Listed **${matchedItem}** for **${price}** coins!`);
}
);
// Market browse - show all listings
mee6.command(
'market browse',
{
description: 'Browse all items for sale',
},
async (ctx) => {
const allKeys = await mee6.store.keys();
const listingKeys = allKeys.filter((k) => k.startsWith('listing_'));
if (listingKeys.length === 0) {
await ctx.reply('🏪 The market is empty! Use `/gather` to find items and `/market sell` to list them.');
return;
}
// Fetch the newest listings — their numbers say which — as a run makes
// 50 calls at most
listingKeys.sort((a, b) => Number(b.slice(8)) - Number(a.slice(8)));
const listings: Array<{ id: string; data: any }> = [];
for (const key of listingKeys.slice(0, 25)) {
const data = await mee6.store.get(key);
if (data) {
listings.push({ id: key, data });
}
}
// Sort by timestamp (newest first)
listings.sort((a, b) => b.data.timestamp - a.data.timestamp);
// Build embed
const fields = listings.slice(0, 25).map((listing, i) => ({
name: `${i + 1}. ${listing.data.item} — ${listing.data.price} coins`,
value: `Seller: ${listing.data.sellerName}\n*ID: \`${listing.id}\`*`,
inline: false,
}));
await ctx.reply({
embed: {
title: '🏪 Player Market',
description: `${listingKeys.length} item${listingKeys.length === 1 ? '' : 's'} available`,
fields: fields,
color: 0x3498db,
footer: { text: 'Use /market buy <listing_id> to purchase' },
},
});
}
);
// Market buy - purchase a listing
mee6.command(
'market buy',
{
description: 'Buy an item from the market',
options: [
{ name: 'listing_id', type: 'string', description: 'The listing ID from /market browse', required: true },
],
},
async (ctx) => {
const listingId = ctx.option<string>('listing_id');
// Fetch listing with atomic check
const listing = await mee6.store.get<any>(listingId);
if (!listing) {
await ctx.reply('❌ That listing no longer exists!');
return;
}
// Can't buy your own listing
if (listing.seller === ctx.author.id) {
await ctx.reply("❌ You can't buy your own listing!");
return;
}
// Check buyer has enough coins
const buyerBalance = await mee6.economy.balance(ctx.author.id);
if (buyerBalance.coins < listing.price) {
await ctx.reply(`❌ You need **${listing.price}** coins but only have **${buyerBalance.coins}**!`);
return;
}
// Atomic purchase - only one person can claim it
const claimed = await mee6.store.compareAndSet(`${listingId}_claimed`, null, ctx.author.id, { ttl: 60 });
if (!claimed) {
await ctx.reply('❌ Someone else bought this listing first!');
return;
}
// Pay the seller before taking the buyer's coins: a seller who has left
// the server cannot be paid, and their listing goes with them.
let sellerLeft = false;
try {
await mee6.economy.add(listing.seller, listing.price);
} catch (err) {
if (!hasLeft(err)) throw err;
sellerLeft = true;
}
// Delete the listing
await mee6.store.delete(listingId);
if (sellerLeft) {
await ctx.reply('❌ The seller has left the server: that listing is withdrawn, and your coins stay with you.');
return;
}
await mee6.economy.remove(ctx.author.id, listing.price);
// Give item to buyer
const buyerStore = mee6.store.member(ctx.author.id);
const buyerInventory = await buyerStore.get<Record<string, number>>('inventory', {});
buyerInventory[listing.item] = (buyerInventory[listing.item] || 0) + 1;
await buyerStore.set('inventory', buyerInventory);
await ctx.reply(`✅ Purchased **${listing.item}** for **${listing.price}** coins!`);
// Notify seller via DM
await mee6.dm(
listing.seller,
`💰 Your **${listing.item}** sold for **${listing.price}** coins to ${ctx.author.username}!`
);
}
);
// Inventory command - check your items
mee6.command('inventory', { description: 'View your items' }, async (ctx) => {
const m = mee6.store.member(ctx.author.id);
const inventory = await m.get<Record<string, number>>('inventory', {});
const items = Object.entries(inventory);
if (items.length === 0) {
await ctx.reply('📦 Your inventory is empty! Use `/gather` to find items.');
return;
}
const itemList = items
.sort((a, b) => b[1] - a[1])
.map(([item, count]) => `${item} × ${count}`)
.join('\n');
await ctx.reply({
embed: {
title: '📦 Your Inventory',
description: itemList,
color: 0x2ecc71,
footer: { text: `${items.length} unique item${items.length === 1 ? '' : 's'}` },
},
});
});