edditing code

This commit is contained in:
2022-08-21 20:59:53 +03:00
parent 1011e287c9
commit 5a50b2ac98
5 changed files with 182 additions and 64 deletions

View File

@@ -1,11 +1,28 @@
import logging
from os import path, makedirs, rename, remove
import tempfile
from os import path, makedirs, rename, remove, walk
from random import random
import disnake
import inter as inter
from disnake import FFmpegPCMAudio
from enum import Enum
from disnake.ext import commands
import lib
# Files = commands.option_enum(lib.Commands.list_files(inter.id))
def Files(str, fold, Enum) -> None:
fl = []
for filenames in walk(fold):
fl.extend(filenames)
break
files = {}
for x in fl[2]:
files[x] = x
class Audio(commands.Cog):
def __init__(self, bot):
self.bot = bot # defining bot as global var in class
@@ -14,17 +31,59 @@ class Audio(commands.Cog):
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.Cog.listener()
async def on_voice_state_update(self, member, before, after):
role = await lib.Commands.read_json(member.guild.id, 'tigger_role')
audio = await lib.DB.read_db(member.guild.id, member.id, 'usertracks')
audio_files = await lib.Commands.list_files(f'audio/{member.id}')
logging.info(f'Func check audio state:\n'
f'\t\t\t\t\tUser: {member}\n'
f'\t\t\t\t\tTrigger role: {role}\n'
f'\t\t\t\t\tAudio list: {audio}\n'
f'\t\t\t\t\tAudio file list: {audio_files}\n'
f'\t\t\t\t\t-----------------')
if before.channel is None and role in member.roles:
pass
# track = random.randint(0, len(f) - 1)
# audio_source = FFmpegPCMAudio(f'audio/{f[track]}')
# logging.error(f'{track}\t\t{f[track]}')
# if not 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()
# if before.channel is None and member.id == _memb:
# track = random.randint(0, len(f) - 1)
# audio_source = FFmpegPCMAudio(f'audio/{_memb}/bear2_enemy_scav3.wav')
# logging.error(f'{track}\t\t\t{f[track]}')
# if not 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()
# @commands.slash_command(name="select_audio")
# async def select_audio(self, inter: disnake.ApplicationCommandInteraction,
# files: Files):
# pass
@commands.command(name="upload_audio")
async def upload_audio(self, ctx, user=None):
user = user or ctx.author
print(tempfile.tempdir)
if ctx.author.guild_permissions.administrator or user is ctx.author:
if ctx.message.attachments:
from os import error
if not path.isdir(f'tmp/{user.id}'):
try:
makedirs(f'tmp/{user.id}')
except error as _error:
logging.info(f"Failed to create dir", _error)
if not path.isdir(f'audio/{user.id}'):
try:
makedirs(f'audio/{user.id}')
@@ -33,28 +92,28 @@ class Audio(commands.Cog):
for at in ctx.message.attachments:
import mimetypes
await at.save(f'tmp/{user.id}/{at.filename}')
guess = mimetypes.guess_type(f'tmp/{user.id}/{at.filename}')
await ctx.reply(f'it`s {guess}')
await at.save(f'{tempfile.tempdir}/{tempfile.mkdtemp(dir=f"tmp/{user.id}")}/{at.filename}')
guess = mimetypes.guess_type(f'{tempfile.tempdir}/{user.id}/{at.filename}')
if guess[0].split('/')[0] == 'audio':
from pymediainfo import MediaInfo
file = f'tmp/{user.id}/{at.filename}'
file = f'{tempfile.tempdir}/{user.id}/{at.filename}'
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
if duration > 15:
await ctx.reply(f'Audio duration is {duration}, but max is 15')
remove(f'tmp/{user.id}/{at.filename}')
remove(f'{tempfile.tempdir}/{user.id}/{at.filename}')
else:
a = lib.DB.read_db(ctx.guild.id, user.id)
if a is None:
audiolist = f'{user.id}/{at.filename}'
a = await lib.DB.read_db(ctx.guild.id, user.id, 'usertracks')
if a:
audiolist = a + ", " + f'{at.filename}'
else:
audiolist = lib.DB.read_db(ctx.guild.id, user.id) + ", " + f'{user.id}/{at.filename}'
audiolist = f'{at.filename}'
lib.DB.add_audio(ctx.guild.id, user.id, audiolist)
rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}')
await lib.DB.check_exist_audio(ctx, ctx.guild.id, user.id, 'usertracks', at.filename)
await lib.DB.add_audio(ctx.guild.id, user.id, audiolist)
rename(f'{tempfile.tempdir}/{user.id}/{at.filename}', f'audio/{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}')
remove(f'{tempfile.tempdir}/{user.id}/{at.filename}')
else:
await ctx.reply("Has no Attachment")
else: