Finished audio list generator
This commit is contained in:
@@ -21,7 +21,6 @@ class Audio(commands.Cog, name='Audio'):
|
|||||||
# todo: complete check activity
|
# todo: complete check activity
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_voice_state_update(self, member, before, after):
|
async def on_voice_state_update(self, member, before, after):
|
||||||
print(type(member.roles))
|
|
||||||
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):
|
if any('Escape from Tarkov' in str(user.activity) for user in after.channel.members):
|
||||||
logger.info('Skip playing by Game')
|
logger.info('Skip playing by Game')
|
||||||
@@ -46,12 +45,12 @@ class Audio(commands.Cog, name='Audio'):
|
|||||||
if audio_db is None: audio_db = []
|
if audio_db is None: audio_db = []
|
||||||
logger.info(f'Play audio from DB')
|
logger.info(f'Play audio from DB')
|
||||||
full_audio = def_audio_db + audio_db
|
full_audio = def_audio_db + audio_db
|
||||||
await play_audio(random.choice(full_audio), self.bot, after.channel)
|
await play_audio(f'audio/{random.choice(full_audio)}', self.bot, after.channel)
|
||||||
elif len(member.roles) == 1 or _role is None:
|
elif len(member.roles) == 1 or _role is None:
|
||||||
logger.info(f'Skip playing by role')
|
logger.info(f'Skip playing by role')
|
||||||
elif any(str(role.id) in _role for role in member.roles):
|
elif any(str(role.id) in _role for role in member.roles):
|
||||||
logger.info(f'Play audio from list by role')
|
logger.info(f'Play audio from list by role')
|
||||||
await play_audio(random.choice(def_audio_ls), self.bot, after.channel)
|
await play_audio(f'audio/{random.choice(def_audio_ls)}', self.bot, after.channel)
|
||||||
else:
|
else:
|
||||||
logger.info(f'Skip playing by any else')
|
logger.info(f'Skip playing by any else')
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class Bot_info(commands.Cog, name='Bot Info'):
|
|||||||
@commands.Cog.listener() # this is a decorator for events/listeners
|
@commands.Cog.listener() # this is a decorator for events/listeners
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
|
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
|
||||||
|
logger.info(f'{[names.name for names in self.bot.users]}')
|
||||||
|
|
||||||
@commands.slash_command(name="info_bot",
|
@commands.slash_command(name="info_bot",
|
||||||
description='Shows general info about bot') # this is for making a command
|
description='Shows general info about bot') # this is for making a command
|
||||||
|
|||||||
31
cogs/test.py
31
cogs/test.py
@@ -4,7 +4,7 @@ import disnake
|
|||||||
from disnake import Option, OptionType, OptionChoice
|
from disnake import Option, OptionType, OptionChoice
|
||||||
from disnake.ext import commands
|
from disnake.ext import commands
|
||||||
|
|
||||||
from lib import list_files
|
from lib import ListGenerator
|
||||||
from lib import logger
|
from lib import logger
|
||||||
from lib import play_audio
|
from lib import play_audio
|
||||||
|
|
||||||
@@ -18,37 +18,48 @@ class Testing(commands.Cog, name='Testing'):
|
|||||||
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
|
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
|
||||||
|
|
||||||
@commands.slash_command(name="play_audio",
|
@commands.slash_command(name="play_audio",
|
||||||
|
description="Make possible playing audio by command",
|
||||||
options=[
|
options=[
|
||||||
Option(name="audio",
|
Option(name="audio",
|
||||||
type=OptionType.string,
|
type=OptionType.string,
|
||||||
required=True
|
required=True
|
||||||
# choices=OptionChoice(list_to_play)
|
|
||||||
)
|
)
|
||||||
])
|
])
|
||||||
async def playaudio(self, inter: disnake.ApplicationCommandInteraction,
|
async def playaudio(self, inter: disnake.ApplicationCommandInteraction,
|
||||||
audio: str
|
audio: str
|
||||||
):
|
):
|
||||||
if inter.author.voice is not None:
|
if inter.author.voice is not None:
|
||||||
await inter.response.send_message(f'Played {audio}')
|
await inter.response.send_message(f'Played {audio}', ephemeral=True)
|
||||||
await play_audio(audio, self.bot, inter.author.voice.channel)
|
await play_audio(audio, self.bot, inter.author.voice.channel)
|
||||||
else:
|
else:
|
||||||
await inter.response.send_message('You`re not in voice')
|
await inter.response.send_message('You`re not in voice', ephemeral=True)
|
||||||
|
|
||||||
@playaudio.autocomplete('audio')
|
@playaudio.autocomplete('audio')
|
||||||
async def list_to_play(self, inter: disnake.ApplicationCommandInteraction, current: str) -> List[OptionChoice]:
|
async def list_to_play(self, inter: disnake.ApplicationCommandInteraction, current: str) -> List[OptionChoice]:
|
||||||
def_list = await list_files()
|
_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:
|
try:
|
||||||
user_list = await list_files(str(inter.author.id))
|
_user_iter = ListGenerator(f'audio/{inter.author.id}')
|
||||||
|
for f in _user_iter:
|
||||||
|
_user_dict[f.name] = f'{f.path}/{f.name}'
|
||||||
|
|
||||||
# user_dict = []
|
# user_dict = []
|
||||||
# for _track in user_list:
|
# for _track in user_list:
|
||||||
|
|
||||||
except IndexError:
|
except IndexError:
|
||||||
user_list = []
|
pass
|
||||||
|
|
||||||
_list = def_list + user_list
|
_dict = {}
|
||||||
|
_dict.update(_def_dict)
|
||||||
|
_dict.update(_user_dict)
|
||||||
return [
|
return [
|
||||||
OptionChoice(name=choice, value=choice)
|
OptionChoice(name=choice, value=f'{_dict[choice]}')
|
||||||
for choice in def_list if current.lower() in choice.lower()
|
for choice in _dict if current.lower() in choice.lower()
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,78 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
class User:
|
||||||
|
def __init__(self, userid: str = None):
|
||||||
|
self.userid = userid
|
||||||
|
self.list: list = self._lister(self.userid)
|
||||||
|
self._current_index = 0
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return (
|
||||||
|
f'<Audio file name={self.name} path={self.userid} type={self.type}>'
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return self.list[self._current_index]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self) -> str:
|
||||||
|
guess = mimetypes.guess_type(f'{self.path}/{self.name}')[0].split('/')[0]
|
||||||
|
return guess
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _lister(cls, path) -> list:
|
||||||
|
_dict: list = []
|
||||||
|
for _files in os.walk(path):
|
||||||
|
_dict.extend(_files)
|
||||||
|
break
|
||||||
|
return _dict[2]
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return _ListGenerationIter(self)
|
||||||
|
|
||||||
|
|
||||||
|
class _FileAttrs:
|
||||||
|
def __init__(self, name, path, type):
|
||||||
|
self.name = name
|
||||||
|
self.path = path
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return 'Audio Dict Generator'
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
|
||||||
|
|
||||||
|
|
||||||
|
class _ListGenerationIter:
|
||||||
|
def __init__(self, list_class):
|
||||||
|
self._list = list_class.list
|
||||||
|
self._name = list_class.name
|
||||||
|
self._type = list_class.type
|
||||||
|
self._path = list_class.path
|
||||||
|
|
||||||
|
self._size = len(self._list)
|
||||||
|
self._current_index = 0
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if self._current_index < self._size:
|
||||||
|
_name = self._list[self._current_index]
|
||||||
|
_path = self._path
|
||||||
|
_type = self._type
|
||||||
|
self._current_index += 1
|
||||||
|
memb = _FileAttrs(_name, _path, _type)
|
||||||
|
return memb
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
|
||||||
async def prepare_db(guild: int):
|
async def prepare_db(guild: int):
|
||||||
try:
|
try:
|
||||||
connect = sqlite3.connect('user.db')
|
connect = sqlite3.connect('user.db')
|
||||||
|
|||||||
@@ -2,76 +2,66 @@ import mimetypes
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class FileAttrs:
|
|
||||||
def __iter__(self, name, path, type):
|
|
||||||
self.name = name
|
|
||||||
self.path = path
|
|
||||||
self.type = type
|
|
||||||
|
|
||||||
@self.name.setter
|
|
||||||
def name(self, value):
|
|
||||||
self.name = value
|
|
||||||
|
|
||||||
@self.type.setter
|
|
||||||
def type(self, value):
|
|
||||||
self.type = value
|
|
||||||
|
|
||||||
@self.path.setter
|
|
||||||
def path(self, value):
|
|
||||||
self.path = value
|
|
||||||
|
|
||||||
class ListGenerator:
|
class ListGenerator:
|
||||||
def __init__(self, path=None):
|
def __init__(self, path: str = None):
|
||||||
self._path = path
|
self.path = path
|
||||||
self.list: list = self._lister(self._path)
|
self.list: list = self._lister(self.path)
|
||||||
self._current_index = 0
|
self._current_index = 0
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
|
if self.path:
|
||||||
return self.name
|
return self.name
|
||||||
|
else:
|
||||||
|
return 'Audio iter generator'
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
if self.path:
|
||||||
return (
|
return (
|
||||||
f'<Audio file name={self.name} path={self.path} type={self.type} size={self._size}>'
|
f'<Audio file name={self.name} path={self.path} type={self.type}>'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
f'<Audio file name={None} path={None} type={None}>'
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return self.list[self._current_index]
|
return self.list[self._current_index]
|
||||||
|
|
||||||
@property
|
|
||||||
def path(self) -> str:
|
|
||||||
return str(self._path)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self) -> str:
|
def type(self) -> str:
|
||||||
guess = mimetypes.guess_type(f'{self._path}/{self.name}')[0].split('/')[0]
|
guess = mimetypes.guess_type(f'{self.path}/{self.name}')[0].split('/')[0]
|
||||||
return guess
|
return guess
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _lister(cls, path) -> list:
|
def _lister(cls, path) -> list:
|
||||||
_dict: list = []
|
_dict: list = []
|
||||||
|
try:
|
||||||
for _files in os.walk(path):
|
for _files in os.walk(path):
|
||||||
_dict.extend(_files)
|
_dict.extend(_files)
|
||||||
break
|
break
|
||||||
return _dict[2]
|
return _dict[2]
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return ListGenerationIter(self)
|
return _ListGenerationIter(self)
|
||||||
|
|
||||||
@name.setter
|
|
||||||
def name(self, value):
|
|
||||||
self.name = value
|
|
||||||
|
|
||||||
@type.setter
|
|
||||||
def type(self, value):
|
|
||||||
self.type = value
|
|
||||||
|
|
||||||
@path.setter
|
|
||||||
def path(self, value):
|
|
||||||
self.path = value
|
|
||||||
|
|
||||||
|
|
||||||
class ListGenerationIter:
|
class _FileAttrs:
|
||||||
|
def __init__(self, name, path, type):
|
||||||
|
self.name = name
|
||||||
|
self.path = path
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
|
||||||
|
|
||||||
|
|
||||||
|
class _ListGenerationIter:
|
||||||
def __init__(self, list_class):
|
def __init__(self, list_class):
|
||||||
self._list = list_class.list
|
self._list = list_class.list
|
||||||
self._name = list_class.name
|
self._name = list_class.name
|
||||||
@@ -86,11 +76,10 @@ class ListGenerationIter:
|
|||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
if self._current_index < self._size:
|
if self._current_index < self._size:
|
||||||
|
|
||||||
_name = self._list[self._current_index]
|
_name = self._list[self._current_index]
|
||||||
_path = self._path
|
_path = self._path
|
||||||
_type = self._type
|
_type = self._type
|
||||||
self._current_index += 1
|
self._current_index += 1
|
||||||
memb = FileAttrs(_name, _path, _type)
|
memb = _FileAttrs(_name, _path, _type)
|
||||||
return memb
|
return memb
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class CustomFormatter(logging.Formatter):
|
class _CustomFormatter(logging.Formatter):
|
||||||
|
|
||||||
grey = "\x1b[38;20m"
|
grey = "\x1b[38;20m"
|
||||||
yellow = "\x1b[33;20m"
|
yellow = "\x1b[33;20m"
|
||||||
@@ -29,5 +29,5 @@ logger.setLevel(logging.DEBUG)
|
|||||||
|
|
||||||
ch = logging.StreamHandler()
|
ch = logging.StreamHandler()
|
||||||
ch.setLevel(logging.DEBUG)
|
ch.setLevel(logging.DEBUG)
|
||||||
ch.setFormatter(CustomFormatter())
|
ch.setFormatter(_CustomFormatter())
|
||||||
logger.addHandler(ch)
|
logger.addHandler(ch)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ async def play_audio(audio, bot, vc):
|
|||||||
await sleep(1)
|
await sleep(1)
|
||||||
vp = await vc.connect()
|
vp = await vc.connect()
|
||||||
if not vp.is_playing():
|
if not vp.is_playing():
|
||||||
vp.play(FFmpegPCMAudio(f'audio/{audio}'), after=None)
|
vp.play(FFmpegPCMAudio(f'{audio}'), after=None)
|
||||||
while vp.is_playing():
|
while vp.is_playing():
|
||||||
await sleep(0.5)
|
await sleep(0.5)
|
||||||
await sleep(1)
|
await sleep(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user