[fix]: Fix paths

This commit is contained in:
Sweetbread 2023-05-07 01:55:55 +03:00
parent f5644666be
commit a61b74c608
2 changed files with 19 additions and 16 deletions

View File

@ -7,6 +7,7 @@ from os import mkdir, rmdir, remove
from cairosvg import svg2png
dir = "tmp"
class FileExt(commands.Cog):
def __init__(self, bot):
@ -16,42 +17,42 @@ class FileExt(commands.Cog):
async def on_message(self, message):
if message.attachments:
files = []
mkdir(joinpath("/home/pi/Koteika/tmp", str(message.id)))
mkdir(joinpath(dir, str(message.id)))
if list(filter(lambda x: splitext(x.filename)[1] in ('.mkv', '.svg'), message.attachments)):
m = await message.reply("Конвертация...")
# MKV
for at in filter(lambda x: x.filename.endswith('.mkv'), message.attachments):
await at.save(fp=joinpath("/home/pi/Koteika/tmp", str(message.id), at.filename))
await at.save(fp=joinpath(dir, str(message.id), at.filename))
import ffmpeg
(
ffmpeg
.input(joinpath("/home/pi/Koteika/tmp", str(message.id), at.filename))
.output(joinpath("/home/pi/Koteika/tmp", str(message.id), splitext(at.filename)[0]+'.mp4'))
.input(joinpath(dir, str(message.id), at.filename))
.output(joinpath(dir, str(message.id), splitext(at.filename)[0]+'.mp4'))
.run()
)
remove(joinpath("/home/pi/Koteika/tmp", str(message.id), at.filename))
with open(joinpath("/home/pi/Koteika/tmp", str(message.id), splitext(at.filename)[0]+'.mp4'), 'rb') as f:
remove(joinpath(dir, str(message.id), at.filename))
with open(joinpath(dir, str(message.id), splitext(at.filename)[0]+'.mp4'), 'rb') as f:
files.append( discord.File(f, spoiler=at.is_spoiler(), filename=splitext(at.filename)[0]+'.mp4') )
remove(joinpath("/home/pi/Koteika/tmp", str(message.id), splitext(at.filename)[0]+'.mp4'))
remove(joinpath(dir, str(message.id), splitext(at.filename)[0]+'.mp4'))
# SVG
for at in filter(lambda x: x.filename.endswith('.svg'), message.attachments):
code = await at.read()
code = code.decode("utf-8")
svg2png(bytestring=code, write_to=joinpath("/home/pi/Koteika/tmp", str(message.id), splitext(at.filename)[0]+'.png'))
svg2png(bytestring=code, write_to=joinpath(dir, str(message.id), splitext(at.filename)[0]+'.png'))
with open(joinpath("/home/pi/Koteika/tmp", str(message.id), splitext(at.filename)[0]+'.png'), 'rb') as f:
with open(joinpath(dir, str(message.id), splitext(at.filename)[0]+'.png'), 'rb') as f:
files.append( discord.File(f, spoiler=at.is_spoiler(), filename=splitext(at.filename)[0]+'.png') )
remove(joinpath("/home/pi/Koteika/tmp", str(message.id), splitext(at.filename)[0]+'.png'))
remove(joinpath(dir, str(message.id), splitext(at.filename)[0]+'.png'))
if files:
await message.reply(files=files)
await m.delete()
rmdir(joinpath("/home/pi/Koteika/tmp", str(message.id)))
rmdir(joinpath(dir, str(message.id)))
async def setup(bot):
await bot.add_cog(FileExt(bot))

View File

@ -4,7 +4,9 @@ from discord import app_commands
from discord.app_commands import Choice
from discord.ext import commands, tasks
from os import listdir
from os.path import splitext
from os.path import splitext, join
dir = "Stickers"
class Stickers(commands.Cog, name="Стикеры"):
def __init__(self, bot):
@ -20,7 +22,7 @@ class Stickers(commands.Cog, name="Стикеры"):
color = discord.Color(0xaaffaa)
list_ = listdir("/home/pi/Koteika/Stickers")
list_ = listdir(dir)
embed = discord.Embed(title="Стикеры", description="\n".join([f"{i+1}: {list_[i]}" for i in range(len(list_))]), color=color)
await ctx.send(embed=embed)
@ -35,7 +37,7 @@ class Stickers(commands.Cog, name="Стикеры"):
embed = discord.Embed(color=color)
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar)
with open(f"/home/pi/Koteika/Stickers/{listdir('/home/pi/Koteika/Stickers')[sticker-1]}", 'rb') as f:
with open(join(dir, listdir(dir)[sticker-1]), 'rb') as f:
await ctx.send(content, file=discord.File(f), embed=embed, reference=ctx.message.reference)
try: await ctx.message.delete()
except: pass
@ -43,9 +45,9 @@ class Stickers(commands.Cog, name="Стикеры"):
@app_commands.command(name="sticker", description="Отправляет стикер")
@app_commands.guilds(discord.Object(822157545622863902))
@app_commands.describe(sticker="Стикер")
@app_commands.choices(sticker=[Choice(name=splitext(i)[0], value=i) for i in listdir("/home/pi/Koteika/Stickers")])
@app_commands.choices(sticker=[Choice(name=splitext(i)[0], value=i) for i in listdir(dir)])
async def send_sticker(self, inter, sticker: Choice[str]):
with open(f"/home/pi/Koteika/Stickers/{sticker.value}", 'rb') as f:
with open(join(dir, sticker.value), 'rb') as f:
await inter.response.send_message(file=discord.File(f))