refractor lib code

This commit is contained in:
2022-08-22 01:17:27 +03:00
parent ea3e9dd105
commit e1b782c2cb
5 changed files with 34 additions and 47 deletions

View File

@@ -25,6 +25,7 @@ class Audio(commands.Cog):
audio = await read_db(member.guild.id, member.id, 'usertracks') audio = await read_db(member.guild.id, member.id, 'usertracks')
from lib.Comands import list_files from lib.Comands import list_files
audio_files = await list_files(f'audio/{member.id}') audio_files = await list_files(f'audio/{member.id}')
logging.info(f"state changed to user {member}")
f = await list_files() f = await list_files()
if role is not None and before.channel is None and role in member.roles: if role is not None and before.channel is None and role in member.roles:
track = randrange(0, len(f) - 1, 1) track = randrange(0, len(f) - 1, 1)

View File

@@ -4,9 +4,8 @@ import disnake
from disnake import Option, OptionType, Colour from disnake import Option, OptionType, Colour
from disnake.ext import commands from disnake.ext import commands
import lib
from lib.Comands import read_json, set_prefix, write_json from lib.Comands import read_json, set_prefix, write_json
from lib.DB import fill_bd, prepare_db, work_with_db from lib.DB import fill_bd, prepare_db, work_with_db, read_db
class General(commands.Cog): class General(commands.Cog):
@@ -29,12 +28,12 @@ class General(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_join(self, member): async def on_member_join(self, member):
await lib.DB.fill_bd(member.name, member.id, member.bot, member.nick, member.guild.id) await fill_bd(member.name, member.id, member.bot, member.nick, member.guild.id)
bot_role = read_json(member.guild.id, 'bot_role') # Get bot 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 guest_role = read_json(member.guild.id, 'guest_role') # Get guest role
if bot_role and guest_role: if bot_role or guest_role:
if member.bot == 0: if member.bot == 0:
role = disnake.utils.get(member.guild.roles, id=guest_role) role = disnake.utils.get(member.guild.roles, id=guest_role)
else: else:
@@ -57,8 +56,8 @@ class General(commands.Cog):
) )
async def info(self, inter, user=None): async def info(self, inter, user=None):
user = user or inter.author user = user or inter.author
user_audio = await lib.DB.read_db(inter.guild.id, user.id, column='usertracks') user_audio = await read_db(inter.guild.id, user.id, column='usertracks')
default_audio = await lib.DB.read_db(inter.guild.id, user.id, column='defaulttracks') default_audio = await read_db(inter.guild.id, user.id, column='defaulttracks')
rolelist = [r.mention for r in user.roles if r != inter.guild.default_role] rolelist = [r.mention for r in user.roles if r != inter.guild.default_role]
if rolelist: if rolelist:
roles = "\n".join(rolelist) roles = "\n".join(rolelist)
@@ -100,9 +99,9 @@ class General(commands.Cog):
] ]
) )
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def set_guest_role(self, ctx, role): async def set_guest_role(self, inter, role):
await write_json(ctx.guild.id, "guest_role", role.id) await write_json(inter.guild.id, "guest_role", role.id)
await ctx.send(f"Setted up dsss bot role to: `{role.name}`", ephemeral=True) await inter.response.send_message(f"Setted up dsss bot role to: `{role.name}`", ephemeral=True)
@commands.command(name="set_prefix") @commands.command(name="set_prefix")
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
@@ -110,6 +109,7 @@ class General(commands.Cog):
await set_prefix(ctx.guild.id, prefix) await set_prefix(ctx.guild.id, prefix)
await ctx.reply(f"Prefix set to: `{prefix}`") await ctx.reply(f"Prefix set to: `{prefix}`")
@commands.guild_only()
@commands.slash_command( @commands.slash_command(
name="set_prefix", name="set_prefix",
description="Setting up bot prefix", description="Setting up bot prefix",
@@ -120,12 +120,9 @@ class General(commands.Cog):
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def slash_set_prefix(self, inter, prefix: str): async def slash_set_prefix(self, inter, prefix: str):
await set_prefix(inter.guild.id, prefix) await set_prefix(inter.guild.id, prefix)
await inter.responce.send_message(f"Prefix set to: `{prefix}`", ephemeral=True) await inter.response.send_message(f"Prefix set to: `{prefix}`", ephemeral=True)
@slash_set_prefix.error
async def set_prefix_error(self, inter, prefix):
await inter.response.send_message('You don`t have permissions', ephemeral=True)
@commands.guild_only()
@commands.slash_command( @commands.slash_command(
name="set_trigger_role", name="set_trigger_role",
description="Setting up role to trigger bot", description="Setting up role to trigger bot",
@@ -145,24 +142,17 @@ class General(commands.Cog):
Option("role", "Specify role", OptionType.role, required=True), Option("role", "Specify role", OptionType.role, required=True),
] ]
) )
@commands.guild_only()
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def set_bot_role(self, ctx, role): async def set_bot_role(self, ctx, role):
await 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) await ctx.send(f"Setted up bot role to: `{role.name}`", ephemeral=True)
@commands.slash_command() @set_bot_role.error
async def help(self, inter): @set_trigger_role.error
embed = disnake.Embed( @slash_set_prefix.error
title='Help', description='', colour=disnake.Colour.blue()) async def set_prefix_error(self, inter, prefix):
embed.set_footer(text='Have fun!') await inter.response.send_message('You don`t have permissions', ephemeral=True)
embed.add_field(
name='Prefix',
value=f'The current prefix for this server is {self.bot.command_prefix}',
inline=True)
logging.info(f'server prefix {self.bot.command_prefix}')
await inter.response.send_message(embed=embed, ephemeral=True)
def setup(bot): # an extension must have a setup function def setup(bot): # an extension must have a setup function

View File

@@ -5,6 +5,8 @@ import disnake
import psutil import psutil
from disnake.ext import commands from disnake.ext import commands
from lib.Comands import determine_prefix
class Bot_info(commands.Cog, name='Bot Info'): class Bot_info(commands.Cog, name='Bot Info'):
def __init__(self, bot): def __init__(self, bot):
@@ -23,10 +25,10 @@ class Bot_info(commands.Cog, name='Bot Info'):
description=f"General information on about bot", description=f"General information on about bot",
) )
# emb.set_thumbnail(self.bot.display_avatar) # emb.set_thumbnail(self.bot.display_avatar)
emb.add_field(name="Bot ping", value=f'Bot ping: {round(self.bot.latency * 1000)}', inline=True) emb.add_field(name="System info:", value=f"Memory Usage: {round(_process.memory_info().rss / 2 ** 20, 2)} Mb\n"
emb.add_field(name="Memory Usage", value=f"{_process.memory_info()[0]/2**20} Mb", inline=True) f"CPU Usage: {_process.cpu_percent()}%\n"
emb.add_field(name="CPU Usage", value=f"{_process.cpu_percent()}%", inline=True) f'Bot ping: {round(self.bot.latency * 1000)}\n'
emb.add_field(name="Prefix", value=f"{self.bot.prefix}") f'Prefix: `{determine_prefix(self.bot, inter)}`')
emb.add_field(name="Bot owner", value=f"<@{self.bot.owner_id}>") emb.add_field(name="Bot owner", value=f"<@{self.bot.owner_id}>")
emb.set_footer(text="Information requested by: {}".format(inter.author.display_name)) emb.set_footer(text="Information requested by: {}".format(inter.author.display_name))

View File

@@ -1,11 +1,5 @@
import json import json
from os import walk, path from os import walk
def check_json():
if not path.isfile('prefix.json'):
with open('prefix.json', 'w+', encoding='utf-8') as f:
f.write("")
async def set_prefix(guildid: int, prefix: str) -> None: async def set_prefix(guildid: int, prefix: str) -> None:
@@ -25,7 +19,6 @@ async def list_files(fold: str = 'audio'):
async def read_json(guild: int, param: str): async def read_json(guild: int, param: str):
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
parameter = None
try: try:
_json = json.load(fp) # Load the custom prefixes _json = json.load(fp) # Load the custom prefixes
except TypeError: except TypeError:
@@ -39,6 +32,7 @@ async def read_json(guild: int, param: str):
pass pass
except: except:
pass pass
return parameter return parameter
@@ -66,7 +60,6 @@ async def determine_prefix(bot, msg):
:return: prefix for server, default is $ :return: prefix for server, default is $
""" """
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
parameter: str
try: try:
from json import load from json import load
_json = load(fp) # Load the custom prefixes _json = load(fp) # Load the custom prefixes

15
test.py
View File

@@ -1,18 +1,19 @@
import logging import logging
import sys import sys
from os import path
import disnake import disnake
from disnake import OptionChoice, OptionType, Option from disnake import OptionChoice, OptionType, Option
from disnake.ext import commands from disnake.ext import commands
from lib.CogsPrepare import cog_list, work_with_cogs from lib.CogsPrepare import cog_list, work_with_cogs
from lib.Comands import check_json, determine_prefix from lib.Comands import determine_prefix
bot_owner = 386629192743256065 if not path.isfile('prefix.json'):
check_json() with open('prefix.json', 'w+', encoding='utf-8') as f:
intents = disnake.Intents(messages=True, guilds=True, message_content=True) f.write("")
intents.members = True
intents.voice_states = True intents = disnake.Intents(messages=True, guilds=True, message_content=True, voice_states=True, members=True)
bot = commands.Bot(command_prefix=determine_prefix, bot = commands.Bot(command_prefix=determine_prefix,
intents=intents, intents=intents,
@@ -62,7 +63,7 @@ async def on_ready():
] ]
) )
async def slash_cogs(inter, what_do): async def slash_cogs(inter, what_do):
if inter.author.id == bot_owner: if inter.author.id == bot.owner_id:
await work_with_cogs(what_do, bot) await work_with_cogs(what_do, bot)
await inter.response.send_message(f'Cogs are {what_do}ed', ephemeral=True) await inter.response.send_message(f'Cogs are {what_do}ed', ephemeral=True)