load authorized_users.json robustly
This commit is contained in:
115
youdis.py
115
youdis.py
@@ -10,24 +10,44 @@ discord-py-interactions 5.11.0 has new option
|
||||
import interactions
|
||||
from os import getenv
|
||||
from pathlib import Path
|
||||
import yt_dlp
|
||||
import json
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
userFile = Path('/config/users.json')
|
||||
userFile.touch(exist_ok=True)
|
||||
|
||||
bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904)
|
||||
|
||||
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:
|
||||
with open(userFile, 'r') as f:
|
||||
authorized_users = json.load(f).get('authorized_users')
|
||||
print(f'authorized_users:{authorized_users}')
|
||||
import yt_dlp
|
||||
import json
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
userFile = Path('/config/users.json')
|
||||
userFile.parent.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904)
|
||||
|
||||
def save_authorized_users(authorized_users):
|
||||
with open(userFile, 'w') 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}')
|
||||
return []
|
||||
|
||||
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}')
|
||||
return authorized_users
|
||||
|
||||
authorized_users = load_authorized_users()
|
||||
|
||||
title = ''
|
||||
|
||||
@@ -62,13 +82,12 @@ def create_hook(ctx,loop):
|
||||
required=True,
|
||||
description='url target'
|
||||
)
|
||||
async def youtube(ctx: interactions.SlashContext, url:str):
|
||||
print(f'{ctx.author.id} requested {url}')
|
||||
loop = asyncio.get_running_loop()
|
||||
hook = create_hook(ctx,loop)
|
||||
msg = ''
|
||||
# use api_to_cli and paste cli options to get the output you need
|
||||
yoptions = {
|
||||
async def youtube(ctx: interactions.SlashContext, url:str):
|
||||
print(f'{ctx.author.id} requested {url}')
|
||||
loop = asyncio.get_running_loop()
|
||||
hook = create_hook(ctx,loop)
|
||||
# use api_to_cli and paste cli options to get the output you need
|
||||
yoptions = {
|
||||
'format':'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
||||
'fragment_tries': 10,
|
||||
'restrictfilenames':True,
|
||||
@@ -81,12 +100,12 @@ async def youtube(ctx: interactions.SlashContext, url:str):
|
||||
'progress_hooks':[hook],
|
||||
'outtmpl': '%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s',
|
||||
'outtmpl_na_placeholder':'',
|
||||
}
|
||||
# check that user is authorized
|
||||
if ctx.author.id not in authorized_users:
|
||||
if ctx.author.id == 127831327012683776:
|
||||
await ctx.author.send('potato stop')
|
||||
await ctx.author.send('you are not authorized to use this command. message my owner to be added.')
|
||||
}
|
||||
# check that user is authorized
|
||||
if str(ctx.author.id) not in authorized_users:
|
||||
if ctx.author.id == 127831327012683776:
|
||||
await ctx.author.send('potato stop')
|
||||
await ctx.author.send('you are not authorized to use this command. message my owner to be added.')
|
||||
return
|
||||
else:
|
||||
await ctx.channel.send(f'Downloading from <{url}>. Status updates via DM.')
|
||||
@@ -120,13 +139,15 @@ async def _interrupt(ctx):
|
||||
description='enable this bot for target user',
|
||||
)
|
||||
@interactions.check(interactions.is_owner())
|
||||
async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
||||
if str(user.id) not in authorized_users:
|
||||
authorized_users.append(str(user.id))
|
||||
with open(userFile,'w') as f: #overwrite file - fix later if other params come up
|
||||
json.dump({'authorized_users':authorized_users})
|
||||
print('react:checkmark')
|
||||
await ctx.message.add_reaction('✅')
|
||||
async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
||||
user_id = str(user.id)
|
||||
if user_id not in authorized_users:
|
||||
authorized_users.append(user_id)
|
||||
save_authorized_users(authorized_users)
|
||||
print(f'authorized {user_id}')
|
||||
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_option(
|
||||
@@ -136,15 +157,15 @@ async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.
|
||||
description='disable this bot for target user',
|
||||
)
|
||||
@interactions.check(interactions.is_owner())
|
||||
async def _removeuser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
||||
if str(user.id) in authorized_users:
|
||||
# ? ? ? fix pls
|
||||
i = index(authorized_users(str(user.id)))
|
||||
|
||||
# update list, rewrite json
|
||||
|
||||
print('react:checkmark')
|
||||
await ctx.message.add_reaction('✅')
|
||||
async def _removeuser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
||||
user_id = str(user.id)
|
||||
if user_id in authorized_users:
|
||||
authorized_users.remove(user_id)
|
||||
save_authorized_users(authorized_users)
|
||||
print(f'deauthorized {user_id}')
|
||||
await ctx.author.send(f'deauthorized {user.mention}')
|
||||
else:
|
||||
await ctx.author.send(f'{user.mention} is not currently authorized')
|
||||
|
||||
async def dl_hook(d):
|
||||
msg = f'{d["status"]} {d["filename"]}'
|
||||
|
||||
Reference in New Issue
Block a user