DisBot.py/disbot.py

140 lines
3.2 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
# Start succes indicator
async def on_ready():
print(f'We have logged in as {bot.user}')
@bot.command()
# Ahoj function
async def ahoj(ctx):
await ctx.send("Zdravím miláčkové!")
@bot.command()
async def balance(ctx, member:discord.Member = "neni"):
# test if acount is opend and makes on if not
await open_account(ctx.author)
if member == "neni":
member = ctx.author
if member != ctx.author:
role = await check_role(ctx, ctx.author)
if role == False:
await ctx.send("Větší snahy ze strany tvé nastati budou než já podlehnu.")
return
users = await get_bank_data()
wallet_amt = users[str(member.id)]["wallet"]
em = discord.Embed(title = f"{member.name} bodíčky",color = discord.Color.red())
em.add_field(name = "Bodíčků máš", value = wallet_amt)
await ctx.send(embed = em)
@bot.command()
async def payout(ctx,member:discord.Member,amount = None):
await open_account(member)
role = await check_role(ctx, ctx.author)
if amount == None:
await ctx.send("Částku zapoměl napsat jsi.")
return
if role == False:
await ctx.send("Já nevěřit tvým šprímům, ty více ukázati musíš.")
if role:
await update_bank(member,amount)
await ctx.send(f"{member} dostal vyplaceno {amount} bodíčků!")
@bot.command()
async def cut(ctx,member:discord.Member,amount = None):
await open_account(member)
role = await check_role(ctx, ctx.author)
if amount == None:
await ctx.send("Částku zapoměl napsat jsi.")
return
if role == False:
await ctx.send("Já nevěřit tvým šprímům, ty více ukázati musíš.")
if role:
await update_bank(member,int(amount)*-1)
await ctx.send(f"{member} dostal zabaveno {amount} bodíčků!")
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
async def update_bank(user,change = 0):
users = await get_bank_data()
users[str(user.id)]["wallet"] += int(change)
with open("mainbank.json", "w") as f:
json.dump(users,f)
bal = [users[str(user.id)]["wallet"]]
return bal
async def check_role(ctx, user:discord.Member):
role = discord.utils.get(ctx.guild.roles,name = "Učitelé")
if role in user.roles:
return True
else:
return False