From 3e7a812fdc4e1a3bdfad2008ea98c1deefaaf577 Mon Sep 17 00:00:00 2001 From: beacon Date: Mon, 22 Aug 2022 21:50:15 +0300 Subject: [PATCH] edditing code --- cogs/audio.py | 44 ++++++++++++++++++++++++-------------------- lib/CogsPrepare.py | 9 ++------- lib/Comands.py | 10 ++++++++-- lib/Player.py | 14 ++++++++++++++ test2.py | 16 ++++++++++++++++ 5 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 lib/Player.py create mode 100644 test2.py diff --git a/cogs/audio.py b/cogs/audio.py index 1419ab3..c6e6b94 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -1,13 +1,12 @@ import logging import tempfile -from asyncio import sleep from os import path, makedirs, rename, remove -from random import randrange +import random -from disnake import FFmpegPCMAudio from disnake.ext import commands from lib.DB import read_db, check_exist_audio, add_audio +from lib.Player import play_audio class Audio(commands.Cog): @@ -22,24 +21,29 @@ class Audio(commands.Cog): async def on_voice_state_update(self, member, before, after): from lib.Comands import read_json role = await read_json(member.guild.id, 'tigger_role') - audio = await read_db(member.guild.id, member.id, 'usertracks') + # Read audio from DB + audio_db = await read_db(member.guild.id, member.id, 'usertracks') + def_audio_db = await read_db(member.guild.id, member.id, 'defaulttracks') + if audio_db is not None: + audio_db = audio_db.split(', ') # Need to fix creating list + for i in range(len(audio_db)): + audio_db[i] = f'{member.id}/{audio_db[i]}' + print(audio_db) + if def_audio_db is not None: + def_audio_db = def_audio_db.split(', ') from lib.Comands import list_files - audio_files = await list_files(f'audio/{member.id}') - logging.info(f"state changed to user {member}") - f = await list_files() - if role is not None and before.channel is None and role in member.roles: - track = randrange(0, len(f) - 1, 1) - audio_source = FFmpegPCMAudio(f'audio/{f[track]}') + def_audio_ls = await list_files() + if before.channel is None and not member.bot: + full_audio = [] + if def_audio_db or audio_db is not None: + full_audio = def_audio_db + audio_db + elif role in member.roles: + full_audio = def_audio_ls + print(full_audio) + audio = random.choice(full_audio) + print(audio) if not self.bot.voice_clients: - await sleep(1) - _channel = after.channel - vc = await after.channel.connect() - if not vc.is_playing(): - vc.play(audio_source, after=None) - while vc.is_playing(): - await sleep(0.5) - await sleep(1) - await vc.disconnect() + await play_audio(audio, self.bot, after.channel) @commands.command(name="upload_audio") async def upload_audio(self, ctx, user=None): @@ -62,7 +66,7 @@ class Audio(commands.Cog): from pymediainfo import MediaInfo file = f'{tempfile.tempdir}/{user.id}/{at.filename}' duration = round(MediaInfo.parse(file).tracks[0].duration / 1000) - if duration > 15: + if duration > 20: await ctx.reply(f'Audio duration is {duration}, but max is 15') remove(f'{tempfile.tempdir}/{user.id}/{at.filename}') else: diff --git a/lib/CogsPrepare.py b/lib/CogsPrepare.py index d7524bd..3b56e03 100644 --- a/lib/CogsPrepare.py +++ b/lib/CogsPrepare.py @@ -2,6 +2,8 @@ from os import listdir """ Loads, unloads Cogs files +cog_list: return list of cog filenames + """ @@ -13,13 +15,6 @@ def cog_list(): return cogs_list -async def cogs_dict(): - cog_dict = {} - for _cog in cog_list(): - cog_dict.update({f'{_cog}': f'{_cog}'}) - return cog_dict - - async def work_with_cogs(what_do, bot): for _filename in cog_list(): if what_do == "load": diff --git a/lib/Comands.py b/lib/Comands.py index e2764a0..cdd04f4 100644 --- a/lib/Comands.py +++ b/lib/Comands.py @@ -1,5 +1,9 @@ import json from os import walk +""" +Some prepare for commands + +""" async def list_files(fold: str = 'audio'): @@ -54,13 +58,14 @@ async def write_json(guild: int, param_name: str, param: str or int): json.dump(_json, f, indent=4) -async def determine_prefix(bot, msg): +def determine_prefix(bot, msg): """ Determite per-server bot prefix :param bot: Disnake Bot object :param msg: Disnake msg object :return: prefix for server, default is $ """ + parameter = '$' with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON try: from json import load @@ -69,6 +74,7 @@ async def determine_prefix(bot, msg): _json = {} try: parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up + except: - parameter = '$' + pass return parameter diff --git a/lib/Player.py b/lib/Player.py new file mode 100644 index 0000000..dfb1265 --- /dev/null +++ b/lib/Player.py @@ -0,0 +1,14 @@ +from asyncio import sleep +from disnake import FFmpegPCMAudio + + +async def play_audio(audio, bot, vc): + if not bot.voice_clients: + await sleep(1) + vp = await vc.connect() + if not vp.is_playing(): + vp.play(FFmpegPCMAudio(f'audio/{audio}'), after=None) + while vp.is_playing(): + await sleep(0.5) + await sleep(1) + await vp.disconnect() diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..f30da69 --- /dev/null +++ b/test2.py @@ -0,0 +1,16 @@ +default_db = None +user = None + +# default_db = [1, 2, 3] +# user = [4, 5, 6] +default = [1, 4, 8, 9, 10] + + + +if default_db or user: + default_db = [] if default_db is None + user = [] if user is None + full = default_db + user +else: + full = default +print(full)