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
|
import interactions
|
||||||
from os import getenv
|
from os import getenv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
import json
|
import json
|
||||||
import asyncio
|
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)
|
||||||
print(f'users.json not found; saving to {userFile}')
|
|
||||||
except FileExistsError:
|
def load_authorized_users():
|
||||||
with open(userFile, 'r') as f:
|
if not userFile.exists():
|
||||||
authorized_users = json.load(f).get('authorized_users')
|
save_authorized_users([])
|
||||||
print(f'authorized_users:{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 = ''
|
title = ''
|
||||||
|
|
||||||
@@ -62,13 +82,12 @@ def create_hook(ctx,loop):
|
|||||||
required=True,
|
required=True,
|
||||||
description='url target'
|
description='url target'
|
||||||
)
|
)
|
||||||
async def youtube(ctx: interactions.SlashContext, url:str):
|
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',
|
||||||
'fragment_tries': 10,
|
'fragment_tries': 10,
|
||||||
'restrictfilenames':True,
|
'restrictfilenames':True,
|
||||||
@@ -81,12 +100,12 @@ async def youtube(ctx: interactions.SlashContext, url:str):
|
|||||||
'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':'',
|
||||||
}
|
}
|
||||||
# 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.')
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
await ctx.channel.send(f'Downloading from <{url}>. Status updates via DM.')
|
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',
|
description='enable this bot for target user',
|
||||||
)
|
)
|
||||||
@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(
|
||||||
@@ -136,15 +157,15 @@ async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.
|
|||||||
description='disable this bot for target user',
|
description='disable this bot for target user',
|
||||||
)
|
)
|
||||||
@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