DisBot.py/example-bot.py
2024-10-20 21:18:35 +02:00

94 lines
2.0 KiB
Python

import discord
from discord.ext import commands
from private.config import token
import json
import os
os.chdir("/home/godot/Documents/scripts/discord-bot.py")
#os.chdir("/opt/disbot")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
"""
@bot.event
async def on_message(message):
# test is bot is not responding to itself
if message.author == bot.user:
return
# hello reaction
if message.content.startswith('ahoj'):
await message.channel.send('Zdravím miláčkové!')
"""
@bot.command()
async def balance(ctx):
# test if acount is opend and makes on if not
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
wallet_amt = users[str(user.id)]["wallet"]
em = discord.Embed(title = f"{ctx.author.name}'s balance",color = discord.Color.red())
em.add_field(name = "Wallet balance", value = wallet_amt)
await ctx.send(embed = em)
async def open_account(user):
# opens json file to found if user id is present
users = await get_bank_data()
# tests presence
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
users[str(user.id)]["wallet"] = 100
# saves a new data in json
with open("mainbank.json", "w") as f:
json.dump(users,f)
return True
# opens json stored data
async def get_bank_data():
with open("mainbank.json", "r") as f:
users = json.load(f)
return users
# for testing perpesis only
@bot.command()
async def beg(ctx):
await open_account(ctx.author)
users = await get_bank_data()
user = ctx.author
earnings = 10
await ctx.send(f"Someone just gave you {earnings} coins")
users[str(user.id)]["wallet"] += earnings
with open("mainbank.json", "w") as f:
json.dump(users,f)
bot.run(token)