Skip to content

Commit 7fe9b7e

Browse files
committed
major change in mongoDB
1 parent 536856e commit 7fe9b7e

15 files changed

+306
-251
lines changed

economy with mongoDB/README.md

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# 📙Quickstart
22

3-
# clone the repository
3+
# Method - 1
4+
5+
## clone the repository
46

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

9-
# Setting up the working directory & installing packages
11+
## Setting up the working directory & installing packages
1012

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

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

18-
# Provide the secret keys/values in `.env` file
20+
### Provide the secret keys/values in `.env` file
1921

20-
# Running the bot
22+
## Running the bot
2123

2224
```sh
2325
python main.py
2426
```
2527

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

28-
<hr>
30+
# Method - 2
31+
32+
## Download the source file
33+
34+
- [click here](https://github.com/Modern-Realm/economy-bot-discord.py/releases/download/v3.0.7/economy.with.mongoDB.zip)
35+
to download the `zip` file.
36+
- extract all the files & folders
37+
38+
## Install required packages
39+
40+
```shell
41+
pip install -r requirements.txt
42+
```
43+
44+
**Note:** make sure to install **any one** of these package`(discord.py, py-cord or nextcord)`
45+
46+
## Running the bot
47+
48+
```shell
49+
python main.py
50+
```
51+
52+
🎉 Your discord bot should be online and ready to use!
53+
54+
---
2955

3056
# Note: for discord.py users
3157

economy with mongoDB/base.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import discord
3+
4+
from discord.ext import commands
5+
from dotenv import load_dotenv, find_dotenv
6+
from pycolorise.colors import *
7+
8+
from modules import Database
9+
10+
__all__ = [
11+
"Auth",
12+
"EconomyBot"
13+
]
14+
15+
load_dotenv(find_dotenv(raise_error_if_not_found=True))
16+
17+
18+
class Auth:
19+
# Make sure to add all details in '.env' file
20+
TOKEN = os.getenv("TOKEN")
21+
COMMAND_PREFIX = os.getenv("COMMAND_PREFIX")
22+
23+
CLUSTER_AUTH_URL = os.getenv("CLUSTER_AUTH_URL")
24+
DB_NAME = os.getenv("DB_NAME")
25+
26+
27+
class EconomyBot(commands.Bot):
28+
def __init__(self, *args, **kwargs):
29+
super().__init__(*args, **kwargs)
30+
31+
self.db = Database(Auth.CLUSTER_AUTH_URL, Auth.DB_NAME)
32+
33+
async def on_ready(self):
34+
await self.change_presence(
35+
status=discord.Status.online,
36+
activity=discord.Game(f"{Auth.COMMAND_PREFIX}help")
37+
)
38+
39+
# if you are using 'discord.py >=v2.0' comment(remove) below code
40+
print(Purple("\nLoading Cogs:"))
41+
for file in os.listdir("./cogs"):
42+
if file.endswith(".py"):
43+
filename = file[:-3]
44+
try:
45+
self.load_extension(f"cogs.{filename}")
46+
print(Blue(f"- {filename} ✅ "))
47+
except:
48+
print(Blue(f"- {filename} ❌ "))
49+
50+
# if you are using 'discord.py >=v2.0' uncomment(add) below code
51+
# print(Purple("\nLoading Cogs:"))
52+
# for file in os.listdir("./cogs"):
53+
# if file.endswith(".py"):
54+
# filename = file[:-3]
55+
# try:
56+
# await client.load_extension(f"cogs.{filename}")
57+
# print(Blue(f"- {filename} ✅ "))
58+
# except:
59+
# print(Blue(f"- {filename} ❌ "))
60+
61+
print()
62+
63+
await self.db.bank.create_table()
64+
await self.db.inv.create_table()
65+
print(Cyan("Created/modified tables successfully"))
66+
67+
print(Cyan(f"{self.user.name} is online !"))

economy with mongoDB/cogs/admin.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
it is not recommended to include these commands.
99
"""
1010

11-
from modules.bank_funcs import *
11+
from base import EconomyBot
1212

1313
import discord
1414

1515
from discord.ext import commands
1616

1717

1818
class Admin(commands.Cog):
19-
def __init__(self, client: commands.Bot):
19+
def __init__(self, client: EconomyBot):
2020
self.client = client
21+
self.bank = self.client.db.bank
2122

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

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

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

5556
amount = int(amount)
56-
await open_bank(member)
57+
await self.bank.open_acc(member)
5758

58-
users = await get_bank_data(member)
59+
users = await self.bank.get_acc(member)
5960
user_amt = users[2 if mode == "bank" else 1]
6061
if user_amt < amount:
6162
return await ctx.reply(
6263
f"You can only remove {user_amt:,} from {member.mention}'s {mode}"
6364
)
6465

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

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

75-
users = await get_bank_data(member)
76+
users = await self.bank.get_acc(member)
7677
if users is None:
77-
await open_bank(member)
78+
await self.bank.open_acc(member)
7879
else:
79-
await reset_bank(member)
80+
await self.bank.reset_acc(member)
8081

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

economy with mongoDB/cogs/economy.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
from modules.bank_funcs import *
1+
from base import EconomyBot
22

33
from numpy import random
44

55
from discord.ext import commands
66

77

88
class Economy(commands.Cog):
9-
def __init__(self, client: commands.Bot):
9+
def __init__(self, client: EconomyBot):
1010
self.client = client
11+
self.bank = self.client.db.bank
1112

1213
@commands.cooldown(1, 24 * 60 * 60)
1314
@commands.command()
1415
@commands.guild_only()
1516
async def daily(self, ctx):
1617
user = ctx.author
17-
await open_bank(user)
18+
await self.bank.open_acc(user)
1819

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

2324
@commands.cooldown(1, 7 * 24 * 60 * 60)
2425
@commands.command()
2526
@commands.guild_only()
2627
async def weekly(self, ctx):
2728
user = ctx.author
28-
await open_bank(user)
29+
await self.bank.open_acc(user)
2930

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

3435
@commands.cooldown(1, 30 * 24 * 60 * 60)
3536
@commands.command()
3637
@commands.guild_only()
3738
async def monthly(self, ctx):
3839
user = ctx.author
39-
await open_bank(user)
40+
await self.bank.open_acc(user)
4041

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

4546

economy with mongoDB/cogs/events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from config import Auth
1+
from base import Auth
22

33
import discord
44

economy with mongoDB/cogs/fun.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from modules.bank_funcs import *
1+
from base import EconomyBot
22

33
import discord
44
import asyncio
@@ -9,43 +9,44 @@
99

1010

1111
class Fun(commands.Cog):
12-
def __init__(self, client: commands.Bot):
12+
def __init__(self, client: EconomyBot):
1313
self.client = client
14+
self.bank = self.client.db.bank
1415

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

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

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

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

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

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

4041
@commands.command(usage="<amount*: integer")
4142
@commands.guild_only()
4243
async def slots(self, ctx: commands.Context, amount: int):
4344
user = ctx.author
44-
await open_bank(user)
45+
await self.bank.open_acc(user)
4546
if not 1000 <= amount <= 10000:
4647
return await ctx.reply("You can only bet amount between 1000 and 10000", mention_author=False)
4748

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

@@ -91,22 +92,22 @@ async def slots(self, ctx: commands.Context, amount: int):
9192
s3 = slot[2]
9293
if s1 == s2 == s3:
9394
reward = round(amount / 2)
94-
await update_bank(user, +reward)
95+
await self.bank.update_acc(user, +reward)
9596
content = f"{user.mention} Jackpot! you won {amount + reward:,}"
9697
elif s1 == s2 or s2 == s3 or s1 == s3:
9798
reward = round(amount / 4)
98-
await update_bank(user, +reward)
99+
await self.bank.update_acc(user, +reward)
99100
content = f"{user.mention} GG! you only won {amount + reward:,}"
100101
else:
101-
await update_bank(user, -amount)
102+
await self.bank.update_acc(user, -amount)
102103
content = f"{user.mention} You lost {amount:,}"
103104

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

106107
@commands.command(usage="<amount*: integer> <bet_on: integer>")
107108
async def dice(self, ctx, amount: int, bet_on: int = 6):
108109
user = ctx.author
109-
await open_bank(user)
110+
await self.bank.open_acc(user)
110111

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

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

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

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

131132

0 commit comments

Comments
 (0)