Skip to content

Commit

Permalink
major change in mongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
skrphenix committed Feb 11, 2024
1 parent 536856e commit 7fe9b7e
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 251 deletions.
36 changes: 31 additions & 5 deletions economy with mongoDB/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# 📙Quickstart

# clone the repository
# Method - 1

## clone the repository

```sh
git clone https://github.com/Modern-Realm/economy-bot-discord.py
```

# Setting up the working directory & installing packages
## Setting up the working directory & installing packages

```sh
cd "economy-bot-discord.py/economy with mongoDB"
Expand All @@ -15,17 +17,41 @@ pip install -r requirements.txt

**Note:** make sure to install **any one** of these package`(discord.py, py-cord or nextcord)`

# Provide the secret keys/values in `.env` file
### Provide the secret keys/values in `.env` file

# Running the bot
## Running the bot

```sh
python main.py
```

🎉 Your discord bot should be online and ready to use!

<hr>
# Method - 2

## Download the source file

- [click here](https://github.com/Modern-Realm/economy-bot-discord.py/releases/download/v3.0.7/economy.with.mongoDB.zip)
to download the `zip` file.
- extract all the files & folders

## Install required packages

```shell
pip install -r requirements.txt
```

**Note:** make sure to install **any one** of these package`(discord.py, py-cord or nextcord)`

## Running the bot

```shell
python main.py
```

🎉 Your discord bot should be online and ready to use!

---

# Note: for discord.py users

Expand Down
67 changes: 67 additions & 0 deletions economy with mongoDB/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import discord

from discord.ext import commands
from dotenv import load_dotenv, find_dotenv
from pycolorise.colors import *

from modules import Database

__all__ = [
"Auth",
"EconomyBot"
]

load_dotenv(find_dotenv(raise_error_if_not_found=True))


class Auth:
# Make sure to add all details in '.env' file
TOKEN = os.getenv("TOKEN")
COMMAND_PREFIX = os.getenv("COMMAND_PREFIX")

CLUSTER_AUTH_URL = os.getenv("CLUSTER_AUTH_URL")
DB_NAME = os.getenv("DB_NAME")


class EconomyBot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.db = Database(Auth.CLUSTER_AUTH_URL, Auth.DB_NAME)

async def on_ready(self):
await self.change_presence(
status=discord.Status.online,
activity=discord.Game(f"{Auth.COMMAND_PREFIX}help")
)

# if you are using 'discord.py >=v2.0' comment(remove) below code
print(Purple("\nLoading Cogs:"))
for file in os.listdir("./cogs"):
if file.endswith(".py"):
filename = file[:-3]
try:
self.load_extension(f"cogs.{filename}")
print(Blue(f"- {filename} ✅ "))
except:
print(Blue(f"- {filename} ❌ "))

# if you are using 'discord.py >=v2.0' uncomment(add) below code
# print(Purple("\nLoading Cogs:"))
# for file in os.listdir("./cogs"):
# if file.endswith(".py"):
# filename = file[:-3]
# try:
# await client.load_extension(f"cogs.{filename}")
# print(Blue(f"- {filename} ✅ "))
# except:
# print(Blue(f"- {filename} ❌ "))

print()

await self.db.bank.create_table()
await self.db.inv.create_table()
print(Cyan("Created/modified tables successfully"))

print(Cyan(f"{self.user.name} is online !"))
21 changes: 11 additions & 10 deletions economy with mongoDB/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
it is not recommended to include these commands.
"""

from modules.bank_funcs import *
from base import EconomyBot

import discord

from discord.ext import commands


class Admin(commands.Cog):
def __init__(self, client: commands.Bot):
def __init__(self, client: EconomyBot):
self.client = client
self.bank = self.client.db.bank

@commands.command(aliases=["addmoney"], usage="<member*: @member> <amount*: integer> <mode: wallet or bank>")
@commands.is_owner()
Expand All @@ -36,8 +37,8 @@ async def add_money(self, ctx, member: discord.Member, amount: str, mode: str =
if amount > limit:
return await ctx.reply(f"You cannot add money more than {limit:,}")

await open_bank(member)
await update_bank(member, +amount, mode)
await self.bank.open_acc(member)
await self.bank.update_acc(member, +amount, mode)
await ctx.reply(f"You added {amount:,} in {member.mention}'s {mode}", mention_author=False)

@commands.command(aliases=["remoney"], usage="<member*: @member> <amount*: integer> <mode: wallet or bank>")
Expand All @@ -53,16 +54,16 @@ async def remove_money(self, ctx, member: discord.Member, amount: str, mode: str
return await ctx.reply("Please enter either wallet or bank only")

amount = int(amount)
await open_bank(member)
await self.bank.open_acc(member)

users = await get_bank_data(member)
users = await self.bank.get_acc(member)
user_amt = users[2 if mode == "bank" else 1]
if user_amt < amount:
return await ctx.reply(
f"You can only remove {user_amt:,} from {member.mention}'s {mode}"
)

await update_bank(member, -amount, mode)
await self.bank.update_acc(member, -amount, mode)
await ctx.reply(f"You removed {amount:,} from {member.mention}'s {mode}", mention_author=False)

@commands.command(usage="<member*: @member>")
Expand All @@ -72,11 +73,11 @@ async def reset_user(self, ctx, member: discord.Member):
if member.bot:
return await ctx.reply("Bots don't have account", mention_author=False)

users = await get_bank_data(member)
users = await self.bank.get_acc(member)
if users is None:
await open_bank(member)
await self.bank.open_acc(member)
else:
await reset_bank(member)
await self.bank.reset_acc(member)

return await ctx.reply(f"{member.mention}'s account has been reset", mention_author=False)

Expand Down
17 changes: 9 additions & 8 deletions economy with mongoDB/cogs/economy.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
from modules.bank_funcs import *
from base import EconomyBot

from numpy import random

from discord.ext import commands


class Economy(commands.Cog):
def __init__(self, client: commands.Bot):
def __init__(self, client: EconomyBot):
self.client = client
self.bank = self.client.db.bank

@commands.cooldown(1, 24 * 60 * 60)
@commands.command()
@commands.guild_only()
async def daily(self, ctx):
user = ctx.author
await open_bank(user)
await self.bank.open_acc(user)

rand_amt = random.randint(3000, 5000)
await update_bank(user, +rand_amt)
await self.bank.update_acc(user, +rand_amt)
await ctx.reply(f"Your daily pocket money is {rand_amt:,}", mention_author=False)

@commands.cooldown(1, 7 * 24 * 60 * 60)
@commands.command()
@commands.guild_only()
async def weekly(self, ctx):
user = ctx.author
await open_bank(user)
await self.bank.open_acc(user)

rand_amt = random.randint(7000, 10000)
await update_bank(user, +rand_amt)
await self.bank.update_acc(user, +rand_amt)
await ctx.reply(f"Your weekly pocket money is {rand_amt:,}", mention_author=False)

@commands.cooldown(1, 30 * 24 * 60 * 60)
@commands.command()
@commands.guild_only()
async def monthly(self, ctx):
user = ctx.author
await open_bank(user)
await self.bank.open_acc(user)

rand_amt = random.randint(30000, 50000)
await update_bank(user, +rand_amt)
await self.bank.update_acc(user, +rand_amt)
await ctx.reply(f"Your monthly pocket money is {rand_amt:,}", mention_author=False)


Expand Down
2 changes: 1 addition & 1 deletion economy with mongoDB/cogs/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from config import Auth
from base import Auth

import discord

Expand Down
31 changes: 16 additions & 15 deletions economy with mongoDB/cogs/fun.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from modules.bank_funcs import *
from base import EconomyBot

import discord
import asyncio
Expand All @@ -9,43 +9,44 @@


class Fun(commands.Cog):
def __init__(self, client: commands.Bot):
def __init__(self, client: EconomyBot):
self.client = client
self.bank = self.client.db.bank

@commands.command(aliases=["cf", "coinflip"], usage="<bet_on*: heads(H) or tails(T)> <amount*: integer>")
@commands.guild_only()
async def coin_flip(self, ctx, bet_on: str, amount: int):
user = ctx.author
await open_bank(user)
await self.bank.open_acc(user)

bet_on = "heads" if "h" in bet_on.lower() else "tails"
if not 500 <= amount <= 5000:
return await ctx.reply("You can only bet amount between 500 and 5000", mention_author=False)

reward = round(amount / 2)
users = await get_bank_data(user)
users = await self.bank.get_acc(user)
if users[1] < amount:
return await ctx.reply("You don't have enough money", mention_author=False)

coin = ["heads", "tails"]
result = random.choice(coin)

if result != bet_on:
await update_bank(user, -amount)
await self.bank.update_acc(user, -amount)
return await ctx.reply(f"Got {result}, you lost {amount:,}", mention_author=False)

await update_bank(user, +reward)
await self.bank.update_acc(user, +reward)
return await ctx.reply(f"Got {result}, you won {amount + reward:,}", mention_author=False)

@commands.command(usage="<amount*: integer")
@commands.guild_only()
async def slots(self, ctx: commands.Context, amount: int):
user = ctx.author
await open_bank(user)
await self.bank.open_acc(user)
if not 1000 <= amount <= 10000:
return await ctx.reply("You can only bet amount between 1000 and 10000", mention_author=False)

users = await get_bank_data(user)
users = await self.bank.get_acc(user)
if users[1] < amount:
return await ctx.reply("You don't have enough money", mention_author=False)

Expand Down Expand Up @@ -91,22 +92,22 @@ async def slots(self, ctx: commands.Context, amount: int):
s3 = slot[2]
if s1 == s2 == s3:
reward = round(amount / 2)
await update_bank(user, +reward)
await self.bank.update_acc(user, +reward)
content = f"{user.mention} Jackpot! you won {amount + reward:,}"
elif s1 == s2 or s2 == s3 or s1 == s3:
reward = round(amount / 4)
await update_bank(user, +reward)
await self.bank.update_acc(user, +reward)
content = f"{user.mention} GG! you only won {amount + reward:,}"
else:
await update_bank(user, -amount)
await self.bank.update_acc(user, -amount)
content = f"{user.mention} You lost {amount:,}"

return await msg.edit(content=content, embed=em)

@commands.command(usage="<amount*: integer> <bet_on: integer>")
async def dice(self, ctx, amount: int, bet_on: int = 6):
user = ctx.author
await open_bank(user)
await self.bank.open_acc(user)

rdice = [1, 2, 3, 4, 5, 6]
if bet_on not in rdice:
Expand All @@ -115,17 +116,17 @@ async def dice(self, ctx, amount: int, bet_on: int = 6):
if not 1000 <= amount <= 5000:
return await ctx.reply("You can only bet amount between 1000 and 5000", mention_author=False)

users = await get_bank_data(user)
users = await self.bank.get_acc(user)
if users[1] < amount:
return await ctx.reply("You don't have enough money", mention_author=False)

rand_num = random.choice(rdice)
if rand_num != bet_on:
await update_bank(user, -amount)
await self.bank.update_acc(user, -amount)
return await ctx.reply(f"Got {rand_num}, you lost {amount:,}", mention_author=False)

reward = round(amount / 2)
await update_bank(user, +reward)
await self.bank.update_acc(user, +reward)
await ctx.reply(f"Got {rand_num}, you won {amount + reward:,}", mention_author=False)


Expand Down
Loading

0 comments on commit 7fe9b7e

Please sign in to comment.