From 3f63c9b7b74a05f5edfd634e4681c3ad0afd2a45 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:44:35 +0530 Subject: [PATCH 1/5] Add `other_keys` section to `runtimeconfig.json` This allows external API keys and tokens to be added, so that other API libraries can be authorized. Also updated `replit` mode to false. --- api/runtimeconfig.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/api/runtimeconfig.json b/api/runtimeconfig.json index e7c0a7a0..53e55944 100644 --- a/api/runtimeconfig.json +++ b/api/runtimeconfig.json @@ -3,5 +3,11 @@ "secret": "", "public_key": "", "runtime_options": [], - "replit": true + "replit": false, + "other_keys": { + "openweathermap": "", + "reddit": "", + "ossapi": "", + "chatgpt": "" + } } From d2cba9be46b8bc15419cb37041bc4b6f2761b6e7 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:45:25 +0530 Subject: [PATCH 2/5] Improve some logic --- api/auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/auth.py b/api/auth.py index 19d6bcee..43a45751 100644 --- a/api/auth.py +++ b/api/auth.py @@ -30,7 +30,7 @@ def get_secret(): def get_public_key(): """Returns the bot's public key in `runtimeconfig.json`, if it exists.""" - if config["public_key"]: return config["public_key"] + if config["public_key"] != "": return config["public_key"] else: return "Public key has not been set." def get_mode() -> bool: From fc3c9ba9e8d60cf487a2fba8699b0b102ada367c Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:46:01 +0530 Subject: [PATCH 3/5] Add new `auth` library methods for fetching external token and runtime options --- api/auth.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api/auth.py b/api/auth.py index 43a45751..8f1e9d7e 100644 --- a/api/auth.py +++ b/api/auth.py @@ -36,3 +36,12 @@ def get_public_key(): def get_mode() -> bool: """Returns a boolean of the current runtime mode.\n\nReturns `True` if replit mode is active, returns `False` if replit mode is inactive.""" return config["replit"] + +def ext_token(token_name: str) -> str: + """Returns an external extra authorization token from `runtimeconfig.json`, if it exists.""" + return str(config["other_keys"][token_name]) + #except KeyError: return "This external authorization key does not exist." + +def get_runtime_options() -> dict: + """Returns a dict of all the client's runtime configuration options, as well as their respective values.""" + return dict(config["runtime_options"]) From 1ee43b0aaae1c55966196a636ee15ed0ee125f8a Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:46:47 +0530 Subject: [PATCH 4/5] Add new client runtime configuration options --- api/runtimeconfig.json | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/api/runtimeconfig.json b/api/runtimeconfig.json index 53e55944..1ad0f84d 100644 --- a/api/runtimeconfig.json +++ b/api/runtimeconfig.json @@ -2,7 +2,16 @@ "token": "", "secret": "", "public_key": "", - "runtime_options": [], + "runtime_options": { + "themes": false, + "log_messages": true, + "guild_log_blacklist": {}, + "only_log_whitelist": false, + "guild_log_whitelist": {}, + "ping_server_override": false, + "debug_mode": false, + "show_ping_on_startup": true + }, "replit": false, "other_keys": { "openweathermap": "", From 4af51680f622c5aa1d9f8f83317b8cd847000bb3 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:49:58 +0530 Subject: [PATCH 5/5] Update cogs to use new `auth` token system --- cogs/osu.py | 5 +++-- cogs/reddit.py | 5 +++-- cogs/utils.py | 4 +++- cogs/weather.py | 4 +++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cogs/osu.py b/cogs/osu.py index 6ed8cef8..81fab535 100644 --- a/cogs/osu.py +++ b/cogs/osu.py @@ -3,6 +3,7 @@ # Imports import discord import os +from api import auth from ossapi import * from discord import option, ApplicationContext from discord.ext import commands @@ -11,8 +12,8 @@ class Osu(commands.Cog): def __init__(self, bot): self.bot = bot - self.api = OssapiV2(13110, os.environ['ossapi_CLIENT_SECRET']) - + self.api = OssapiV2(13110, auth.ext_token("ossapi")) + @commands.slash_command( name="osu_user", description="View information on an osu! player." diff --git a/cogs/reddit.py b/cogs/reddit.py index 0f7bb335..5cd15a6a 100644 --- a/cogs/reddit.py +++ b/cogs/reddit.py @@ -4,19 +4,20 @@ import discord import praw import os +from api import auth from discord import ApplicationContext, option from discord.ext import commands from random import randint # Variables color = discord.Color.random() -reddit = praw.Reddit(client_id='_pazwWZHi9JldA', client_secret=os.environ['reddit_CLIENT_SECRET'], user_agent='idk', check_for_async=False) +reddit = praw.Reddit(client_id='_pazwWZHi9JldA', client_secret=auth.ext_token('reddit'), user_agent='idk', check_for_async=False) # Commands class RedditMedia(commands.Cog): def __init__(self, bot): self.bot = bot - + @commands.slash_command( name='memes', description='Finely hand-picks a high-quality meme from the depths of reddit.' diff --git a/cogs/utils.py b/cogs/utils.py index cf295511..794a9832 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -6,6 +6,7 @@ import psutil import openai import discord +from api import auth from framework.isobot import currency, embedengine, commands as cmds from framework.isobot.db import levelling from discord import option, ApplicationContext @@ -17,7 +18,8 @@ color = discord.Color.random() currency = currency.CurrencyAPI("database/currency.json", "logs/currency.log") levelling = levelling.Levelling() -openai.api_key = os.getenv("chatgpt_API_KEY") +# openai.api_key = os.getenv("chatgpt_API_KEY") +openai.api_key = auth.ext_token('chatgpt') chatgpt_conversation = dict() _presence = Presence() diff --git a/cogs/weather.py b/cogs/weather.py index 8ca3c075..3ea92f93 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -3,12 +3,14 @@ import json import requests import os +from api import auth from framework.isobot.db import weather from discord import ApplicationContext, option from discord.ext import commands # Variables -api_key = os.environ['openweathermap_API_KEY'] +#api_key = os.environ['openweathermap_API_KEY'] +api_key = auth.ext_token('openweathermap') weather = weather.Weather() # Commands