load authorized_users.json robustly
This commit is contained in:
67
youdis.py
67
youdis.py
@@ -16,18 +16,38 @@ import asyncio
|
|||||||
import threading
|
import threading
|
||||||
|
|
||||||
userFile = Path('/config/users.json')
|
userFile = Path('/config/users.json')
|
||||||
userFile.touch(exist_ok=True)
|
userFile.parent.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904)
|
bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904)
|
||||||
|
|
||||||
userFile.parent.mkdir(exist_ok=True, parents=True)
|
def save_authorized_users(authorized_users):
|
||||||
try:
|
with open(userFile, 'w') as f:
|
||||||
with open(userFile, 'x') as f:
|
json.dump({'authorized_users': authorized_users}, f)
|
||||||
|
|
||||||
|
def load_authorized_users():
|
||||||
|
if not userFile.exists():
|
||||||
|
save_authorized_users([])
|
||||||
print(f'users.json not found; saving to {userFile}')
|
print(f'users.json not found; saving to {userFile}')
|
||||||
except FileExistsError:
|
return []
|
||||||
with open(userFile, 'r') as f:
|
|
||||||
authorized_users = json.load(f).get('authorized_users')
|
try:
|
||||||
|
with open(userFile, 'r') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
except (json.JSONDecodeError, OSError):
|
||||||
|
save_authorized_users([])
|
||||||
|
print(f'users.json invalid; resetting {userFile}')
|
||||||
|
return []
|
||||||
|
|
||||||
|
authorized_users = data.get('authorized_users', [])
|
||||||
|
if not isinstance(authorized_users, list):
|
||||||
|
authorized_users = []
|
||||||
|
|
||||||
|
authorized_users = [str(user_id) for user_id in authorized_users]
|
||||||
|
save_authorized_users(authorized_users)
|
||||||
print(f'authorized_users:{authorized_users}')
|
print(f'authorized_users:{authorized_users}')
|
||||||
|
return authorized_users
|
||||||
|
|
||||||
|
authorized_users = load_authorized_users()
|
||||||
|
|
||||||
title = ''
|
title = ''
|
||||||
|
|
||||||
@@ -66,7 +86,6 @@ async def youtube(ctx: interactions.SlashContext, url:str):
|
|||||||
print(f'{ctx.author.id} requested {url}')
|
print(f'{ctx.author.id} requested {url}')
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
hook = create_hook(ctx,loop)
|
hook = create_hook(ctx,loop)
|
||||||
msg = ''
|
|
||||||
# use api_to_cli and paste cli options to get the output you need
|
# use api_to_cli and paste cli options to get the output you need
|
||||||
yoptions = {
|
yoptions = {
|
||||||
'format':'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
'format':'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
||||||
@@ -83,7 +102,7 @@ async def youtube(ctx: interactions.SlashContext, url:str):
|
|||||||
'outtmpl_na_placeholder':'',
|
'outtmpl_na_placeholder':'',
|
||||||
}
|
}
|
||||||
# check that user is authorized
|
# check that user is authorized
|
||||||
if ctx.author.id not in authorized_users:
|
if str(ctx.author.id) not in authorized_users:
|
||||||
if ctx.author.id == 127831327012683776:
|
if ctx.author.id == 127831327012683776:
|
||||||
await ctx.author.send('potato stop')
|
await ctx.author.send('potato stop')
|
||||||
await ctx.author.send('you are not authorized to use this command. message my owner to be added.')
|
await ctx.author.send('you are not authorized to use this command. message my owner to be added.')
|
||||||
@@ -121,12 +140,14 @@ async def _interrupt(ctx):
|
|||||||
)
|
)
|
||||||
@interactions.check(interactions.is_owner())
|
@interactions.check(interactions.is_owner())
|
||||||
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:
|
user_id = str(user.id)
|
||||||
authorized_users.append(str(user.id))
|
if user_id not in authorized_users:
|
||||||
with open(userFile,'w') as f: #overwrite file - fix later if other params come up
|
authorized_users.append(user_id)
|
||||||
json.dump({'authorized_users':authorized_users})
|
save_authorized_users(authorized_users)
|
||||||
print('react:checkmark')
|
print(f'authorized {user_id}')
|
||||||
await ctx.message.add_reaction('✅')
|
await ctx.author.send(f'authorized {user.mention}')
|
||||||
|
else:
|
||||||
|
await ctx.author.send(f'{user.mention} is already authorized')
|
||||||
|
|
||||||
@interactions.slash_command(name="removeuser",description="deauthorize target user")
|
@interactions.slash_command(name="removeuser",description="deauthorize target user")
|
||||||
@interactions.slash_option(
|
@interactions.slash_option(
|
||||||
@@ -137,14 +158,14 @@ async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.
|
|||||||
)
|
)
|
||||||
@interactions.check(interactions.is_owner())
|
@interactions.check(interactions.is_owner())
|
||||||
async def _removeuser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
async def _removeuser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
||||||
if str(user.id) in authorized_users:
|
user_id = str(user.id)
|
||||||
# ? ? ? fix pls
|
if user_id in authorized_users:
|
||||||
i = index(authorized_users(str(user.id)))
|
authorized_users.remove(user_id)
|
||||||
|
save_authorized_users(authorized_users)
|
||||||
# update list, rewrite json
|
print(f'deauthorized {user_id}')
|
||||||
|
await ctx.author.send(f'deauthorized {user.mention}')
|
||||||
print('react:checkmark')
|
else:
|
||||||
await ctx.message.add_reaction('✅')
|
await ctx.author.send(f'{user.mention} is not currently authorized')
|
||||||
|
|
||||||
async def dl_hook(d):
|
async def dl_hook(d):
|
||||||
msg = f'{d["status"]} {d["filename"]}'
|
msg = f'{d["status"]} {d["filename"]}'
|
||||||
|
|||||||
Reference in New Issue
Block a user