some code changes

This commit is contained in:
2022-08-31 18:28:07 +03:00
parent 4a33b35283
commit 5b134e6446
19 changed files with 288 additions and 145 deletions

View File

@@ -1,4 +1,3 @@
import logging
from asyncio import sleep
import disnake
@@ -6,8 +5,9 @@ from disnake import Option, OptionType
from disnake.ext import commands, tasks
from lib.Comands import read_json, write_json
from lib.DB import fill_bd, prepare_db, work_with_db
from lib import read_json, write_json
from lib import fill_bd, prepare_db, work_with_db
from lib import logger
class Admin(commands.Cog, name='Admin'):
@@ -22,7 +22,7 @@ class Admin(commands.Cog, name='Admin'):
await fill_bd(g.name, g.id, g.bot, g.nick, g.guild.id)
self.activity.start()
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
@tasks.loop(seconds=20)
async def activity(self):
@@ -63,7 +63,7 @@ class Admin(commands.Cog, name='Admin'):
role = disnake.utils.get(member.guild.roles, id=guest_role)
else:
role = disnake.utils.get(member.guild.roles, id=bot_role)
logging.info(f"Adding to {member} role {role}")
logger.info(f"Adding to {member} role {role}")
await member.add_roles(role)
@commands.slash_command(
@@ -137,7 +137,9 @@ class Admin(commands.Cog, name='Admin'):
Option("seconds", "specify max duration", OptionType.integer, required=True),
]
)
async def set_time(self, inter, seconds: commands.Range[5, 30]):
async def set_time(self,
inter: disnake.ApplicationCommandInteraction,
seconds: commands.Range[5, 30]):
await write_json(inter.guild.id, "seconds", seconds)
await inter.response.send_message(f"Change max audio duration to {seconds} sec", ephemeral=True)

View File

@@ -1,32 +1,30 @@
import logging
import random
from os import path, makedirs, rename, remove
from typing import Optional
import disnake
from disnake import Option, OptionType
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, get_audio_list
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):
class Audio(commands.Cog, name='Audio'):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
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):
print(type(member.roles))
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')
logger.info('Skip playing by Game')
else:
# Prepare list of audio
from lib.Comands import read_json
@@ -46,35 +44,30 @@ class Audio(commands.Cog):
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')
logger.info(f'Play audio from DB')
full_audio = def_audio_db + audio_db
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 by role')
logger.info(f'Skip playing by role')
elif any(str(role.id) in _role for role in member.roles):
logging.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)
else:
logging.info(f'Skip playing by any else')
logger.info(f'Skip playing by any else')
@commands.slash_command(name="play_audio",
description="Set channel whitch itterate with bot",
options=[
Option("audio", "select audio", OptionType.string, required=True),
]
)
async def play_audio(self,
integration: disnake.ApplicationCommandInteraction,
audio: Optional[str]
) -> None:
await get_audio_list(integration, audio)
@commands.command(name="upload_audio")
@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}')
@@ -83,16 +76,17 @@ 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 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}'
file = f'tmp/{user.id}/{at.filename}'
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
max_duration = determine_time(ctx)
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}')
remove(f'tmp/{user.id}/{at.filename}')
else:
a = await read_db(ctx.guild.id, user.id, 'usertracks')
if a:
@@ -102,10 +96,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'tmp/{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'tmp/{user.id}")/{at.filename}')
remove(f'tmp/{user.id}/{at.filename}')
else:
await ctx.reply("Has no Attachment")
else:

View File

@@ -1,6 +1,5 @@
import logging
from disnake.ext import commands
from lib import logger
class Fun(commands.Cog, name='Fun'):
@@ -9,7 +8,7 @@ class Fun(commands.Cog, name='Fun'):
@commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
def setup(bot): # an extension must have a setup function

View File

@@ -1,11 +1,10 @@
import logging
import disnake
from disnake import Option, OptionType, Colour
from disnake.ext import commands
from lib.Comands import write_json
from lib.DB import work_with_db, read_db
from lib import read_db
from lib import logger
class General(commands.Cog):
@@ -14,7 +13,7 @@ class General(commands.Cog):
@commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.slash_command(
name="info",

View File

@@ -1,11 +1,11 @@
import logging
import os
import disnake
import psutil
from disnake.ext import commands
from lib.Comands import determine_prefix, determine_time
from lib import determine_prefix, determine_time
from lib import logger
class Bot_info(commands.Cog, name='Bot Info'):
@@ -14,9 +14,10 @@ class Bot_info(commands.Cog, name='Bot Info'):
@commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.slash_command(name="info_bot") # this is for making a command
@commands.slash_command(name="info_bot",
description='Shows general info about bot') # this is for making a command
async def info_bot(self, inter):
_pid = os.getpid()
_process = psutil.Process(_pid)

56
cogs/test.py Normal file
View File

@@ -0,0 +1,56 @@
from typing import List
import disnake
from disnake import Option, OptionType, OptionChoice
from disnake.ext import commands
from lib import list_files
from lib import logger
from lib import play_audio
class Testing(commands.Cog, name='Testing'):
def __init__(self, bot):
self.bot = bot # defining bot as global var in class
@commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self):
logger.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.slash_command(name="play_audio",
options=[
Option(name="audio",
type=OptionType.string,
required=True
# choices=OptionChoice(list_to_play)
)
])
async def playaudio(self, inter: disnake.ApplicationCommandInteraction,
audio: str
):
if inter.author.voice is not None:
await inter.response.send_message(f'Played {audio}')
await play_audio(audio, self.bot, inter.author.voice.channel)
else:
await inter.response.send_message('You`re not in voice')
@playaudio.autocomplete('audio')
async def list_to_play(self, inter: disnake.ApplicationCommandInteraction, current: str) -> List[OptionChoice]:
def_list = await list_files()
try:
user_list = await list_files(str(inter.author.id))
# user_dict = []
# for _track in user_list:
except IndexError:
user_list = []
_list = def_list + user_list
return [
OptionChoice(name=choice, value=choice)
for choice in def_list if current.lower() in choice.lower()
]
def setup(bot): # an extension must have a setup function
bot.add_cog(Testing(bot)) # adding a cog