DisBot.py/disbot.py
2025-03-11 09:29:06 +01:00

232 lines
7.1 KiB
Python

import discord
import os, datetime, shutil, json
#from private.config import token
from discord import Option
os.chdir("/opt/disbot")
bot = discord.Bot()
g_ids = [1278700374756298834] #Schoool
date = datetime.datetime.now()
@bot.event
async def on_ready():
shutil.copy("./mainbank.json", f"backup/{date.year}-{date.month}-{date.day-1}.json")
print(f'We have logged in as {bot.user}')
@bot.slash_command(guild_ids=g_ids, name = "ahoj", description="Je slušností pozdravit")
async def ahoj(ctx):
await ctx.channel.trigger_typing() # that bot is typing
try:
await ctx.respond(f"Zdravím miláčkové!")
except:
await ctx.send(f"Zdravím miláčkové!")
@bot.slash_command(guild_ids=g_ids, name = "balance", description="Kolik bodíčků máš?")
async def balance(ctx, koho: Option(str, description="Koho bodíčky chceš věděti?", required = False)):
await ctx.channel.trigger_typing() # that bot is typing
if koho == None:
koho = str(ctx.author.id)
for mem in koho.split():
mem = mem.strip("<>@")
if mem != str(ctx.author.id):
role = await check_role(ctx, ctx.author)
if role == False:
try:
await ctx.respond("Větší snahy ze strany tvé nastati budou než já podlehnu.")
except:
await ctx.send("Větší snahy ze strany tvé nastati budou než já podlehnu.")
break
member = await bot.fetch_user(int(mem))
await open_account(ctx.author.id)
await open_account(mem)
users = await get_bank_data()
wallet_amt = users[str(mem)]["wallet"]
leader_board = {}
total = []
for user in users:
name = int(user)
leader_board[users[user]["wallet"]] = name
total.append([users[user]["wallet"]])
total = sorted(total, reverse=True)
poradi = total.index([users[str(mem)]["wallet"]]) +1
em = discord.Embed(title = f"{member.name}",color = discord.Color.blue())
em.add_field(name = "Bodíčků máš", value = wallet_amt, inline = False)
em.add_field(name = "V poradi jsi:", value = poradi, inline = False)
em.set_thumbnail(url = member.avatar) # idk what to do with this
try:
await ctx.respond("Co bych pro tebe neudělal!")
except:
await ctx.send("Co bych pro tebe neudělal!")
await ctx.author.send(embed = em)
@bot.slash_command(guild_ids=g_ids, name="shame", descritpion="Shows users that shud sink in shame")
async def shame(ctx, hranice: Option(int, description = "Where is the line", required = False) = -1):
await ctx.channel.trigger_typing() # that bot is typing
try:
await ctx.respond("Rado se, pracuju na tom.")
except:
await ctx.send("Rado se, pracuju na tom.")
users = await get_bank_data()
loser_dict = {}
em = discord.Embed(title = "List of shame", color = discord.Color.red())
for user in users:
if users[user]["wallet"] <= hranice:
#creates dictionary of needed info
debt = users[user]["wallet"]
if int(users[user]["wallet"]) in loser_dict:
loser_dict[debt].append(user)
else:
loser_dict[debt] = []
loser_dict[debt].append(user)
#sortes
loser_dict = dict(sorted(loser_dict.items()))
# formats string to desired format
for loser in loser_dict:
i = 0
while i < len(loser_dict[loser]):
loser_dict[loser][i] = f'<@{loser_dict[loser][i]}>'
i += 1
em.add_field(name = loser, value = ", ".join(loser_dict[loser]), inline = False)
await ctx.author.send(embed = em)
@bot.slash_command(guild_ids=g_ids, name="laska_k_svetu", descritpion="Auto send message to users in shame")
async def laska_k_svetu(ctx, message: Option(str, descritpion = "What to send", required = True), hranice: Option(int, description = "Where is the line", required = False) = -1):
await ctx.channel.trigger_typing() # that bot is typing
try:
await ctx.respond("Rado se, pracuju na tom.")
except:
await ctx.send("Rado se, pracuju na tom.")
users = await get_bank_data()
for user in users:
if users[user]["wallet"] <= hranice:
user_obj = await bot.fetch_user(int(user))
await user_obj.send(f"{message}. Máš {users[user]['wallet']} bodíčků.")
@bot.slash_command(guild_ids=g_ids, name="payout", descritpion="Pays user given amount")
async def payout(ctx, komu: Option(str, description = "Komu to sebrat?", required = True), kolik: Option(int, description = "Kolik bodicku sebrat", required = True)):
await ctx.channel.trigger_typing() # that bot is typing
try:
await ctx.respond("Pracuje se na tom.")
except:
await ctx.send("Pracuje se na tom.")
for mem in komu.split():
mem = mem.strip("<>@")
member = await bot.fetch_user(int(mem))
await open_account(mem)
await update_bank(member,kolik)
await ctx.send(f"<@{mem}> dostal vyplaceno {kolik} bodíčků!")
@bot.slash_command(guild_ids=g_ids, name = "cut", description = "Takes bodičky from users")
async def cut(ctx, komu: Option(str, description = "Komu mam sebrat bodiky?", required = True),kolik: Option(int, descritpion = "Kolik mam sebrat bodičku?", required = True)):
await ctx.channel.trigger_typing()
try:
await ctx.respond("Pracuje se na tom.")
except:
await ctx.send("Pracuje se na tom.")
for mem in komu.split():
mem = mem.strip("<>@")
member = await bot.fetch_user(int(mem))
await open_account(mem)
await update_bank(member,int(kolik)*-1)
await ctx.send(f"<@{mem}> dostal zabaveno {kolik} bodíčků!")
async def open_account(id):
# opens json file to found if user id is present
users = await get_bank_data()
# tests presence
if str(id) in users:
return False
else:
users[str(id)] = {}
users[str(id)]["wallet"] = 0
# 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