Update in db exist checker
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import random
|
||||
from os import path, makedirs, rename, remove
|
||||
from typing import List
|
||||
|
||||
import disnake
|
||||
from disnake import OptionChoice, OptionType, Option
|
||||
from disnake.ext import commands
|
||||
|
||||
from lib import logger
|
||||
from lib import logger, ListGenerator
|
||||
from lib import determine_time
|
||||
from lib import read_db, check_exist_audio, add_audio
|
||||
from lib import play_audio
|
||||
|
||||
|
||||
# todo: write chose audio from list by slash command
|
||||
# todo: write set audio from list by slash command
|
||||
class Audio(commands.Cog, name='Audio'):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
@@ -41,8 +44,8 @@ class Audio(commands.Cog, name='Audio'):
|
||||
def_audio_ls = await list_files()
|
||||
|
||||
if def_audio_db or audio_db is not None:
|
||||
if not def_audio_db: def_audio_db = []
|
||||
if not audio_db: audio_db = []
|
||||
def_audio_db = [] if not def_audio_db else def_audio_db
|
||||
audio_db = [] if not audio_db else audio_db
|
||||
logger.info(f'Play audio from DB')
|
||||
full_audio = def_audio_db + audio_db
|
||||
await play_audio(f'audio/{random.choice(full_audio)}', self.bot, after.channel)
|
||||
@@ -82,7 +85,6 @@ class Audio(commands.Cog, name='Audio'):
|
||||
file = f'tmp/{user.id}/{at.filename}'
|
||||
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
|
||||
max_duration = int(determine_time(ctx))
|
||||
print(type(max_duration))
|
||||
if duration > max_duration:
|
||||
await ctx.reply(f'Audio duration is {duration}, but max is {max_duration}')
|
||||
remove(f'tmp/{user.id}/{at.filename}')
|
||||
@@ -93,9 +95,13 @@ class Audio(commands.Cog, name='Audio'):
|
||||
else:
|
||||
audiolist = f'{at.filename}'
|
||||
|
||||
await check_exist_audio(ctx, ctx.guild.id, user.id, 'usertracks', at.filename)
|
||||
await add_audio(ctx.guild.id, user.id, audiolist)
|
||||
rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}')
|
||||
if not await check_exist_audio(ctx.guild.id, user.id, at.filename):
|
||||
rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}')
|
||||
await add_audio(ctx.guild.id, user.id, audiolist)
|
||||
await ctx.reply(f'Audio {at.filename.split(".")[0]} added to db')
|
||||
else:
|
||||
await ctx.reply(f'Audio {at.filename.split(".")[0]} already in db')
|
||||
remove(f'tmp/{user.id}/{at.filename}')
|
||||
elif guess[0].split('/')[0] != 'audio':
|
||||
await ctx.reply(f'It not audio {at.filename}\n it`s {guess[0]}')
|
||||
remove(f'tmp/{user.id}/{at.filename}')
|
||||
@@ -104,6 +110,50 @@ class Audio(commands.Cog, name='Audio'):
|
||||
else:
|
||||
await ctx.reply(f'You`re not admin. You can add audio only for your own account')
|
||||
|
||||
@commands.slash_command(name="play_audio",
|
||||
description="Make possible playing audio by command",
|
||||
options=[
|
||||
Option(name="audio",
|
||||
type=OptionType.string,
|
||||
required=True
|
||||
)
|
||||
])
|
||||
async def playaudio(self, inter: disnake.ApplicationCommandInteraction,
|
||||
audio: str
|
||||
):
|
||||
if inter.author.voice is not None:
|
||||
await inter.response.send_message(f'Played {audio}', ephemeral=True)
|
||||
await play_audio(audio, self.bot, inter.author.voice.channel)
|
||||
else:
|
||||
await inter.response.send_message('You`re not in voice', ephemeral=True)
|
||||
|
||||
@playaudio.autocomplete('audio')
|
||||
async def list_to_play(self, inter: disnake.ApplicationCommandInteraction, current: str) -> List[OptionChoice]:
|
||||
_def_iter = ListGenerator('audio')
|
||||
_def_dict: dict = {}
|
||||
for f in _def_iter:
|
||||
_def_dict[f.name] = f'{f.path}/{f.name}'
|
||||
|
||||
_user_dict: dict = {}
|
||||
try:
|
||||
_user_iter = ListGenerator(f'audio/{inter.author.id}')
|
||||
for f in _user_iter:
|
||||
_user_dict[f.name] = f'{f.path}/{f.name}'
|
||||
|
||||
# user_dict = []
|
||||
# for _track in user_list:
|
||||
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
_dict = {}
|
||||
_dict.update(_def_dict)
|
||||
_dict.update(_user_dict)
|
||||
return [
|
||||
OptionChoice(name=choice, value=f'{_dict[choice]}')
|
||||
for choice in _dict if current.lower() in choice.lower()
|
||||
]
|
||||
|
||||
|
||||
def setup(bot): # an extension must have a setup function
|
||||
bot.add_cog(Audio(bot)) # adding a cog
|
||||
|
||||
Reference in New Issue
Block a user