This commit is contained in:
2022-06-26 18:56:10 +03:00
parent dc1f3b07b2
commit 611b722f9a
2 changed files with 113 additions and 14 deletions

127
test.py
View File

@@ -1,3 +1,5 @@
import json
import os
import sys
import threading
import logging
@@ -6,7 +8,7 @@ import sqlite3
from os import walk
from discord.ext import commands
from dislash import InteractionClient, Option, OptionType
from dislash import InteractionClient, Option, OptionType, SelectMenu, SelectOption
class DB:
@@ -52,7 +54,6 @@ class DB:
sql_read = f"""SELECT * FROM "{guildid}" where userid = {user}"""
cursor.execute(sql_read)
record = cursor.fetchone()
print(record[4])
if record[4] is None:
return "None"
else:
@@ -66,9 +67,19 @@ threading.current_thread().name = "main"
logging.basicConfig(stream=sys.stdout, filemode='w', level='INFO',
format='%(asctime)s - %(levelname)s - %(threadName)s - %(message)s')
with open("config.json", 'r') as f:
prefixes = json.load(f)
default_prefix = "$"
def prefix(bot, message):
id = message.guild.id
return prefixes.get(id, default_prefix)
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='$', guild_subscriptions=True, intents=intents)
bot = commands.Bot(command_prefix=prefix, guild_subscriptions=True, intents=intents)
inter_client = InteractionClient(bot)
@@ -85,6 +96,11 @@ class Arg:
dict[x] = x
class Audio:
def add(user,):
pass
@bot.event
async def on_ready():
for g in bot.get_all_members():
@@ -97,7 +113,13 @@ async def on_ready():
@bot.event
async def on_guild_join(guild):
DB._prepare_db(guild.id)
with open('config.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '$'
with open('config.json', 'r') as f:
json.dump(prefixes, f, indent=4)
for g in guild.members:
DB._fill_bd(g.name, g.id, g.bot, g.nick, guild.id)
@@ -107,11 +129,6 @@ async def on_member_join(member):
DB._fill_bd(member.name, member.id, member.bot, member.nick, member.guild.id)
# @bot.command()
# async def add_audio(ctx, user, audio):
# DB._add_audio(ctx.guild.id, user.removesuffix('>').removeprefix('<@'), audio)
@inter_client.slash_command(
name="add-audio",
description="Add audio track to user",
@@ -148,9 +165,16 @@ async def info(ctx, user=None):
user = user or ctx.author
audio = DB._read_db(ctx.guild.id, user.id)
rolelist = [r.mention for r in user.roles if r != ctx.guild.default_role]
audiolist = audio.split(", ")
roles = "\n".join(rolelist)
audios = "\n".join(audiolist)
if rolelist:
roles = "\n".join(rolelist)
else:
roles = "Not added any role"
if audio == "None":
audios = "Not selected audio"
else:
audios = "" + "\n".join(sorted(audio.split(", ")))
emb = discord.Embed(
title=f"General information",
@@ -170,8 +194,67 @@ async def info(ctx, user=None):
@bot.command()
async def test(ctx, user):
await ctx.send(DB._read_db(ctx.guild.id, user))
async def select_audio(ctx, user=None):
msg = await ctx.send(
"Select audio for user!",
components=[
SelectMenu(
custom_id="test",
placeholder="List of audio?",
max_values=2,
options=[
SelectOption("Option 1", "value 1"),
SelectOption("Option 2", "value 2"),
SelectOption("Option 3", "value 3")
]
)
]
)
# Wait for someone to click on it
inter = await msg.wait_for_dropdown()
# Send what you received
labels = [option.label for option in inter.select_menu.selected_options]
await inter.reply(f"Options: {', '.join(labels)}")
@inter_client.slash_command(description="Select audio from groups to user")
async def groups(inter):
pass
@groups.sub_command_group()
async def select_user_audio(inter, user=None):
pass
@bot.command(name="upload_audio")
async def upload_audio(ctx, user=None):
user = user or ctx.author
if ctx.author.guild_permissions.administrator or user is ctx.author:
if ctx.message.attachments:
if not os.path.isdir(f'tmp/{user.id}'):
try:
os.makedirs(f'tmp/{user.id}')
except os.error as error:
logging.info(f"Failed to create dir", error)
for a in ctx.message.attachments:
import mimetypes
await a.save(f'tmp/{user.id}/{a.filename}')
guess = mimetypes.guess_type(f'tmp/{user.id}/{a.filename}')
if guess[0]:
from pymediainfo import MediaInfo
file = f'tmp/{user.id}/{a.filename}'
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
if duration > 15:
await ctx.reply(f'Audio duration is {duration}, but max is 10')
else:
await ctx.reply(f'Failed to find MIME type of {a.filename}')
os.remove(f'tmp/{user.id}/{a.filename}')
else:
await ctx.reply("Has no Attachment")
else:
await ctx.reply(f'You`re not admin. You can add audio only for your own account')
@bot.event
@@ -181,4 +264,20 @@ async def on_member_update(before: discord.Member, after: discord.Member):
DB._work_with_db(sql_update_query, data_tuple)
@bot.command(pass_context=True)
@commands.has_permissions(administrator=True) #ensure that only administrators can use this command
async def changeprefix(ctx, prefix): #command: bl!changeprefix ...
with open('config.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('config.json', 'w') as f: #writes the new prefix into the .json
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix changed to: {prefix}') #confirms the prefix it's been changed to
#next step completely optional: changes bot nickname to also have prefix in the nickname
name=f'{prefix}BotBot'
bot.run('OTQ3OTUzOTAxNzgzNjIxNjYy.GTXbMv.KrztaTO7-ivsPEAVjsyikSQ-GP-ANwULmDraig')