Skip to content

Commit d0ab830

Browse files
authored
Merge pull request #82 from PyBotDevs/corelib-rework-api.auth
Add custom token support and saving
2 parents 8d09c23 + b939817 commit d0ab830

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

api/auth.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1-
token = ''
2-
secret = ''
3-
public_key = ''
1+
# Imports
2+
import json
3+
import os
4+
5+
# Config
6+
wdir = os.getcwd()
7+
8+
# Setup
9+
with open(f'{wdir}/api/runtimeconfig.json', 'r') as f:
10+
global config
11+
config = json.load(f)
12+
13+
# Commands
14+
def get_token():
15+
"""Returns the token in `runtimeconfig.json`, if it exists.\nIf there is no token provided in the config, it will manually ask the user for input and it will autosave it."""
16+
if config["token"] == "":
17+
print("[!] Looks like you didn't input a token. Would you like to import one?")
18+
arg1 = input("[Yes/No/y/n] > ")
19+
if arg1.lower() == "y":
20+
arg2 = input("Enter your custom token: ")
21+
config["token"] = str(arg2)
22+
with open(f'{wdir}/api/runtimeconfig.json', 'w+') as f:
23+
json.dump(config, f, indent=4)
24+
print("[✅] Setup successful!")
25+
elif arg1.lower() == "n": return
26+
return str(config["token"])
27+
28+
def get_secret():
29+
"""Returns the bot client secret in `runtimeconfig.json`, if it exists."""
30+
if config["secret"] != "": return config["secret"]
31+
else: return "Secret has not been set."
32+
33+
def get_public_key():
34+
"""Returns the bot's public key in `runtimeconfig.json`, if it exists."""
35+
if config["public_key"]: return config["public_key"]
36+
else: return "Public key has not been set."

api/runtimeconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"token": "",
3+
"secret": "",
4+
"public_key": "",
5+
"runtime_options": []
6+
}

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ async def modify_balance(ctx:SlashContext, user:discord.User, modifier:int):
10731073

10741074
# Initialization
10751075
utils.ping.host()
1076-
client.run(api.auth.token)
1076+
client.run(api.auth.get_token())
10771077

10781078

10791079

0 commit comments

Comments
 (0)