change scope of tokens to global constants, obtained from env vars

This commit is contained in:
Enzo Nunes
2025-12-02 20:02:35 +00:00
parent 6c7eafb8d3
commit cbcd90732d

15
main.py
View File

@@ -1,5 +1,5 @@
import os
from asyncio import Condition, Lock, create_task, shield, to_thread, wait_for from asyncio import Condition, Lock, create_task, shield, to_thread, wait_for
from os import getenv
import requests import requests
from discord import Game, Intents, Interaction, app_commands from discord import Game, Intents, Interaction, app_commands
@@ -8,6 +8,8 @@ from discord.ext import commands
from PICable import PICable from PICable import PICable
MAX_STORAGE_KB = 50 * 1024 * 1024 # 50GB MAX_STORAGE_KB = 50 * 1024 * 1024 # 50GB
DISCORD_TOKEN = os.getenv("PICABLE_DISCORD_TOKEN")
GITHUB_TOKEN = os.getenv("PICABLE_GITHUB_TOKEN")
if __name__ == "__main__": if __name__ == "__main__":
current_storage_kb = 0 current_storage_kb = 0
@@ -15,17 +17,14 @@ if __name__ == "__main__":
current_repositories = set() current_repositories = set()
current_repositories_lock = Lock() current_repositories_lock = Lock()
discord_token = getenv("PICABLE_DISCORD_TOKEN") if not DISCORD_TOKEN or not GITHUB_TOKEN:
github_token = getenv("PICABLE_GITHUB_TOKEN")
if not discord_token or not github_token:
print("Discord or Github tokens not set in env. Use variables PICABLE_DISCORD_TOKEN and PICABLE_GITHUB_TOKEN") print("Discord or Github tokens not set in env. Use variables PICABLE_DISCORD_TOKEN and PICABLE_GITHUB_TOKEN")
exit(1) exit(1)
intents = Intents.default() intents = Intents.default()
intents.message_content = True intents.message_content = True
client = commands.Bot(command_prefix="/", intents=intents) client = commands.Bot(command_prefix="/", intents=intents)
client.run(discord_token) client.run(DISCORD_TOKEN)
@client.event @client.event
async def on_ready(): async def on_ready():
@@ -67,7 +66,7 @@ if __name__ == "__main__":
current_repositories.add(repository_full_name) current_repositories.add(repository_full_name)
url = f"https://api.github.com/repos/{owner}/{repository}" url = f"https://api.github.com/repos/{owner}/{repository}"
headers = {"Authorization": f"token {github_token}"} headers = {"Authorization": f"token {GITHUB_TOKEN}"}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if not response.status_code == 200: if not response.status_code == 200:
@@ -90,7 +89,7 @@ if __name__ == "__main__":
global current_storage_kb global current_storage_kb
try: try:
result_future = create_task(handle_picable(owner, repository, repository_size_kb, github_token)) result_future = create_task(handle_picable(owner, repository, repository_size_kb, GITHUB_TOKEN))
try: try:
result = await wait_for(shield(result_future), timeout=600) result = await wait_for(shield(result_future), timeout=600)
await interaction.followup.send(result) await interaction.followup.send(result)