From d8d8597ade4e4ce1e5ef4b59a98264ebcdab23aa Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Mon, 15 May 2023 23:23:40 +0300 Subject: [PATCH] [refactor]: Move command to separate cog --- bot.py | 86 ----------------------------------------------------- cogs/fun.py | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 86 deletions(-) create mode 100644 cogs/fun.py diff --git a/bot.py b/bot.py index 21af984..57f79ae 100644 --- a/bot.py +++ b/bot.py @@ -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) diff --git a/cogs/fun.py b/cogs/fun.py new file mode 100644 index 0000000..43e53bf --- /dev/null +++ b/cogs/fun.py @@ -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())