From fcc1489b74f9caba5ae94b9d2b084cddbf6bd7f5 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 24 Jun 2024 20:42:30 -0400 Subject: [PATCH] FEAT: Added Currency FIX: Added some files to the .gitignore DOC: Added the config.py --- .gitignore | 3 ++ .vscode/launch.json | 15 ++++++++++ config.py | 14 +++++++++ main.py | 35 ++++++++++++++++++++++ modules/user/currency.py | 63 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 config.py create mode 100644 main.py create mode 100644 modules/user/currency.py diff --git a/.gitignore b/.gitignore index 5d381cc..ccbcd35 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,6 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +/modules/user/__init__.py +/modules/__init__.py +/data/selena.db \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..26e3a5d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Run: Selena", + "type": "debugpy", + "request": "launch", + "program": "${workspaceFolder}/main.py", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..55b0385 --- /dev/null +++ b/config.py @@ -0,0 +1,14 @@ +from dotenv import load_dotenv +import os + +load_dotenv() + +config = { + 'DISCORD_TOKEN': os.getenv('DISCORD_TOKEN'), + 'GUILD_ID': int(os.getenv('DISCORD_GUILD_ID')), + 'modules': { + 'currency': { + 'enabled': True + } + } +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..fc4523f --- /dev/null +++ b/main.py @@ -0,0 +1,35 @@ +import discord +from config import config + +TOKEN = config['DISCORD_TOKEN'] +GUILD_ID = config['GUILD_ID'] + +intents = discord.Intents.default() +intents.messages = True + + +class Selena(discord.Client): + def __init__(self): + super().__init__(intents=intents) + self.tree = discord.app_commands.CommandTree(self) + self.load_modules() + + def load_modules(self): + if config['modules']['currency']['enabled']: + from modules.user.currency import Currency + currency = Currency(self) + currency.setup(self.tree) + + async def setup_hook(self): + self.tree.copy_global_to(guild=discord.Object(id=GUILD_ID)) + await self.tree.sync(guild=discord.Object(id=GUILD_ID)) + + +bot = Selena() + + +@bot.event +async def on_ready(): + print(f'{bot.user.name} has connected to Discord!') + +bot.run(TOKEN) diff --git a/modules/user/currency.py b/modules/user/currency.py new file mode 100644 index 0000000..efe4476 --- /dev/null +++ b/modules/user/currency.py @@ -0,0 +1,63 @@ +import discord +from discord import app_commands +import random +import sqlite3 +import os + + +class Currency: + def __init__(self, bot): + self.bot = bot + self.db_path = 'data/selena.db' + self._init_db() + + def _init_db(self): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS users ( + user_id TEXT PRIMARY KEY, + kibble INTEGER DEFAULT 0 + ) + ''') + conn.commit() + + def _get_user_kibble(self, user_id): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute('SELECT kibble FROM users WHERE user_id = ?', + (user_id,)) + row = cursor.fetchone() + return row[0] if row else 0 + + def _update_user_kibble(self, user_id, amount): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute('INSERT OR IGNORE INTO users (user_id, kibble) VALUES (?, ?)', + (user_id, 0)) + cursor.execute('UPDATE users SET kibble = kibble + ? WHERE user_id = ?', + (amount, user_id)) + conn.commit() + + def setup(self, tree): + @tree.command(name='earn_kibble', description='Earn Kibble') + @app_commands.checks.cooldown(1, 30) + async def earn_kibble(interaction: discord.Interaction): + user_id = str(interaction.user.id) + amount = random.choices([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100], + [0.01, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 0.01])[0] + self._update_user_kibble(user_id, amount) + await interaction.response.send_message + (f'{interaction.user.mention} earned {amount} Kibble!') + + @tree.command(name='balance', description='Check your Kibble balance') + async def balance(interaction: discord.Interaction): + user_id = str(interaction.user.id) + balance = self._get_user_kibble(user_id) + await interaction.response.send_message + (f'{interaction.user.mention} has {balance} Kibble.') + + +def setup(bot): + bot.tree.add_cog(Currency(bot))