v0.0.6\nplay_audio fixes
This commit is contained in:
192
cogs/audio.py
192
cogs/audio.py
@@ -1,109 +1,83 @@
|
||||
import random
|
||||
from os import path, makedirs, rename, remove
|
||||
|
||||
from disnake.ext import commands
|
||||
|
||||
from lib import logger
|
||||
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
|
||||
class Audio(commands.Cog, name='Audio'):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
logger.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 before.channel is None and not member.bot:
|
||||
if any('Escape from Tarkov' in str(user.activity) for user in after.channel.members):
|
||||
logger.info('Skip playing by Game')
|
||||
else:
|
||||
# Prepare list of audio
|
||||
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 not def_audio_db: def_audio_db = []
|
||||
if not audio_db: 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)
|
||||
elif len(member.roles) == 1 or _role is None:
|
||||
logger.info(f'Skip playing by role')
|
||||
elif any(str(role.id) in _role for role in member.roles):
|
||||
logger.info(f'Play audio from list by role')
|
||||
await play_audio(f'audio/{random.choice(def_audio_ls)}', self.bot, after.channel)
|
||||
else:
|
||||
logger.info(f'Skip playing by any else')
|
||||
|
||||
@commands.command(name="upload_audio",
|
||||
description=f"Add audio to bot")
|
||||
async def upload_audio(self, ctx, user=None):
|
||||
user = user or ctx.author
|
||||
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:
|
||||
pass
|
||||
|
||||
if not path.isdir(f'audio/{user.id}'):
|
||||
try:
|
||||
makedirs(f'audio/{user.id}')
|
||||
except error as _error:
|
||||
pass
|
||||
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}')
|
||||
if guess[0].split('/')[0] == 'audio':
|
||||
from pymediainfo import MediaInfo
|
||||
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}')
|
||||
else:
|
||||
a = await read_db(ctx.guild.id, user.id, 'usertracks')
|
||||
if a:
|
||||
audiolist = a + ", " + f'{at.filename}'
|
||||
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}')
|
||||
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}')
|
||||
else:
|
||||
await ctx.reply("Has no Attachment")
|
||||
else:
|
||||
await ctx.reply(f'You`re not admin. You can add audio only for your own account')
|
||||
|
||||
|
||||
def setup(bot): # an extension must have a setup function
|
||||
bot.add_cog(Audio(bot)) # adding a cog
|
||||
import random
|
||||
from itertools import islice
|
||||
from typing import List
|
||||
|
||||
import disnake
|
||||
from disnake import OptionChoice, Option, OptionType
|
||||
from disnake.ext import commands
|
||||
|
||||
from lib import logger, ListGenerator
|
||||
from lib import read_db
|
||||
from lib import play_audio
|
||||
|
||||
|
||||
# todo: write chose audio from list by slash command
|
||||
class Audio(commands.Cog, name='Audio'):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
logger.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 before.channel is None and not member.bot:
|
||||
if any('Escape from Tarkov' in str(user.activity) for user in after.channel.members):
|
||||
logger.info('Skip playing by Game')
|
||||
else:
|
||||
# Prepare list of audio
|
||||
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, 'defaulttracks')
|
||||
|
||||
if audio_db is not None:
|
||||
audio = audio_db.split(', ')
|
||||
else:
|
||||
from lib.Comands import list_files
|
||||
audio = await list_files()
|
||||
|
||||
if audio_db is not None:
|
||||
logger.info(f'Play audio from DB')
|
||||
await play_audio(f'audio/{random.choice(audio)}', self.bot, after.channel)
|
||||
elif len(member.roles) == 1 or _role is None:
|
||||
logger.info(f'Skip playing by role')
|
||||
elif any(str(role.id) in _role for role in member.roles):
|
||||
logger.info(f'Play audio from list by role')
|
||||
await play_audio(f'audio/{random.choice(audio)}', self.bot, after.channel)
|
||||
else:
|
||||
logger.info(f'Skip playing by any else')
|
||||
|
||||
@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):
|
||||
current = current.lower()
|
||||
_dict: dict = {}
|
||||
for f in ListGenerator('audio'):
|
||||
_dict[f.name] = f'{f.path}/{f.name}'
|
||||
return [
|
||||
OptionChoice(name=choice, value=f'{_dict[choice]}')
|
||||
for choice in _dict if current 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