added dall-e, reworked bot, added nip89config

This commit is contained in:
Believethehype
2023-11-24 17:20:29 +01:00
parent 7c533ec439
commit 215916c1ef
16 changed files with 410 additions and 99 deletions

View File

@@ -1,4 +1,5 @@
# DATABASE LOGIC
import json
import sqlite3
import time
@@ -7,7 +8,7 @@ from dataclasses import dataclass
from datetime import timedelta
from logging import Filter
from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Metadata, Filter
from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Metadata, Filter, Options, Client
from utils.definitions import NEW_USER_BALANCE
from utils.nostr_utils import send_event
@@ -68,11 +69,11 @@ def add_to_sql_table(db, npub, sats, iswhitelisted, isblacklisted, nip05, lud16,
print(e)
def update_sql_table(db, npub, sats, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive):
def update_sql_table(db, npub, balance, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive):
try:
con = sqlite3.connect(db)
cur = con.cursor()
data = (sats, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, npub)
data = (balance, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, npub)
cur.execute(""" UPDATE users
SET sats = ? ,
@@ -97,8 +98,7 @@ def get_from_sql_table(db, npub):
row = cur.fetchone()
con.close()
if row is None:
user = None
return user
return None
else:
user = User
user.npub = row[0]
@@ -192,29 +192,55 @@ def update_user_balance(db, sender, sats, client, config):
def get_or_add_user(db, npub, client):
user = get_from_sql_table(db, npub)
if user is None:
name, nip05, lud16 = fetch_user_metadata(npub, client)
print("Adding User: " + name + " (" + npub +")")
print("Adding User: " + npub + " (" + npub + ")")
add_to_sql_table(db, npub, NEW_USER_BALANCE, False, False, nip05,
lud16, name, Timestamp.now().as_secs())
user = get_from_sql_table(db, npub)
else:
# update Name, Nip05 and lud16 lnaddress
user.name, user.nip05, user.lud16 = fetch_user_metadata(npub, client)
update_sql_table(db, user.npub, user.balance, user.iswhitelisted, user.isblacklisted, user.nip05, user.lud16,
user.name, Timestamp.now().as_secs())
return user
return user
def fetch_user_metadata(sender, client) -> (str, str, str):
def fetch_user_metadata(npub, client):
name = ""
nip05 = ""
lud16 = ""
# Get metadata
pk = PublicKey.from_hex(npub)
print(f"\nGetting profile metadata for {pk.to_bech32()}...")
filter = Filter().kind(0).author(pk).limit(1)
events = client.get_events_of([filter], timedelta(seconds=3))
if len(events) > 0:
latest_entry = events[0]
newest = 0
for entry in events:
if entry.created_at().as_secs() > newest:
newest = entry.created_at().as_secs()
latest_entry = entry
print(latest_entry.content())
profile = json.loads(latest_entry.content())
if profile.get("name"):
name = profile['name']
if profile.get("nip05"):
nip05 = profile['nip05']
if profile.get("lud16"):
lud16 = profile['lud16']
return name, nip05, lud16
def fetch_user_metadata2(sender, client) -> (str, str, str):
name = ""
nip05 = ""
lud16 = ""
try:
pk = PublicKey.from_hex(sender)
print(f"\nGetting profile metadata for {pk.to_bech32()}...")
profile_filter = Filter().kind(0).author(pk).limit(1)
events = client.get_events_of([profile_filter], timedelta(seconds=3))
events = client.get_events_of([profile_filter], timedelta(seconds=1))
if len(events) > 0:
ev = events[0]
metadata = Metadata.from_json(ev.content())
@@ -223,9 +249,11 @@ def fetch_user_metadata(sender, client) -> (str, str, str):
name = metadata.get_name()
nip05 = metadata.get_nip05()
lud16 = metadata.get_lud16()
else:
print("Profile not found")
return name, nip05, lud16
except:
print("Couldn't get meta information")
return name, nip05, lud16