aded pathlib and try-catch if users.json doesnt exist at runtime.

This commit is contained in:
2025-08-16 20:29:41 -04:00
parent 87a0a78a9c
commit f26222b4e7

View File

@@ -9,17 +9,24 @@ discord-py-interactions 5.11.0 has new option
import interactions import interactions
from os import getenv from os import getenv
from pathlib import Path
import yt_dlp import yt_dlp
import json import json
import asyncio import asyncio
import threading import threading
user_file = '/config/users.json' userFile = Path('/config/users.json')
bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904) bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904)
with open(user_file, 'r') as f:
userFile.parent.mkdir(exist_ok=True, parents=True)
try:
with open(userFile, 'x') as f:
print(f'users.json not found; saving to {userFile}')
except FileExistsError:
authorized_users = json.load(f).get('authorized_users') authorized_users = json.load(f).get('authorized_users')
print(f'authorized_users:{authorized_users}') print(f'authorized_users:{authorized_users}')
title = '' title = ''
async def send_message(ctx, message): async def send_message(ctx, message):
@@ -68,7 +75,7 @@ async def youtube(ctx: interactions.SlashContext, url:str):
'writeinfojson':False, 'writeinfojson':False,
'allow_playlist_files':True, 'allow_playlist_files':True,
'noplaylist':True, 'noplaylist':True,
'download_archive':'/config/archive.txt', 'download_archive':'./config/archive.txt',
'progress_hooks':[hook], 'progress_hooks':[hook],
'outtmpl': '%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s', 'outtmpl': '%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s',
'outtmpl_na_placeholder':'', 'outtmpl_na_placeholder':'',
@@ -114,7 +121,7 @@ async def _interrupt(ctx):
async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.USER): async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
if str(user.id) not in authorized_users: if str(user.id) not in authorized_users:
authorized_users.append(str(user.id)) authorized_users.append(str(user.id))
with open(user_file,'w') as f: #overwrite file - fix later if other params come up with open(userFile,'w') as f: #overwrite file - fix later if other params come up
json.dump({'authorized_users':authorized_users}) json.dump({'authorized_users':authorized_users})
print('react:checkmark') print('react:checkmark')
await ctx.message.add_reaction('') await ctx.message.add_reaction('')