[refactor]: Move command to separate cog

This commit is contained in:
Sweetbread 2023-05-15 23:23:40 +03:00
parent d905aca2fe
commit d8d8597ade
2 changed files with 62 additions and 86 deletions

86
bot.py
View File

@ -8,9 +8,6 @@ from json import load, dump
from os import listdir, getenv
from os.path import isfile, join, getsize
import utils.XOR
import utils.FFC
import discord
from discord.utils import get
from discord.ext import commands, tasks
@ -35,7 +32,6 @@ import Levenshtein
import DiscordUtils
from utils.emojis import *
from utils.checks import *
from utils.translate import *
from cogs.music import *
TOKEN = getenv('NATSUKO_TOKEN')
my_id = 710089471835504672
@ -207,88 +203,6 @@ async def clear(ctx, count: int):
await ctx.channel.purge(limit=count)
@bot.group(brief="$commands_HTiP_brief")
async def HTiP(ctx):
region = ctx.message.author.id
region = db.members.find_one({"id": region})["language"]
if ctx.invoked_subcommand is None:
await ctx.send(translate('$commands_HTiP_subcommand', region))
abc = str(ctx.message.attachments[0].filename)[-3:]
if not ctx.message.attachments and abc != 'jpg' and abc != 'png':
await ctx.send(translate('$commands_HTiP_picNotExists', region))
else:
url = ctx.message.attachments[0].url
filename = ctx.message.attachments[0].filename
img = requests.get(url)
img_file = open(filename, 'wb')
img_file.write(img.content)
img_file.close()
@HTiP.command(brief="$commands_HTiP_w_brief")
async def w(ctx, *, text: str):
region = ctx.message.author.id
region = db.members.find_one({"id": region})["language"]
if text is None:
await ctx.send(translate('$commands_HTiP_w_textNotExists', region))
return None
if text.split(' ')[-1].startswith('key='):
key = text.split(' ')[-1][4:]
text = ' '.join(text.split(' ')[:-1])
else:
key = None
if key is not None:
text = XOR.char_encode(text, key)
abc = str(ctx.message.attachments[0].filename)[-3:]
if not ctx.message.attachments and abc != 'jpg' and abc != 'png':
await ctx.send(translate('$commands_HTiP_picNotExists', region))
else:
url = ctx.message.attachments[0].url
filename = ctx.message.attachments[0].filename
img = requests.get(url)
img_file = open(filename, 'wb')
img_file.write(img.content)
img_file.close()
FFC.write(filename, 'secret.' + abc, text)
with open('secret.' + abc, 'rb') as f:
file = discord.File(f)
await ctx.send(file=file)
@HTiP.command(brief="$commands_HTiP_r_brief")
async def r(ctx, key: str = None):
region = ctx.message.author.id
region = db.members.find_one({"id": region})["language"]
try:
abc = str(ctx.message.attachments[0].filename)[-3:]
if not ctx.message.attachments and abc != 'jpg' and abc != 'png':
await ctx.send(translate('$commands_HTiP_picNotExists', region))
except IndexError:
await ctx.send(translate('$commands_HTiP_picNotExists', region))
else:
url = ctx.message.attachments[0].url
filename = ctx.message.attachments[0].filename
img = requests.get(url)
img_file = open('C:\\Users\\gleb\\PycharmProjects\\systemnik\\' + filename, 'wb')
img_file.write(img.content)
img_file.close()
secret_text = FFC.read(filename)
if key is not None:
secret_text = XOR.char_encode(secret_text, key)
await ctx.send(secret_text)
@bot.command(name='eval')
@commands.check(is_secret)

62
cogs/fun.py Normal file
View File

@ -0,0 +1,62 @@
import discord
import requests
import utils.XOR as XOR
import utils.FFC as FFC
from discord import app_commands
from discord.ext import commands
from os import remove
from os.path import splitext, join
tmp_dir = "tmp"
class Fun(commands.Cog):
htip_gr = app_commands.Group(name="htip", description="Hide text in picture")
@htip_gr.command()
async def write(self, itr: discord.Interaction, text: str, pic: discord.Attachment, key: str = None):
if key is not None:
text = XOR.char_encode(text, key)
filename = f"{itr.id}.pic.filename"
fln, ext = splitext(filename)
if ext not in ('.jpg', '.png'):
await itr.response.send_message("Supported only JPEG and PNG")
return
url = pic.url
img = requests.get(url)
with open(join(tmp_dir, filename), 'wb') as img_file:
img_file.write(img.content)
FFC.write(join(tmp_dir, filename), join(tmp_dir, f'{fln}.secret{ext}'), text)
with open(join(tmp_dir, f'{fln}.secret{ext}'), 'rb') as f:
await itr.response.send_message(file=discord.File(f))
remove(join(tmp_dir, filename))
remove(join(tmp_dir, f'{fln}.secret{ext}'))
@htip_gr.command()
async def read(self, itr: discord.Interaction, pic: discord.Attachment, key: str = None):
filename = f"{itr.id}.pic.filename"
fln, ext = splitext(filename)
if ext not in ('.jpg', '.png'):
await itr.response.send_message('Supported only JPEG or PNG')
return
url = pic.url
img = requests.get(url)
with open(join(tmp_dir, filename), 'wb') as img_file:
img_file.write(img.content)
secret_text = FFC.read(join(tmp_dir, filename))
if key is not None:
secret_text = XOR.char_encode(secret_text, key)
await itr.response.send_message(secret_text)
remove(join(tmp_dir, filename))
async def setup(bot):
await bot.add_cog(Fun())