64 lines
2.8 KiB
Python
64 lines
2.8 KiB
Python
import logging
|
|
import lib
|
|
|
|
from os import path, makedirs, rename, remove
|
|
from discord.ext import commands
|
|
|
|
|
|
class Audio(commands.Cog):
|
|
def __init__(self, bot):
|
|
self.bot = bot # defining bot as global var in class
|
|
|
|
@commands.Cog.listener() # this is a decorator for events/listeners
|
|
async def on_ready(self):
|
|
logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
|
|
|
|
@commands.command(name="upload_audio")
|
|
async def upload_audio(self, ctx, user=None):
|
|
user = user or ctx.author
|
|
if ctx.author.guild_permissions.administrator or user is ctx.author:
|
|
if ctx.message.attachments:
|
|
from os import error
|
|
if not path.isdir(f'tmp/{user.id}'):
|
|
try:
|
|
makedirs(f'tmp/{user.id}')
|
|
except error as _error:
|
|
logging.info(f"Failed to create dir", _error)
|
|
if not path.isdir(f'audio/{user.id}'):
|
|
try:
|
|
makedirs(f'audio/{user.id}')
|
|
except error as _error:
|
|
logging.info(f"Failed to create dir", _error)
|
|
for at in ctx.message.attachments:
|
|
import mimetypes
|
|
|
|
await at.save(f'tmp/{user.id}/{at.filename}')
|
|
guess = mimetypes.guess_type(f'tmp/{user.id}/{at.filename}')
|
|
if guess[0]:
|
|
from pymediainfo import MediaInfo
|
|
file = f'tmp/{user.id}/{at.filename}'
|
|
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
|
|
if duration > 15:
|
|
await ctx.reply(f'Audio duration is {duration}, but max is 15')
|
|
remove(f'tmp/{user.id}/{at.filename}')
|
|
else:
|
|
a = lib.DB.read_db(ctx.guild.id, user.id)
|
|
if a is None:
|
|
audiolist = f'{user.id}/{at.filename}'
|
|
else:
|
|
audiolist = lib.DB.read_db(ctx.guild.id, user.id) + ", " + f'{user.id}/{at.filename}'
|
|
|
|
lib.DB.add_audio(ctx.guild.id, user.id, audiolist)
|
|
rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}')
|
|
else:
|
|
await ctx.reply(f'Failed to find MIME type of {at.filename}', ephemeral=True)
|
|
remove(f'tmp/{user.id}/{at.filename}')
|
|
else:
|
|
await ctx.reply("Has no Attachment", ephemeral=True)
|
|
else:
|
|
await ctx.reply(f'You`re not admin. You can add audio only for your own account', ephemeral=True)
|
|
|
|
|
|
def setup(bot): # an extension must have a setup function
|
|
bot.add_cog(Audio(bot)) # adding a cog
|