added idolize and showidol commands, as well as an easter egg!

This commit is contained in:
Sakimori 2020-12-20 07:25:25 -05:00
parent 9aea2cf9dc
commit f194a60042
3 changed files with 100 additions and 14 deletions

View File

@ -23,9 +23,17 @@ def initialcheck():
timestamp text NOT NULL
); """
player_table_check_string = """ CREATE TABLE IF NOT EXISTS user_designated_players (
user_id integer PRIMARY KEY,
user_name text,
player_id text NOT NULL,
player_name text NOT NULL,
player_json_string text NOT NULL
);"""
if conn is not None:
c = conn.cursor()
c.execute(soulscream_table_check_string)
c.execute(player_table_check_string)
conn.commit()
conn.close()
@ -68,8 +76,38 @@ def cache_soulscream(username, soulscream):
if conn is not None:
c = conn.cursor()
c.execute(store_string, (username, soulscream, datetime.datetime.now(datetime.timezone.utc)))
conn.commit()
conn.commit()
conn.close()
def designate_player(user, player_json):
conn = create_connection()
store_string = """ INSERT INTO user_designated_players(user_id, user_name, player_id, player_name, player_json_string)
VALUES (?, ?, ?, ?, ?)"""
user_player = get_user_player_conn(conn, user)
c = conn.cursor()
if user_player is not None:
c.execute("DELETE FROM user_designated_players WHERE user_id=?", (user.id,)) #delete player if already exists
c.execute(store_string, (user.id, user.name, player_json["id"], player_json["name"], json.dumps(player_json)))
conn.commit()
conn.close()
def get_user_player_conn(conn, user):
try:
if conn is not None:
c = conn.cursor()
c.execute("SELECT player_json_string FROM user_designated_players WHERE user_id=?", (user.id,))
return json.loads(c.fetchone()[0])
else:
return False
except:
return False
def get_user_player(user):
conn = create_connection()
player = get_user_player_conn(conn, user)
conn.close()
return player

View File

@ -7,14 +7,18 @@ import database as db
onomancer_url = "https://onomancer.sibr.dev/api/"
name_stats_hook = "generateStats2?name="
def get_stats(username):
#check database for cached name first
def get_stats(name):
#yell at onomancer if not in cache or too old
response = requests.get(onomancer_url + name_stats_hook + urllib.parse.quote_plus(name))
if response.status_code == 200:
return response.json()
def get_scream(username):
scream = db.get_soulscream(username)
if scream is not None:
return scream
#yell at onomancer if not in cache or too old
response = requests.get(onomancer_url + name_stats_hook + urllib.parse.quote_plus(username))
if response.status_code == 200:
db.cache_soulscream(username, response.json()["soulscream"])
return response.json()["soulscream"]
else:
scream = get_stats(username)["soulscream"]
db.cache_soulscream(username, scream)
return scream

View File

@ -43,11 +43,11 @@ async def on_message(msg):
elif msg.channel.id == config()["soulscream channel id"]:
try:
await msg.channel.send(ono.get_stats(msg.author.nick))
await msg.channel.send(ono.get_scream(msg.author.nick))
except TypeError or AttributeError:
await msg.channel.send(ono.get_stats(msg.author.name))
await msg.channel.send(ono.get_scream(msg.author.name))
except AttributeError:
await msg.channel.send(ono.get_stats(msg.author.name))
await msg.channel.send(ono.get_scream(msg.author.name))
elif command.startswith("roman "):
possible_int_string = command.split(" ",1)[1]
@ -56,6 +56,32 @@ async def on_message(msg):
except ValueError:
await msg.channel.send(f"\"{possible_int_string}\" isn't an integer in Arabic numerals.")
elif command.startswith("idolize"):
if (command.startswith("idolizememe")):
meme = True
else:
meme = False
player_name = command.split(" ",1)[1]
try:
player_json = ono.get_stats(player_name)
db.designate_player(msg.author, player_json)
if not meme:
await msg.channel.send(f"{player_name} is now your idol.")
else:
await msg.channel.send(f"{player_name} is now {msg.author.display_name}'s idol.")
await msg.channel.send(f"Reply if {player_name} is your idol also.")
except:
await msg.channel.send("Something went wrong. Tell 16.")
elif command == "showidol":
try:
player_json = db.get_user_player(msg.author)
embed=build_star_embed(player_json)
embed.set_footer(text=msg.author.display_name)
await msg.channel.send(embed=embed)
except:
await msg.channel.send("We can't find your idol. Looked everywhere, too.")
elif command == "credit":
await msg.channel.send("Our avatar was graciously provided to us, with permission, by @HetreaSky on Twitter.")
@ -72,4 +98,22 @@ Our avatar was graciously provided to us, with permission, by @HetreaSky on Twit
await channel.send(text)
def build_star_embed(player_json):
starkeys = {"batting_stars" : "Batting", "pitching_stars" : "Pitching", "baserunning_stars" : "Baserunning", "defense_stars" : "Defense"}
embed = discord.Embed(color=discord.Color.purple(), title=player_json["name"])
for key in starkeys.keys():
starstring = str(player_json[key])
if "." in starstring:
starnum = int(starstring[0])
addhalf = True
else:
starnum = int(player_json[key])
addhalf = False
embedstring = "" * starnum
if addhalf:
embedstring += ""
embed.add_field(name=starkeys[key], value=embedstring, inline=False)
return embed
client.run(config()["token"])