FEAT: Added Currency

FIX: Added some files to the .gitignore
DOC: Added the config.py
This commit is contained in:
Dan 2024-06-24 20:42:30 -04:00
parent 9f02c1da5e
commit fcc1489b74
5 changed files with 130 additions and 0 deletions

3
.gitignore vendored
View File

@ -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

15
.vscode/launch.json vendored Normal file
View File

@ -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"
}
]
}

14
config.py Normal file
View File

@ -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
}
}
}

35
main.py Normal file
View File

@ -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)

63
modules/user/currency.py Normal file
View File

@ -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))