add sqlite database
This commit is contained in:
parent
6cfeed39e1
commit
6a9c1038d2
4 changed files with 38 additions and 7 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -3,6 +3,8 @@ config.toml
|
||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
|
database.db
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[codz]
|
*.py[codz]
|
||||||
|
|
|
||||||
36
bot.py
36
bot.py
|
|
@ -1,17 +1,23 @@
|
||||||
import os
|
import os
|
||||||
import importlib
|
import importlib
|
||||||
|
import aiosqlite
|
||||||
|
|
||||||
import nerimity
|
import nerimity
|
||||||
import pylast
|
import pylast
|
||||||
|
|
||||||
class Bot(nerimity.Client):
|
class Bot(nerimity.Client):
|
||||||
"""Extended client class for extra functionality."""
|
"""Extended client class for extra functionality."""
|
||||||
def __init__(self, lastfm_api_key: str, lastfm_api_secret: str, *args, **kwargs):
|
def __init__(self, lastfm_api_key: str, lastfm_api_secret: str, db_file: str = "database.db", *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# initialize last.fm client
|
||||||
self.lastfm = pylast.LastFMNetwork(
|
self.lastfm = pylast.LastFMNetwork(
|
||||||
api_key = lastfm_api_key,
|
api_key = lastfm_api_key,
|
||||||
api_secret = lastfm_api_secret
|
api_secret = lastfm_api_secret
|
||||||
)
|
)
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
self.db = None
|
||||||
|
self.db_file = db_file
|
||||||
|
|
||||||
def load_commands(self, commands_dir: str):
|
def load_commands(self, commands_dir: str):
|
||||||
for filename in os.listdir(commands_dir):
|
for filename in os.listdir(commands_dir):
|
||||||
|
|
@ -22,3 +28,29 @@ class Bot(nerimity.Client):
|
||||||
module.setup(self)
|
module.setup(self)
|
||||||
print(f"Loaded {module_name}")
|
print(f"Loaded {module_name}")
|
||||||
print("Registered commands:", self.commands)
|
print("Registered commands:", self.commands)
|
||||||
|
|
||||||
|
async def init_db(self):
|
||||||
|
print("Initializing database...")
|
||||||
|
self.db = await aiosqlite.connect(self.db_file)
|
||||||
|
await self.db.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS lastfm_users (
|
||||||
|
user_id TEXT PRIMARY KEY,
|
||||||
|
lastfm_username TEXT NOT NULL
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
await self.db.commit()
|
||||||
|
|
||||||
|
async def set_lastfm(self, user_id: str, username: str):
|
||||||
|
await self.db.execute(
|
||||||
|
"INSERT OR REPLACE INTO lastfm_users (user_id, lastfm_username) VALUES (?, ?)",
|
||||||
|
(user_id, username)
|
||||||
|
)
|
||||||
|
await self.db.commit()
|
||||||
|
|
||||||
|
async def get_lastfm(self, user_id: str):
|
||||||
|
async with self.db.execute(
|
||||||
|
"SELECT lastfm_username FROM lastfm_users WHERE user_id = ?",
|
||||||
|
(user_id,)
|
||||||
|
) as cursor:
|
||||||
|
row = await cursor.fetchone()
|
||||||
|
return row[0] if row else None
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
def error_msg(message: str):
|
|
||||||
return f"[#e5323b][Error] [#reset]{message}"
|
|
||||||
5
main.py
5
main.py
|
|
@ -1,7 +1,5 @@
|
||||||
import tomllib
|
import tomllib
|
||||||
|
|
||||||
import nerimity
|
import nerimity
|
||||||
import pylast
|
|
||||||
|
|
||||||
import bot
|
import bot
|
||||||
|
|
||||||
|
|
@ -10,8 +8,8 @@ with open("config.toml", "rb") as f:
|
||||||
|
|
||||||
# nerimity client
|
# nerimity client
|
||||||
bot = bot.Bot(
|
bot = bot.Bot(
|
||||||
token = config['token'],
|
|
||||||
prefix = '!',
|
prefix = '!',
|
||||||
|
token = config['token'],
|
||||||
lastfm_api_key = config['api_key'],
|
lastfm_api_key = config['api_key'],
|
||||||
lastfm_api_secret = config['api_secret']
|
lastfm_api_secret = config['api_secret']
|
||||||
)
|
)
|
||||||
|
|
@ -21,5 +19,6 @@ bot.load_commands("commands")
|
||||||
@bot.listen("on_ready")
|
@bot.listen("on_ready")
|
||||||
async def on_ready(params):
|
async def on_ready(params):
|
||||||
print(f"Logged in as {bot.account.username}")
|
print(f"Logged in as {bot.account.username}")
|
||||||
|
await bot.init_db()
|
||||||
|
|
||||||
bot.run()
|
bot.run()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue