refractor lib code

This commit is contained in:
2022-08-21 22:24:52 +03:00
parent 5a50b2ac98
commit ea3e9dd105
9 changed files with 244 additions and 316 deletions

View File

@@ -1,81 +1,44 @@
import logging
import tempfile
from os import path, makedirs, rename, remove, walk
from random import random
from asyncio import sleep
from os import path, makedirs, rename, remove
from random import randrange
import disnake
import inter as inter
from disnake import FFmpegPCMAudio
from enum import Enum
from disnake.ext import commands
import lib
# Files = commands.option_enum(lib.Commands.list_files(inter.id))
def Files(str, fold, Enum) -> None:
fl = []
for filenames in walk(fold):
fl.extend(filenames)
break
files = {}
for x in fl[2]:
files[x] = x
from lib.DB import read_db, check_exist_audio, add_audio
class Audio(commands.Cog):
def __init__(self, bot):
self.bot = bot # defining bot as global var in class
self.bot = bot
@commands.Cog.listener() # this is a decorator for events/listeners
@commands.Cog.listener()
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.Cog.listener()
async def on_voice_state_update(self, member, before, after):
role = await lib.Commands.read_json(member.guild.id, 'tigger_role')
audio = await lib.DB.read_db(member.guild.id, member.id, 'usertracks')
audio_files = await lib.Commands.list_files(f'audio/{member.id}')
logging.info(f'Func check audio state:\n'
f'\t\t\t\t\tUser: {member}\n'
f'\t\t\t\t\tTrigger role: {role}\n'
f'\t\t\t\t\tAudio list: {audio}\n'
f'\t\t\t\t\tAudio file list: {audio_files}\n'
f'\t\t\t\t\t-----------------')
if before.channel is None and role in member.roles:
pass
# track = random.randint(0, len(f) - 1)
# audio_source = FFmpegPCMAudio(f'audio/{f[track]}')
# logging.error(f'{track}\t\t{f[track]}')
# if not 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()
# if before.channel is None and member.id == _memb:
# track = random.randint(0, len(f) - 1)
# audio_source = FFmpegPCMAudio(f'audio/{_memb}/bear2_enemy_scav3.wav')
# logging.error(f'{track}\t\t\t{f[track]}')
# if not 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()
# @commands.slash_command(name="select_audio")
# async def select_audio(self, inter: disnake.ApplicationCommandInteraction,
# files: Files):
# pass
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')
from lib.Comands import list_files
audio_files = await list_files(f'audio/{member.id}')
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]}')
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()
@commands.command(name="upload_audio")
async def upload_audio(self, ctx, user=None):
@@ -88,7 +51,7 @@ class Audio(commands.Cog):
try:
makedirs(f'audio/{user.id}')
except error as _error:
logging.info(f"Failed to create dir", _error)
pass
for at in ctx.message.attachments:
import mimetypes
@@ -102,14 +65,14 @@ class Audio(commands.Cog):
await ctx.reply(f'Audio duration is {duration}, but max is 15')
remove(f'{tempfile.tempdir}/{user.id}/{at.filename}')
else:
a = await lib.DB.read_db(ctx.guild.id, user.id, 'usertracks')
a = await read_db(ctx.guild.id, user.id, 'usertracks')
if a:
audiolist = a + ", " + f'{at.filename}'
else:
audiolist = f'{at.filename}'
await lib.DB.check_exist_audio(ctx, ctx.guild.id, user.id, 'usertracks', at.filename)
await lib.DB.add_audio(ctx.guild.id, user.id, audiolist)
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}')
elif guess[0].split('/')[0] != 'audio':
await ctx.reply(f'It not audio {at.filename}\n it`s {guess[0]}')

View File

@@ -5,6 +5,8 @@ from disnake import Option, OptionType, Colour
from disnake.ext import commands
import lib
from lib.Comands import read_json, set_prefix, write_json
from lib.DB import fill_bd, prepare_db, work_with_db
class General(commands.Cog):
@@ -14,23 +16,23 @@ class General(commands.Cog):
@commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self):
for g in self.bot.get_all_members():
await lib.DB.prepare_db(g.guild.id)
await prepare_db(g.guild.id)
for g in self.bot.get_all_members():
await lib.DB.fill_bd(g.name, g.id, g.bot, g.nick, g.guild.id)
await fill_bd(g.name, g.id, g.bot, g.nick, g.guild.id)
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.Cog.listener()
async def on_guild_join(self, guild):
for g in guild.members:
await lib.DB.fill_bd(g.name, g.id, g.bot, g.nick, guild.id)
await fill_bd(g.name, g.id, g.bot, g.nick, guild.id)
@commands.Cog.listener()
async def on_member_join(self, member):
await lib.DB.fill_bd(member.name, member.id, member.bot, member.nick, member.guild.id)
bot_role = lib.Commands.read_json(member.guild.id, 'bot_role') # Get bot role
guest_role = lib.Commands.read_json(member.guild.id, 'guest_role') # Get guest role
bot_role = read_json(member.guild.id, 'bot_role') # Get bot role
guest_role = read_json(member.guild.id, 'guest_role') # Get guest role
if bot_role and guest_role:
if member.bot == 0:
@@ -44,7 +46,7 @@ class General(commands.Cog):
async def on_member_update(self, before: disnake.Member, after: disnake.Member):
sql_update_query = f"""UPDATE "{after.guild.id}" set nick = ? where userid = ?"""
data_tuple = (after.nick, before.id)
await lib.DB.work_with_db(sql_update_query, data_tuple)
await work_with_db(sql_update_query, data_tuple)
@commands.slash_command(
name="info",
@@ -99,13 +101,13 @@ class General(commands.Cog):
)
@commands.has_permissions(administrator=True)
async def set_guest_role(self, ctx, role):
await lib.Commands.write_json(ctx.guild.id, "guest_role", role.id)
await write_json(ctx.guild.id, "guest_role", role.id)
await ctx.send(f"Setted up dsss bot role to: `{role.name}`", ephemeral=True)
@commands.command(name="set_prefix")
@commands.has_permissions(administrator=True)
async def command_set_prefix(self, ctx, prefix: str):
await lib.Commands.set_prefix(ctx, prefix)
await set_prefix(ctx.guild.id, prefix)
await ctx.reply(f"Prefix set to: `{prefix}`")
@commands.slash_command(
@@ -117,7 +119,7 @@ class General(commands.Cog):
)
@commands.has_permissions(administrator=True)
async def slash_set_prefix(self, inter, prefix: str):
await lib.Commands.set_prefix(inter, prefix)
await set_prefix(inter.guild.id, prefix)
await inter.responce.send_message(f"Prefix set to: `{prefix}`", ephemeral=True)
@slash_set_prefix.error
@@ -133,7 +135,7 @@ class General(commands.Cog):
)
@commands.has_permissions(administrator=True)
async def set_trigger_role(self, inter, role):
await lib.Commands.write_json(inter.guild.id, "tigger_role", role.id)
await write_json(inter.guild.id, "tigger_role", role.id)
await inter.response.send_message(f"Role to trigger set to : `{role.name}`", ephemeral=True)
@commands.slash_command(
@@ -145,7 +147,7 @@ class General(commands.Cog):
)
@commands.has_permissions(administrator=True)
async def set_bot_role(self, ctx, role):
await lib.Commands.write_json(ctx.guild.id, "bot_role", role.id)
await write_json(ctx.guild.id, "bot_role", role.id)
await ctx.send(f"Setted up bot role to: `{role.name}`", ephemeral=True)
@commands.slash_command()

View File

@@ -1,9 +1,8 @@
import logging
import os
import psutil
import disnake
import psutil
from disnake.ext import commands
@@ -27,6 +26,7 @@ class Bot_info(commands.Cog, name='Bot Info'):
emb.add_field(name="Bot ping", value=f'Bot ping: {round(self.bot.latency * 1000)}', inline=True)
emb.add_field(name="Memory Usage", value=f"{_process.memory_info()[0]/2**20} Mb", inline=True)
emb.add_field(name="CPU Usage", value=f"{_process.cpu_percent()}%", inline=True)
emb.add_field(name="Prefix", value=f"{self.bot.prefix}")
emb.add_field(name="Bot owner", value=f"<@{self.bot.owner_id}>")
emb.set_footer(text="Information requested by: {}".format(inter.author.display_name))