added requirements.txt

This commit is contained in:
2022-08-28 19:25:39 +03:00
parent a603898f2d
commit 78a477f21a
8 changed files with 175 additions and 125 deletions

View File

@@ -2,11 +2,12 @@ import logging
from asyncio import sleep
import disnake
from disnake import Option, OptionType
from disnake.ext import commands, tasks
from lib.Comands import read_json
from lib.DB import fill_bd, prepare_db
from lib.Comands import read_json, write_json
from lib.DB import fill_bd, prepare_db, work_with_db
class Admin(commands.Cog, name='Admin'):
@@ -39,6 +40,12 @@ class Admin(commands.Cog, name='Admin'):
)
)
@commands.Cog.listener()
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 work_with_db(sql_update_query, data_tuple)
@commands.Cog.listener()
async def on_guild_join(self, guild):
for g in guild.members:
@@ -59,6 +66,93 @@ class Admin(commands.Cog, name='Admin'):
logging.info(f"Adding to {member} role {role}")
await member.add_roles(role)
@commands.slash_command(
name="set_guest_role",
description="Set Default bot role",
options=[
Option("role", "Specify role", OptionType.role, required=True),
]
)
@commands.has_permissions(administrator=True)
async def set_guest_role(self, inter, role):
await write_json(inter.guild.id, "guest_role", role.id)
await inter.response.send_message(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 write_json(ctx.guild.id, "prefix", prefix)
await ctx.reply(f"Prefix set to: `{prefix}`")
@commands.guild_only()
@commands.slash_command(
name="set_prefix",
description="Setting up bot prefix",
options=[
Option("prefix", "Specify prefix", OptionType.string, required=True),
]
)
@commands.has_permissions(administrator=True)
async def slash_set_prefix(self, inter, prefix: str):
await write_json(inter.guild.id, "prefix", prefix)
await inter.response.send_message(f"Prefix set to: `{prefix}`", ephemeral=True)
@commands.guild_only()
@commands.slash_command(
name="set_trigger_role",
description="Setting up role to trigger bot",
options=[
Option("role", "Specify role", OptionType.role, required=True),
]
)
@commands.has_permissions(administrator=True)
async def set_trigger_role(self, inter, role):
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(
name="set_bot_role",
description="Set Default bot role",
options=[
Option("role", "Specify role", OptionType.role, required=True),
]
)
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def set_bot_role(self, ctx, role):
await write_json(ctx.guild.id, "bot_role", role.id)
await ctx.send(f"Setted up bot role to: `{role.name}`", ephemeral=True)
@set_bot_role.error
@set_trigger_role.error
@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.has_permissions(administrator=True)
@commands.command(
name="set_time",
description="Read list of tracks for user",
options=[
Option("seconds", "specify max duration", OptionType.integer, required=True),
]
)
async def set_time(self, inter, 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)
@commands.has_permissions(administrator=True)
@commands.command(
name="set_bot_channel",
description="Set channel whitch itterate with bot",
options=[
Option("channel", "specify channel", OptionType.channel, required=True),
]
)
async def set_time(self, inter, _channel: commands.Range[5, 30]):
await write_json(inter.guild.id, "channel", _channel)
await inter.response.send_message(f"Channet setted up to{_channel} sec", ephemeral=True)
def setup(bot): # an extension must have a setup function
bot.add_cog(Admin(bot)) # adding a cog

View File

@@ -1,14 +1,15 @@
import logging
import random
import tempfile
from os import path, makedirs, rename, remove
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
# todo: write chose audio from list by slash command
class Audio(commands.Cog):
def __init__(self, bot):
self.bot = bot
@@ -17,46 +18,44 @@ class Audio(commands.Cog):
async def on_ready(self):
logging.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):
if any('Escape from Tarkov' in str(user.activity) for user in after.channel.members):
logging.info('Skip playing by Game')
else:
from lib.Comands import read_json
_role = await read_json(member.guild.id, 'tigger_role')
# 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]}'
if def_audio_db is not None:
def_audio_db = def_audio_db.split(', ')
from lib.Comands import list_files
def_audio_ls = await list_files()
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):
logging.info('Skip playing by Game')
else:
from lib.Comands import read_json
_role = await read_json(member.guild.id, 'tigger_role')
# 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]}'
if def_audio_db is not None:
def_audio_db = def_audio_db.split(', ')
from lib.Comands import list_files
def_audio_ls = await list_files()
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')
full_audio = def_audio_db + audio_db
audio = random.choice(full_audio)
await play_audio(audio, self.bot, after.channel)
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')
elif any(str(role.id) in _role for role in member.roles):
logging.info(f'Play audio from list by role')
audio = random.choice(def_audio_ls)
await play_audio(audio, self.bot, after.channel)
await play_audio(random.choice(def_audio_ls), self.bot, after.channel)
else:
logging.info(f'Skip playing')
@commands.command(name="upload_audio")
async def upload_audio(self, ctx, user=None):
user = user or ctx.author
print(tempfile.tempdir)
if ctx.author.guild_permissions.administrator or user is ctx.author:
if ctx.message.attachments:
from os import error
@@ -68,15 +67,16 @@ class Audio(commands.Cog):
for at in ctx.message.attachments:
import mimetypes
await at.save(f'{tempfile.tempdir}/{tempfile.mkdtemp(dir=f"tmp/{user.id}")}/{at.filename}')
guess = mimetypes.guess_type(f'{tempfile.tempdir}/{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'{tempfile.tempdir}/{user.id}/{at.filename}'
file = f'tmp/{user.id}")/{at.filename}'
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
if duration > 20:
await ctx.reply(f'Audio duration is {duration}, but max is 15')
remove(f'{tempfile.tempdir}/{user.id}/{at.filename}')
max_duration = determine_time(ctx)
if duration > max_duration:
await ctx.reply(f'Audio duration is {duration}, but max is {max_duration}')
remove(f'tmp/{user.id}")/{at.filename}')
else:
a = await read_db(ctx.guild.id, user.id, 'usertracks')
if a:
@@ -86,10 +86,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'{tempfile.tempdir}/{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'{tempfile.tempdir}/{user.id}/{at.filename}')
remove(f'tmp/{user.id}")/{at.filename}')
else:
await ctx.reply("Has no Attachment")
else:

View File

@@ -16,12 +16,6 @@ class General(commands.Cog):
async def on_ready(self):
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.Cog.listener()
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 work_with_db(sql_update_query, data_tuple)
@commands.slash_command(
name="info",
description="Read list of tracks for user",
@@ -66,69 +60,6 @@ class General(commands.Cog):
await inter.response.send_message(embed=emb, ephemeral=True)
@commands.slash_command(
name="set_guest_role",
description="Set Default bot role",
options=[
Option("role", "Specify role", OptionType.role, required=True),
]
)
@commands.has_permissions(administrator=True)
async def set_guest_role(self, inter, role):
await write_json(inter.guild.id, "guest_role", role.id)
await inter.response.send_message(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 write_json(ctx.guild.id, "prefix", prefix)
await ctx.reply(f"Prefix set to: `{prefix}`")
@commands.guild_only()
@commands.slash_command(
name="set_prefix",
description="Setting up bot prefix",
options=[
Option("prefix", "Specify prefix", OptionType.string, required=True),
]
)
@commands.has_permissions(administrator=True)
async def slash_set_prefix(self, inter, prefix: str):
await write_json(inter.guild.id, "prefix", prefix)
await inter.response.send_message(f"Prefix set to: `{prefix}`", ephemeral=True)
@commands.guild_only()
@commands.slash_command(
name="set_trigger_role",
description="Setting up role to trigger bot",
options=[
Option("role", "Specify role", OptionType.role, required=True),
]
)
@commands.has_permissions(administrator=True)
async def set_trigger_role(self, inter, role):
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(
name="set_bot_role",
description="Set Default bot role",
options=[
Option("role", "Specify role", OptionType.role, required=True),
]
)
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def set_bot_role(self, ctx, role):
await write_json(ctx.guild.id, "bot_role", role.id)
await ctx.send(f"Setted up bot role to: `{role.name}`", ephemeral=True)
@set_bot_role.error
@set_trigger_role.error
@slash_set_prefix.error
async def set_prefix_error(self, inter, prefix):
await inter.response.send_message("You don`t have permissions", ephemeral=True)
def setup(bot): # an extension must have a setup function
bot.add_cog(General(bot)) # adding a cog

View File

@@ -5,7 +5,7 @@ import disnake
import psutil
from disnake.ext import commands
from lib.Comands import determine_prefix
from lib.Comands import determine_prefix, determine_time
class Bot_info(commands.Cog, name='Bot Info'):
@@ -24,11 +24,13 @@ class Bot_info(commands.Cog, name='Bot Info'):
title=f"General information",
description=f"General information on about bot",
)
# emb.set_thumbnail(self.bot.display_avatar)
# emb.set_thumbnail(self.bot.user.avarat_url)
emb.add_field(name="System info:", value=f"Memory Usage: {round(_process.memory_info().rss / 2 ** 20, 2)} Mb\n"
f"CPU Usage: {_process.cpu_percent()}%\n"
f'Bot ping: {round(self.bot.latency * 1000)}\n'
f'Prefix: `{determine_prefix(self.bot, inter)}`')
f'Prefix: `{determine_prefix(self.bot, inter)}\n`'
f"Max audio duration: {determine_time(inter)} sec\n"
)
emb.add_field(name="Bot owner", value=f"<@{self.bot.owner_id}>")
emb.set_footer(text="Information requested by: {}".format(inter.author.display_name))