edditing code

This commit is contained in:
2022-08-22 21:50:15 +03:00
parent 89707a7e9a
commit 3e7a812fdc
5 changed files with 64 additions and 29 deletions

View File

@@ -1,13 +1,12 @@
import logging
import tempfile
from asyncio import sleep
from os import path, makedirs, rename, remove
from random import randrange
import random
from disnake import FFmpegPCMAudio
from disnake.ext import commands
from lib.DB import read_db, check_exist_audio, add_audio
from lib.Player import play_audio
class Audio(commands.Cog):
@@ -22,24 +21,29 @@ class Audio(commands.Cog):
async def on_voice_state_update(self, member, before, after):
from lib.Comands import read_json
role = await read_json(member.guild.id, 'tigger_role')
audio = await read_db(member.guild.id, member.id, 'usertracks')
# 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]}'
print(audio_db)
if def_audio_db is not None:
def_audio_db = def_audio_db.split(', ')
from lib.Comands import list_files
audio_files = await list_files(f'audio/{member.id}')
logging.info(f"state changed to user {member}")
f = await list_files()
if role is not None and before.channel is None and role in member.roles:
track = randrange(0, len(f) - 1, 1)
audio_source = FFmpegPCMAudio(f'audio/{f[track]}')
def_audio_ls = await list_files()
if before.channel is None and not member.bot:
full_audio = []
if def_audio_db or audio_db is not None:
full_audio = def_audio_db + audio_db
elif role in member.roles:
full_audio = def_audio_ls
print(full_audio)
audio = random.choice(full_audio)
print(audio)
if not self.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()
await play_audio(audio, self.bot, after.channel)
@commands.command(name="upload_audio")
async def upload_audio(self, ctx, user=None):
@@ -62,7 +66,7 @@ class Audio(commands.Cog):
from pymediainfo import MediaInfo
file = f'{tempfile.tempdir}/{user.id}/{at.filename}'
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
if duration > 15:
if duration > 20:
await ctx.reply(f'Audio duration is {duration}, but max is 15')
remove(f'{tempfile.tempdir}/{user.id}/{at.filename}')
else:

View File

@@ -2,6 +2,8 @@ from os import listdir
"""
Loads, unloads Cogs files
cog_list: return list of cog filenames
"""
@@ -13,13 +15,6 @@ def cog_list():
return cogs_list
async def cogs_dict():
cog_dict = {}
for _cog in cog_list():
cog_dict.update({f'{_cog}': f'{_cog}'})
return cog_dict
async def work_with_cogs(what_do, bot):
for _filename in cog_list():
if what_do == "load":

View File

@@ -1,5 +1,9 @@
import json
from os import walk
"""
Some prepare for commands
"""
async def list_files(fold: str = 'audio'):
@@ -54,13 +58,14 @@ async def write_json(guild: int, param_name: str, param: str or int):
json.dump(_json, f, indent=4)
async def determine_prefix(bot, msg):
def determine_prefix(bot, msg):
"""
Determite per-server bot prefix
:param bot: Disnake Bot object
:param msg: Disnake msg object
:return: prefix for server, default is $
"""
parameter = '$'
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
try:
from json import load
@@ -69,6 +74,7 @@ async def determine_prefix(bot, msg):
_json = {}
try:
parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up
except:
parameter = '$'
pass
return parameter

14
lib/Player.py Normal file
View File

@@ -0,0 +1,14 @@
from asyncio import sleep
from disnake import FFmpegPCMAudio
async def play_audio(audio, bot, vc):
if not bot.voice_clients:
await sleep(1)
vp = await vc.connect()
if not vp.is_playing():
vp.play(FFmpegPCMAudio(f'audio/{audio}'), after=None)
while vp.is_playing():
await sleep(0.5)
await sleep(1)
await vp.disconnect()

16
test2.py Normal file
View File

@@ -0,0 +1,16 @@
default_db = None
user = None
# default_db = [1, 2, 3]
# user = [4, 5, 6]
default = [1, 4, 8, 9, 10]
if default_db or user:
default_db = [] if default_db is None
user = [] if user is None
full = default_db + user
else:
full = default
print(full)