From 61e98684b632bbe299878cc8fd71723ce190430b Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 3 Sep 2024 04:43:13 +0200 Subject: [PATCH] invite system --- README.md | 2 +- aura.py | 50 ++++++++++++++++++++++++++++++++++++++++ install.py | 16 +++++++++++++ invite_sys.py | 18 +++++++++++++++ main.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ run.py | 13 +++++++++++ 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 aura.py create mode 100644 install.py create mode 100644 invite_sys.py create mode 100644 main.py create mode 100644 run.py diff --git a/README.md b/README.md index 0221e5d..114e466 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ AruaBot is a economy discord bot! # Installation run `python3 install.py` (Windows not supported yet) -then run `./.venv/bin/python3 main.py` \ No newline at end of file +then run `python3 run.py` \ No newline at end of file diff --git a/aura.py b/aura.py new file mode 100644 index 0000000..4e47195 --- /dev/null +++ b/aura.py @@ -0,0 +1,50 @@ +from json import dump, load + +class User: + def __init__(self, balance: int) -> None: + self.balance = balance + + def set_balance(self, balance) -> int: + self.balance = balance + return self.balance + + def add_balance(self, balance) -> int: + self.balance += balance + return self.balance + + def to_dict(self) -> dict: + return {'balance': self.balance} + + def to_md(self) -> str: + return f"**- balance: {self.balance}**" + +class Aura: + data: dict[str, User] = {} + file = '' + def __init__(self, file: str) -> None: + with open(file, 'r')as f: + self.file = file + data = load(f) + for k in data: + self.data[k] = User(data[k]['balance']) + + def load(self) -> None: + with open(self.file, 'r')as f: + data = load(f) + for k in data: + self.data[k] = User(data[k]['balance']) + + def get(self, user_id) -> User | None: + if user_id in self.data: + return self.data[user_id] + + def add(self, user_id) -> User: + self.data[user_id] = User(10) + return self.data[user_id] + + def save(self): + data = {} + for k in self.data: + data[k] = self.data[k].to_dict() + with open(self.file, 'w')as f: + dump(data, f, indent=2) \ No newline at end of file diff --git a/install.py b/install.py new file mode 100644 index 0000000..609e2a7 --- /dev/null +++ b/install.py @@ -0,0 +1,16 @@ +from os import system as cmd, name + +if name != 'nt': + print('initializing environment') + cmd('python3 -m venv .venv') + print('installing discord.py') + cmd('./.venv/bin/pip3 install discord.py') + cmd('./.venv/bin/pip3 install Flask') + token = input('Bot Token>') + invite = input('Permanent Invite Link>') + with open('config.py', 'w') as f: + f.write(f"BOT_TOKEN='{token}'\nINVITE='{invite}'") + with open('aura.json', 'w') as f1: + f1.write('{}') +else: + print('installer currently does not support windows') \ No newline at end of file diff --git a/invite_sys.py b/invite_sys.py new file mode 100644 index 0000000..2d37086 --- /dev/null +++ b/invite_sys.py @@ -0,0 +1,18 @@ +from aura import * +from config import * +from flask import Flask, redirect +from os import environ + +aura = Aura('aura.json') +app = Flask(__name__) + +@app.route('/') +def root(id): + aura.load() + user = aura.get(str(id)) + if user: + user.balance+=120 + aura.save() + return redirect(INVITE, code=302) + +app.run('127.0.0.1', environ['PORT']) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ef725b1 --- /dev/null +++ b/main.py @@ -0,0 +1,63 @@ +from aura import * +from config import * +import discord +from discord import app_commands +from discord.ext import commands + +aura = Aura('aura.json') +client = discord.Client(intents=discord.Intents.all()) +# bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) +tree = app_commands.CommandTree(client) + +@client.event +async def on_ready(): + print(client.user) + for guild in client.guilds: + async for member in guild.fetch_members(limit=None): + user = aura.get(str(member.id)) + if user==None: + aura.add(str(member.id)) + aura.save() + print(await tree.sync()) + +@client.event +async def on_message(msg: discord.Message): + user = aura.get(str(msg.author.id)) + user.balance += 3 + aura.save() + +@client.event +async def on_member_join(member: discord.Member): + user = aura.get(str(member.id)) + if not user: + user = aura.add(str(member.id)) + member.send(embed=discord.Embed(color=member.color, title='✨ Welcome to ORUS! ✨', description=f'**Aura: {user.balance}**')) + +@tree.command(name='aura', description='Check the amount of aura a member has') +async def get_aura(interaction: discord.Interaction, member: discord.Member = None): + aura.load() + if member == None: + member = interaction.user + user = aura.get(str(member.id)) + embed = discord.Embed(color=member.color, title=f'{member.name}\'s Aura', description=user.to_md()) + await interaction.response.send_message(embed=embed) + +@tree.command(name='send', description='Send aura to a member') +async def send(interaction: discord.Interaction, to: discord.Member, amount: int): + aura.load() + user = aura.get(str(interaction.user.id)) + user_to = aura.get(str(to.id)) + if user.balance >= amount: + user.balance -= amount + user_to.balance += amount + embed = discord.Embed( + color=interaction.user.color, + description=f'**From: {interaction.user.name}\nAmount: {amount}**' + ) + await to.send(embed=embed) + await interaction.response.send_message(f'Sent! You now have {user.balance} aura!', ephemeral=True) + aura.save() + else: + await interaction.response.send_message(f'Cannot send. You only have {user.balance} aura!', ephemeral=True) + +client.run(BOT_TOKEN) \ No newline at end of file diff --git a/run.py b/run.py new file mode 100644 index 0000000..7b79ec5 --- /dev/null +++ b/run.py @@ -0,0 +1,13 @@ +from subprocess import run +from threading import Thread + +t1 = Thread(target=run, args=(['./.venv/bin/python3', 'invite_sys.py'],)) +t2 = Thread(target=run, args=(['./.venv/bin/python3', 'main.py'],)) +t1.daemon = True +t2.daemon = True +t1.start() +t2.start() +while 1: + if input() == 'q': + t1.join(0) + t2.join(0) \ No newline at end of file