Building a discord game part 2

Building a discord game part 2

ยท

4 min read

I am sorry, I have been away I lost my favorite aunt last month ๐Ÿ˜ž May her soul continue to rest in peace. Amen ๐Ÿ™๐Ÿฝ. So because of that I just took time off to mourn. I am now much better, even though I miss her daily.

So today we start our discord bot game The economy game but I choose to call it "Economy Game"

Now we start, by importing important libraries

from gevent import monkey
import os
import random
import json
import discord
from automate import automate
from discord.ext import commands
intents = discord.Intents.all()
intents.members = True

Open Json File This JSON file is meant for the bank account details

# open json file
f = open('bank.json')

Bot Prefix This helps with the bot syntax like ($balance to create an account)

client = commands.Bot(command_prefix="$", intents=intents)

Starting Game Server

@client.event
async def on_ready():
    print('NFT Monopoly Ready!')

Creating An Account We need a code for our discord users to be able to create their own Personal bank account.

# Account function
async def open_account(user):
    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["NFT Wallet"] = 0
        users[str(user.id)]["Bank"] = 0

    with open("bank.json", "w") as f:
        json.dump(users, f)
    return True

Get bank details This is the code to get bank update.

async def get_bank_data():
    with open("bank.json", "r") as f:
        users = json.load(f)
    return users

Update Bank details This code helps our discord players update their bank details

async def update_bank(user, change=0, mode="NFT Wallet"):
    users = await get_bank_data()

    change = int(change)
    users[str(user.id)][mode] += change

    with open("bank.json", "w") as f:
        json.dump(users, f)

    balance = [users[str(user.id)]["NFT Wallet"], users[str(user.id)]["Bank"]]
    return balance

Game Balance

Syntax - $balance

# Game balance
@client.command()
async def balance(ctx):
    await open_account(ctx.author)

    user = ctx.author
    users = await get_bank_data()

    wallet_amount = users[str(user.id)]["NFT Wallet"]
    bank_amount = users[str(user.id)]["Bank"]
    em = discord.Embed(title=f"{ctx.author.name}'s balance",
                       color=discord.Color.red())
    em.add_field(name="NFT Wallet Balance", value=wallet_amount)
    em.add_field(name="Bank Balance", value=bank_amount)
    await ctx.send(embed=em)

Withdraw Code

Syntax - $withdraw

# Withdraw coin
@client.command()
async def withdraw(ctx, amount=None):
    await open_account(ctx.author)

    if amount == None:
        await ctx.send("Please enter the amount")
        return

    balance = await update_bank(ctx.author)

    amount = int(amount)
    if amount > balance[1]:
        await ctx.send("You don't have that much money!")
        return

    if amount < 0:
        await ctx.send("Amount must be positive!")
        return

    await update_bank(ctx.author, amount)
    await update_bank(ctx.author, -1 * amount, "Bank")

    await ctx.send(f"You withdrew {amount} coins!")

Deposit code

Syntax - $deposit

# Deposit coin
@client.command()
async def deposit(ctx, amount=None):
    await open_account(ctx.author)

    if amount == None:
        await ctx.send("Please enter the amount")
        return

    balance = await update_bank(ctx.author)

    amount = int(amount)
    if amount > balance[0]:
        await ctx.send("You don't have that much money!")
        return

    if amount < 0:
        await ctx.send("Amount must be positive!")
        return

    await update_bank(ctx.author, -1 * amount)
    await update_bank(ctx.author, amount, "Bank")

    await ctx.send(f"You deposited {amount} coins!")

Send Coin to other players code

Syntax - $send

# Send coin
@client.command()
async def send(ctx, member: discord.Member, amount=None):
    await open_account(ctx.author)
    await open_account(member)

    if amount == None:
        await ctx.send("Please enter the amount")
        return

    balance = await update_bank(ctx.author)
    if amount == "all":
        amount = balance[0]

    amount = int(amount)
    if amount > balance[1]:
        await ctx.send("You don't have that much money!")
        return

    if amount < 0:
        await ctx.send("Amount must be positive!")
        return

    await update_bank(ctx.author, -1 * amount, "Bank")
    await update_bank(member, amount, "Bank")

    await ctx.send(f"You gave {member} {amount} coins!")

Code for players to work and earn

Syntax - $work

# Work
@client.command()
async def work(ctx):
    await open_account(ctx.author)

    user = ctx.author
    users = await get_bank_data()

    earnings = random.randrange(100)
    work = 'Scientist', 'Bouncer', 'a Designer', 'a Musician', 'a' 'an Analyst'
    career = random.choice(work)

    await ctx.send(f"You Worked as {career} and earned  {earnings} coins!!!")

    users[str(user.id)]["NFT Wallet"] += earnings

    with open("bank.json", "w") as f:
        json.dump(users, f)

Code for players to gamble for coin

Syntax - $slots

@client.command()
async def slots(ctx, amount=None):
    await open_account(ctx.author)

    if amount == None:
        await ctx.send("Please enter the amount")
        return

    balance = await update_bank(ctx.author)

    amount = int(amount)
    if amount > balance[0]:
        await ctx.send("You don't have that much money!")
        return

    if amount < 0:
        await ctx.send("Amount must be positive!")
        return

    final = []
    for i in range(3):
        a = random.choice(["X", "0", "Q"])

        final.append(a)
    await ctx.send(str(final))

    if final[0] == final[1] or final[0] == final[2] or final[2] == final[1]:
        await update_bank(ctx.author, 2 * amount)
        await ctx.send("You won!!!")
    else:
        await update_bank(ctx.author, -1 * amount)
        await ctx.send("You lost!!!")

In part 3 we would build our bot game shop, shopping bag, buy and sell

ย