added requirements.txt
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
import logging
|
||||
import random
|
||||
import tempfile
|
||||
from os import path, makedirs, rename, remove
|
||||
|
||||
from disnake.ext import commands
|
||||
|
||||
from lib.Comands import determine_time
|
||||
from lib.DB import read_db, check_exist_audio, add_audio
|
||||
from lib.Player import play_audio
|
||||
|
||||
|
||||
# todo: write chose audio from list by slash command
|
||||
class Audio(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
@@ -17,46 +18,44 @@ class Audio(commands.Cog):
|
||||
async def on_ready(self):
|
||||
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
|
||||
|
||||
# todo: complete check activity
|
||||
@commands.Cog.listener()
|
||||
async def on_voice_state_update(self, member, before, after):
|
||||
if any('Escape from Tarkov' in str(user.activity) for user in after.channel.members):
|
||||
logging.info('Skip playing by Game')
|
||||
else:
|
||||
from lib.Comands import read_json
|
||||
_role = await read_json(member.guild.id, 'tigger_role')
|
||||
# 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]}'
|
||||
if def_audio_db is not None:
|
||||
def_audio_db = def_audio_db.split(', ')
|
||||
from lib.Comands import list_files
|
||||
def_audio_ls = await list_files()
|
||||
if before.channel is None and not member.bot:
|
||||
if before.channel is None and not member.bot:
|
||||
if any('Escape from Tarkov' in str(user.activity) for user in after.channel.members):
|
||||
logging.info('Skip playing by Game')
|
||||
else:
|
||||
from lib.Comands import read_json
|
||||
_role = await read_json(member.guild.id, 'tigger_role')
|
||||
# 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]}'
|
||||
if def_audio_db is not None:
|
||||
def_audio_db = def_audio_db.split(', ')
|
||||
from lib.Comands import list_files
|
||||
def_audio_ls = await list_files()
|
||||
|
||||
if def_audio_db or audio_db is not None:
|
||||
if def_audio_db is None: def_audio_db = []
|
||||
if audio_db is None: audio_db = []
|
||||
logging.info(f'Play audio from DB')
|
||||
full_audio = def_audio_db + audio_db
|
||||
audio = random.choice(full_audio)
|
||||
await play_audio(audio, self.bot, after.channel)
|
||||
await play_audio(random.choice(full_audio), self.bot, after.channel)
|
||||
elif len(member.roles) == 1 or _role is None:
|
||||
logging.info(f'Skip playing')
|
||||
elif any(str(role.id) in _role for role in member.roles):
|
||||
logging.info(f'Play audio from list by role')
|
||||
audio = random.choice(def_audio_ls)
|
||||
await play_audio(audio, self.bot, after.channel)
|
||||
await play_audio(random.choice(def_audio_ls), self.bot, after.channel)
|
||||
else:
|
||||
logging.info(f'Skip playing')
|
||||
|
||||
|
||||
@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
|
||||
@@ -68,15 +67,16 @@ class Audio(commands.Cog):
|
||||
for at in ctx.message.attachments:
|
||||
import mimetypes
|
||||
|
||||
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}')
|
||||
await at.save(f'tmp/{user.id}")/{at.filename}')
|
||||
guess = mimetypes.guess_type(f'tmp/{user.id}")/{at.filename}')
|
||||
if guess[0].split('/')[0] == 'audio':
|
||||
from pymediainfo import MediaInfo
|
||||
file = f'{tempfile.tempdir}/{user.id}/{at.filename}'
|
||||
file = f'tmp/{user.id}")/{at.filename}'
|
||||
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
|
||||
if duration > 20:
|
||||
await ctx.reply(f'Audio duration is {duration}, but max is 15')
|
||||
remove(f'{tempfile.tempdir}/{user.id}/{at.filename}')
|
||||
max_duration = determine_time(ctx)
|
||||
if duration > max_duration:
|
||||
await ctx.reply(f'Audio duration is {duration}, but max is {max_duration}')
|
||||
remove(f'tmp/{user.id}")/{at.filename}')
|
||||
else:
|
||||
a = await read_db(ctx.guild.id, user.id, 'usertracks')
|
||||
if a:
|
||||
@@ -86,10 +86,10 @@ class Audio(commands.Cog):
|
||||
|
||||
await check_exist_audio(ctx, ctx.guild.id, user.id, 'usertracks', at.filename)
|
||||
await add_audio(ctx.guild.id, user.id, audiolist)
|
||||
rename(f'{tempfile.tempdir}/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}')
|
||||
rename(f'tmp/{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'{tempfile.tempdir}/{user.id}/{at.filename}')
|
||||
remove(f'tmp/{user.id}")/{at.filename}')
|
||||
else:
|
||||
await ctx.reply("Has no Attachment")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user