mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Initial commit
Transfer Didier to this repo
This commit is contained in:
commit
f1138c3b56
116 changed files with 353825 additions and 0 deletions
0
functions/database/__init__.py
Normal file
0
functions/database/__init__.py
Normal file
27
functions/database/birthdays.py
Normal file
27
functions/database/birthdays.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from functions.database import utils
|
||||
|
||||
|
||||
def get_user(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT day, month, year FROM birthdays WHERE userid = %s", (int(userid),))
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def get_users_on_date(day, month):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT userid FROM birthdays WHERE day = %s AND month = %s", (int(day), int(month),))
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def add_user(userid, day, month, year):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
if get_user(userid):
|
||||
cursor.execute("UPDATE birthdays SET day = %s, month = %s, year = %s WHERE userid = %s",
|
||||
(int(day), int(month), int(year), int(userid),))
|
||||
else:
|
||||
cursor.execute("INSERT INTO birthdays(userid, day, month, year) VALUES (%s, %s, %s, %s)",
|
||||
(int(userid), int(day), int(month), int(year),))
|
||||
connection.commit()
|
||||
107
functions/database/currency.py
Normal file
107
functions/database/currency.py
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import datetime
|
||||
from functions.database import utils, stats
|
||||
import time
|
||||
|
||||
|
||||
def dinks(userid):
|
||||
return getOrAddUser(userid)[1]
|
||||
|
||||
|
||||
def dinksAll(userid):
|
||||
platinumDinks = 0
|
||||
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT amount FROM inventory WHERE userid = %s AND itemid = %s", (int(userid), 1,))
|
||||
result = cursor.fetchall()
|
||||
|
||||
if result:
|
||||
platinumDinks = result[0][0]
|
||||
|
||||
return {"dinks": dinks(userid), "platinum": platinumDinks}
|
||||
|
||||
|
||||
def getAllRows():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
"""SELECT * FROM currencytable"""
|
||||
)
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def getAllPlatDinks():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT userid, amount FROM inventory WHERE itemid = 1")
|
||||
result = cursor.fetchall()
|
||||
dic = {}
|
||||
for user in result:
|
||||
dic[str(user[0])] = user[1]
|
||||
return dic
|
||||
|
||||
|
||||
def getOrAddUser(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "SELECT * FROM currencytable WHERE userid = %s"
|
||||
cursor.execute(
|
||||
query, (int(userid),)
|
||||
)
|
||||
result = cursor.fetchall()
|
||||
# User didn't exist yet, so create a new default file
|
||||
if len(result) == 0:
|
||||
createNewUser(userid, connection)
|
||||
return getOrAddUser(userid)
|
||||
return result[0]
|
||||
|
||||
|
||||
# TODO check for nightly bonus & add+return that instead of 420
|
||||
def nightly(userid):
|
||||
user = getOrAddUser(userid)
|
||||
today = datetime.datetime.today().date()
|
||||
lastNightly = datetime.datetime.fromtimestamp(user[9]).date()
|
||||
streak = int(user[10])
|
||||
if lastNightly < today:
|
||||
update(userid, "dinks", float(user[1]) + 420.0)
|
||||
update(userid, "nightly", int(time.time()))
|
||||
|
||||
# Update the streak
|
||||
if (today - lastNightly).days > 1:
|
||||
update(userid, "nightly_streak", 1)
|
||||
streak = 1
|
||||
else:
|
||||
update(userid, "nightly_streak", streak + 1)
|
||||
streak += 1
|
||||
|
||||
s = stats.getOrAddUser(userid)
|
||||
|
||||
if streak > int(s[5]):
|
||||
stats.update(userid, "longest_streak", streak)
|
||||
|
||||
stats.update(userid, "nightlies_count", int(s[6]) + 1)
|
||||
|
||||
return [True, 420, streak]
|
||||
return [False, 0, -1]
|
||||
|
||||
|
||||
def createNewUser(userid, connection=None):
|
||||
if connection is None:
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "INSERT INTO currencytable(userid, dinks, banklevel, investedamount, investeddays, profit, defense, offense, bitcoins, nightly) " \
|
||||
"VALUES (%s, 0.0, 1, 0.0, 0, 0, 1, 1, 0.0, 0)"
|
||||
cursor.execute(query, (int(userid),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def update(userid, column, value):
|
||||
_ = getOrAddUser(userid)
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "UPDATE currencytable " \
|
||||
"SET {} = %s " \
|
||||
"WHERE userid = %s".format(column)
|
||||
cursor.execute(query, (value, userid,))
|
||||
connection.commit()
|
||||
16
functions/database/dadjoke.py
Normal file
16
functions/database/dadjoke.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from functions.database import utils
|
||||
import random
|
||||
|
||||
|
||||
def getRandomJoke():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM dad_jokes")
|
||||
return random.choice(cursor.fetchall())[1]
|
||||
|
||||
|
||||
def addJoke(joke):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO dad_jokes(joke) VALUES (%s)", (str(joke),))
|
||||
connection.commit()
|
||||
60
functions/database/faq.py
Normal file
60
functions/database/faq.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
from functions.database import utils
|
||||
|
||||
|
||||
def getCategories():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
"""SELECT name FROM faq_categories"""
|
||||
)
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def addCategory(name):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
"""INSERT INTO faq_categories(name) VALUES (%s)""", (name.lower(),)
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
|
||||
def addQuestion(category: str, question: str, answer: str, answer_markdown: str = None):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Find the Id of this category
|
||||
cursor.execute(
|
||||
"""SELECT id FROM faq_categories WHERE name = %s""", (category.lower(),)
|
||||
)
|
||||
categoryId = cursor.fetchall()[0]
|
||||
|
||||
if not categoryId:
|
||||
return
|
||||
|
||||
categoryId = categoryId[0]
|
||||
|
||||
# Check if a markdown string has to be added
|
||||
if answer_markdown is None:
|
||||
cursor.execute(
|
||||
"""INSERT INTO faq_entries(category_id, question, answer) VALUES (%s, %s, E%s)""",
|
||||
(categoryId, question, answer,)
|
||||
)
|
||||
else:
|
||||
cursor.execute(
|
||||
"""INSERT INTO faq_entries(category_id, question, answer, answer_markdown) VALUES (%s, %s, E%s, E%s)""", (categoryId, question, answer, answer_markdown)
|
||||
)
|
||||
|
||||
connection.commit()
|
||||
|
||||
|
||||
def getCategory(category):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
"""SELECT *
|
||||
FROM faq_entries INNER JOIN faq_categories fc on faq_entries.category_id = fc.id
|
||||
WHERE %s = fc.name""",
|
||||
(category.lower(),)
|
||||
)
|
||||
return cursor.fetchall()
|
||||
27
functions/database/githubs.py
Normal file
27
functions/database/githubs.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from functions.database import utils
|
||||
|
||||
|
||||
def add(userid, link):
|
||||
user = get_user(userid)
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
if len(user) == 0:
|
||||
cursor.execute("INSERT INTO githubs(userid, githublink) VALUES (%s, %s)", (int(userid), str(link),))
|
||||
else:
|
||||
cursor.execute("""UPDATE githubs SET githublink = %s WHERE userid = %s""", (user[0][0] + "\n" + link, int(userid),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def getAll():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM githubs")
|
||||
result = cursor.fetchall()
|
||||
return result
|
||||
|
||||
|
||||
def get_user(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT githublink FROM githubs WHERE userid = %s", (int(userid),))
|
||||
return cursor.fetchall()
|
||||
29
functions/database/memes.py
Normal file
29
functions/database/memes.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from functions.database import utils
|
||||
|
||||
|
||||
def insert(id, name, fields):
|
||||
if getMeme(name)[0]:
|
||||
return [False, "Deze meme staat al in de database."]
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO memes(id, name, fields) VALUES (%s, %s, %s)", [int(id), name.lower(), int(fields)])
|
||||
connection.commit()
|
||||
return [True, "{} is toegevoegd aan de database.".format(name[0].upper() + name[1:].lower())]
|
||||
|
||||
|
||||
def getMeme(name):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM memes WHERE name like %s", ["%" + name.lower() + "%"])
|
||||
result = cursor.fetchall()
|
||||
if len(result) == 0:
|
||||
return [False, "Deze meme staat niet in de database."]
|
||||
return [True, result[0]]
|
||||
|
||||
|
||||
def getAllMemes():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM memes")
|
||||
result = cursor.fetchall()
|
||||
return result
|
||||
76
functions/database/muttn.py
Normal file
76
functions/database/muttn.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
from data import constants
|
||||
from functions.database import utils
|
||||
import random
|
||||
|
||||
|
||||
def getOrAddUser(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "SELECT * FROM muttn WHERE userid = %s"
|
||||
cursor.execute(
|
||||
query, (int(userid),)
|
||||
)
|
||||
result = cursor.fetchall()
|
||||
# User didn't exist yet, so create a new default file
|
||||
if len(result) == 0:
|
||||
createNewUser(userid, connection)
|
||||
return getOrAddUser(userid)
|
||||
return result[0]
|
||||
|
||||
|
||||
def getAllRows():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM muttn")
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def createNewUser(userid, connection):
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO muttn(userid, stats, count, message) VALUES (%s, %s, %s, %s)", (
|
||||
int(userid), random.randint(0, 50) + (random.randint(0, 100)/100), 0, 0,
|
||||
))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def muttn(userid, count, messageid):
|
||||
if str(userid) == constants.didierId:
|
||||
return
|
||||
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
user = getOrAddUser(userid)
|
||||
|
||||
# Dont' allow earlier messages to be spammed to avoid abuse
|
||||
if messageid < user[3]:
|
||||
return
|
||||
|
||||
# 5 or more = percentage increase
|
||||
if count >= 5:
|
||||
# React was removed & added again: don't do anything
|
||||
# New count is smaller than or equal to old max for this message
|
||||
if count <= user[4] and messageid == user[3]:
|
||||
return
|
||||
|
||||
cursor.execute("UPDATE muttn SET stats = %s, message = %s, highest = %s WHERE userid = %s",
|
||||
(float(user[1]) + (random.randint(2, 50) * count/1000), int(messageid), count, int(userid)))
|
||||
connection.commit()
|
||||
cursor.execute("UPDATE muttn SET count = %s WHERE userid = %s",
|
||||
(int(user[2]) + 1, int(userid),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def removeMuttn(message):
|
||||
if str(message.author.id) == constants.didierId:
|
||||
return
|
||||
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
user = getOrAddUser(message.author.id)
|
||||
|
||||
cursor.execute("UPDATE muttn SET count = %s WHERE userid = %s",
|
||||
(int(user[2]) - 1, int(message.author.id),))
|
||||
connection.commit()
|
||||
|
||||
58
functions/database/poke.py
Normal file
58
functions/database/poke.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
from functions.database import utils, stats
|
||||
import time
|
||||
|
||||
|
||||
def get():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM poke")
|
||||
return cursor.fetchall()[0]
|
||||
|
||||
|
||||
def update(user, new):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
"UPDATE poke "
|
||||
"SET current = %s, poketime = %s, previous = %s",
|
||||
(int(new), int(time.time()), int(user))
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
|
||||
def blacklisted(user):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT poke_blacklist FROM info WHERE userid = %s", (int(user),))
|
||||
res = cursor.fetchall()
|
||||
if len(res) == 0:
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO info(userid, poke_blacklist) VALUES(%s, False)", (int(user),))
|
||||
connection.commit()
|
||||
return False
|
||||
return res[0][0]
|
||||
|
||||
|
||||
# Changes the poke blacklist state to true or false
|
||||
def blacklist(user, bl=True):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("UPDATE info "
|
||||
"SET poke_blacklist = %s WHERE userid = %s", (bl, int(user),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
# Returns a list of all blacklisted users
|
||||
def getAllBlacklistedUsers():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT userid FROM info WHERE poke_blacklist = True")
|
||||
return [str(user[0]) for user in cursor.fetchall()]
|
||||
|
||||
|
||||
def reset():
|
||||
current = get()[0]
|
||||
new = stats.pokeResetCandidate(current, [int(user) for user in getAllBlacklistedUsers()])
|
||||
update(current, new)
|
||||
return new
|
||||
44
functions/database/prison.py
Normal file
44
functions/database/prison.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from functions.database import utils
|
||||
|
||||
|
||||
def getUser(userId):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM prison WHERE userid = %s", (int(userId),))
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def remove(userId):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("DELETE FROM prison WHERE userid = %s", (int(userId),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def imprison(userid, bailsum, days, daily):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO prison(userid, bail, days, daily) VALUES (%s, %s, %s, %s)",
|
||||
(int(userid), float(bailsum), int(days), float(daily),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def dailyLowers():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Release people from prison on their last day
|
||||
cursor.execute("DELETE FROM prison WHERE days = 1")
|
||||
connection.commit()
|
||||
|
||||
# Get all remaining users
|
||||
cursor.execute("SELECT * FROM prison")
|
||||
prisoners = cursor.fetchall()
|
||||
|
||||
for prisoner in prisoners:
|
||||
cursor.execute("UPDATE prison "
|
||||
"SET bail = %s, days = %s "
|
||||
"WHERE userid = %s",
|
||||
(float(prisoner[1]) - float(prisoner[3]), int(prisoner[2]) - 1,
|
||||
int(prisoner[0]),))
|
||||
connection.commit()
|
||||
154
functions/database/stats.py
Normal file
154
functions/database/stats.py
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
from functions import xp
|
||||
from functions.database import utils
|
||||
import json
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def getOrAddUser(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "SELECT * FROM personalstats WHERE userid = %s"
|
||||
cursor.execute(
|
||||
query, (int(userid),)
|
||||
)
|
||||
result = cursor.fetchall()
|
||||
# User didn't exist yet, so create a new default file
|
||||
if len(result) == 0:
|
||||
createNewUser(userid, connection)
|
||||
return getOrAddUser(userid)
|
||||
return result[0]
|
||||
|
||||
|
||||
def createNewUser(userid, connection=None):
|
||||
if connection is None:
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "INSERT INTO personalstats(userid, poked, robs_success, robs_failed, robs_total, longest_streak) " \
|
||||
"VALUES (%s, 0, 0, 0, 0, 0)"
|
||||
cursor.execute(query, (int(userid),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def getAllRows():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
"""SELECT * FROM personalstats"""
|
||||
)
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def update(userid, column, value):
|
||||
_ = getOrAddUser(userid)
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
query = "UPDATE personalstats " \
|
||||
"SET {} = %s " \
|
||||
"WHERE userid = %s".format(column)
|
||||
cursor.execute(query, (value, userid,))
|
||||
connection.commit()
|
||||
statsTracker(column)
|
||||
|
||||
|
||||
# Automatically change global stats
|
||||
def statsTracker(column):
|
||||
if column in stats()["rob"]:
|
||||
with open("files/stats.json", "r") as fp:
|
||||
s = json.load(fp)
|
||||
|
||||
s["rob"][column] += 1
|
||||
|
||||
with open("files/stats.json", "w") as fp:
|
||||
json.dump(s, fp)
|
||||
|
||||
|
||||
def stats():
|
||||
return {"rob": ["robs_failed", "robs_success"]}
|
||||
|
||||
|
||||
# Gets a random person that has been poked before & has not blacklisted themself
|
||||
def pokeResetCandidate(current, blacklisted):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM personalstats WHERE poked != 0 and userid != %s and userid not in %s",
|
||||
(int(current), tuple(blacklisted)))
|
||||
return random.choice(cursor.fetchall())[0]
|
||||
|
||||
|
||||
def sentMessage(message):
|
||||
user = message.author
|
||||
|
||||
# Ignore bots
|
||||
if user.bot:
|
||||
return
|
||||
|
||||
# Ignore dm's
|
||||
if message.guild is None:
|
||||
return
|
||||
|
||||
# Don't give xp for bot commands
|
||||
if message.content.lower().startswith(("didier", "owo", "?", "rps", "p!", "-")):
|
||||
return
|
||||
|
||||
user_db = getOrAddUser(user.id)
|
||||
|
||||
update(user.id, "messages", user_db[11] + 1)
|
||||
update_channel(message.channel.id)
|
||||
|
||||
# Only gain xp every minute
|
||||
if round(time.time()) - user_db[13] > 30:
|
||||
gainXp(user.id, user_db)
|
||||
|
||||
|
||||
def gainXp(user, user_db):
|
||||
update(user, "xp", user_db[12] + random.randint(5, 15) + (xp.calculate_level(user_db[12]) * 3))
|
||||
update(user, "last_message", round(time.time()))
|
||||
|
||||
|
||||
def getOrAddChannel(channelid: int):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("SELECT * FROM channel_activity WHERE channel_id = %s", (channelid,))
|
||||
|
||||
res = cursor.fetchall()
|
||||
|
||||
if not res:
|
||||
cursor.execute("INSERT INTO channel_activity(channel_id, message_count) VALUES (%s, 0)", (channelid,))
|
||||
connection.commit()
|
||||
|
||||
return getOrAddChannel(channelid)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def channel_activity(channel=None):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
# All channels
|
||||
if channel is None:
|
||||
cursor.execute("SELECT * FROM channel_activity")
|
||||
return cursor.fetchall()
|
||||
return getOrAddChannel(channel.id)
|
||||
|
||||
|
||||
def update_channel(channelid: int):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
channel = getOrAddChannel(channelid)[0]
|
||||
cursor.execute("UPDATE channel_activity SET message_count = %s WHERE channel_id = %s",
|
||||
(float(channel[1]) + 1, channelid))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def lower_channel(channelid: int, message_count):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("UPDATE channel_activity SET message_count = %s WHERE channel_id = %s",
|
||||
(float(message_count), channelid,))
|
||||
connection.commit()
|
||||
87
functions/database/store.py
Normal file
87
functions/database/store.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
from functions import checks
|
||||
from functions.database import utils, currency
|
||||
|
||||
|
||||
def buy(ctx, userid, itemid, amount):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
dinks = currency.dinks(userid)
|
||||
cursor.execute("SELECT * FROM store WHERE itemid = %s", (int(itemid),))
|
||||
result = cursor.fetchall()
|
||||
if not result:
|
||||
return False, "Er is geen item met dit id."
|
||||
|
||||
# Not an empty list, no IndexError.
|
||||
result = result[0]
|
||||
|
||||
cursor.execute("SELECT amount FROM inventory WHERE userid = %s AND itemid = %s", (int(userid), int(itemid),))
|
||||
inv = cursor.fetchall()
|
||||
# Check if user already owns this
|
||||
limit = result[3]
|
||||
if limit is not None \
|
||||
and inv \
|
||||
and inv[0][0] + amount > limit:
|
||||
return False, "Je kan dit item maar {} keer kopen.".format(limit)
|
||||
|
||||
isValid = checks.isValidAmount(ctx, result[2] * amount)
|
||||
|
||||
if not isValid[0]:
|
||||
return isValid
|
||||
|
||||
currency.update(userid, "dinks", dinks - (result[2] * amount))
|
||||
addItem(userid, itemid, amount, inv)
|
||||
return True, {"id": result[0], "name": result[1], "price": result[2] * amount}
|
||||
|
||||
|
||||
def addItem(userid, itemid, amount, inv):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
# It's already in there, add more to the counter
|
||||
if inv:
|
||||
amount += inv[0][0]
|
||||
cursor.execute("UPDATE inventory SET amount = %s WHERE userid = %s AND itemid = %s", (amount, userid, itemid))
|
||||
connection.commit()
|
||||
return
|
||||
|
||||
# Doesn't own this item yet, add a new row
|
||||
cursor.execute("INSERT INTO inventory(userid, itemid, amount) VALUES (%s, %s, %s)", (userid, itemid, amount))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def getAllItems():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("SELECT * FROM store")
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def getItemPrice(itemid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("SELECT price FROM store WHERE itemid = %s", (itemid,))
|
||||
return cursor.fetchone()
|
||||
|
||||
|
||||
def inventory(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("SELECT inventory.itemid, name, amount FROM inventory INNER JOIN store on inventory.itemid = store.itemid WHERE userid = %s", (int(userid),))
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
# Amount = amount of item before sell
|
||||
def sell(userid, itemid, sold, amount):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Don't store amount: 0
|
||||
if sold == amount:
|
||||
cursor.execute("DELETE FROM inventory WHERE userid = %s AND itemid = %s", (userid, itemid,))
|
||||
return connection.commit()
|
||||
|
||||
cursor.execute("UPDATE inventory SET amount = %s WHERE userid = %s AND itemid = %s", (amount - sold, userid, itemid))
|
||||
return connection.commit()
|
||||
17
functions/database/trump.py
Normal file
17
functions/database/trump.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from functions.database import utils
|
||||
import random
|
||||
|
||||
|
||||
def add(quote, date, location):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO trump(quote, date, location) VALUES (%s, %s, %s)", (str(quote), str(date), str(location),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def getRandomQuote():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM trump")
|
||||
result = cursor.fetchall()
|
||||
return random.choice(result)
|
||||
28
functions/database/twitch.py
Normal file
28
functions/database/twitch.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from functions.database import utils
|
||||
|
||||
|
||||
def add(userid, link):
|
||||
user = get_user(userid)
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
if len(user) == 0:
|
||||
cursor.execute("INSERT INTO twitch(userid, link) VALUES (%s, %s)", (int(userid), str(link),))
|
||||
else:
|
||||
cursor.execute("""UPDATE twitch SET link = %s WHERE userid = %s""", (link, int(userid),))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def getAll():
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT * FROM twitch")
|
||||
result = cursor.fetchall()
|
||||
return result
|
||||
|
||||
|
||||
def get_user(userid):
|
||||
connection = utils.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT link FROM twitch WHERE userid = %s", (int(userid),))
|
||||
return cursor.fetchall()
|
||||
|
||||
36
functions/database/utils.py
Normal file
36
functions/database/utils.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import psycopg2
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
connection = None
|
||||
|
||||
|
||||
def connect():
|
||||
global connection
|
||||
|
||||
if connection is None:
|
||||
create_connection()
|
||||
return connection
|
||||
|
||||
|
||||
def create_connection():
|
||||
global connection
|
||||
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open(os.path.join(dir_path, "../../files/database.json"), "r") as fp:
|
||||
db = json.load(fp)
|
||||
|
||||
connection = psycopg2.connect(
|
||||
host=db["host"],
|
||||
database=db["database"],
|
||||
user=db["username"],
|
||||
password=db["password"]
|
||||
)
|
||||
|
||||
|
||||
def reconnect():
|
||||
global connection
|
||||
|
||||
connection.close()
|
||||
create_connection()
|
||||
Loading…
Add table
Add a link
Reference in a new issue