Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 22, 2022 12:30 pm GMT

Building discord game part 2

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 monkeyimport osimport randomimport jsonimport discordfrom automate import automatefrom discord.ext import commandsintents = discord.Intents.all()intents.members = True

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

# open json filef = 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.eventasync 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 functionasync 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 [email protected]()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 [email protected]()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 [email protected]()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 [email protected]()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

# [email protected]()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


Original Link: https://dev.to/designegycreatives/building-discord-game-part-2-164e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To