diff options
author | xultist <xultist@proton.me> | 2023-02-20 09:58:50 +1100 |
---|---|---|
committer | xultist <xultist@proton.me> | 2023-02-20 09:58:50 +1100 |
commit | 322d4fe6ed7d2aecee06c4c2e40b4cfe51d3d052 (patch) | |
tree | 6bcdbd0d476b3b65342ffee26e49e55cd2cc26e9 | |
parent | c3b20a86e6d4a5fbe1d53688adb523ae9e2bfa05 (diff) |
Moved syntax to seperate class
-rw-r--r-- | cogs/__pycache__/utils.cpython-310.pyc | bin | 5183 -> 8202 bytes | |||
-rw-r--r-- | cogs/syntax.py | 85 | ||||
-rw-r--r-- | cogs/utils.py | 75 | ||||
-rw-r--r-- | config.json | 8 | ||||
-rw-r--r-- | main.py | 6 | ||||
-rw-r--r-- | requirements.txt | 2 |
6 files changed, 94 insertions, 82 deletions
diff --git a/cogs/__pycache__/utils.cpython-310.pyc b/cogs/__pycache__/utils.cpython-310.pyc Binary files differindex c6e4aff..8ab8b38 100644 --- a/cogs/__pycache__/utils.cpython-310.pyc +++ b/cogs/__pycache__/utils.cpython-310.pyc diff --git a/cogs/syntax.py b/cogs/syntax.py new file mode 100644 index 0000000..c426c2e --- /dev/null +++ b/cogs/syntax.py @@ -0,0 +1,85 @@ +import discord +from discord.ext import commands +from discord.ext.commands import Context +from main import config +from pygments.lexer import RegexLexer +from pygments.token import * +from PIL import Image +from pygments import highlight +from pygments.lexers import get_lexer_by_name +from pygments.styles.onedark import OneDarkStyle +from pygments.formatters import ImageFormatter +import io +import secrets + +class HolyCLexer(RegexLexer): + name = 'HolyC' + aliases = ['holyc'] + filenames = ['*.hc', '*.hh', '*.zc'] + tokens = { + 'root': [ + (r'\\b(break|case|continue|default|do|else|for|goto|if|return|switch|while|throw|try|catch|extern|MOV|CALL|PUSH|LEAVE|RET|SUB|SHR|ADD|RETF|CMP|JNE|BTS|INT|XOR|JC|JZ|LOOP|POP|TEST|SHL|ADC|SBB|JMP|INC|asm|const|extern|register|restrict|static|volatile|inline|_extern|_import|IMPORT|public)\b', Keyword), + (r'\b(U0|I8|U8|I16|U16|I32|U32|I64|U64|F64|Bool|class|union|DU8|DU16|DU32|DU64|RAX|RCX|RDX|RBX|RSP|RBP|RSI|RDI|EAX|ECX|EDX|EBX|ESP|EBP|ESI|EDI|AX|CX|DX|BX|SP|BP|SI|DI|SS|CS|DS|ES|FS|GS|CH)\b', Keyword.Type), + (r'"[^"]*"', String), + (r'//.*?\n', Comment.Single), + (r'/\*.*?\*/', Comment.Multiline), + (r'\d+', Number), + (r'\b(true|false)\b', Keyword.Constant), + (r'[A-Za-z_]\w*', Name), + (r'[~!%^&*+=|?:<>/-]', Operator), + (r'[(){}\[\],.;]', Punctuation), + (r'\s+', Text), + ] + } + +class Syntax(commands.Cog, name="syntax"): + def __init__(self, bot): + self.bot = bot + + global color + color = int(config["bot_color"], base=16) + + @discord.slash_command(guild_ids=config['main_guilds'], description="Converts your last message to HolyC Code") + async def holyc(self, ctx): + channel = ctx.channel + user: discord.Member = ctx.author + async for message in channel.history(limit=100): + if message.author == user: + if message.content.startswith("```") and message.content.endswith("```"): + code = message.content[3:-3] + + lexer = HolyCLexer() + formatter = ImageFormatter(font_name='JetBrainsMono-Medium.ttf', font_size=20, line_numbers=False, + style=OneDarkStyle) + + highlighted_code = highlight(code, lexer, formatter) + image_data = io.BytesIO(highlighted_code) + image = Image.open(image_data) + # Increase the image resolution + width, height = image.size + image = image.resize((width * 2, height * 2), resample=Image.LANCZOS) + + with io.BytesIO() as image_binary: + image.save(image_binary, 'PNG') + image_binary.seek(0) + image_data = image_binary.getvalue() + + file = discord.File(io.BytesIO(image_data), filename='image.png') + + # Create the embed and set the image to the file + embed = discord.Embed(title=f"HolyC Snippet", description=f"Requested by {user.mention}") + embed.set_footer(text="You can create your own HolyC snippets by sending code in chat and then typing /holyc.") + embed.set_image(url="attachment://image.png") + await message.delete() + await ctx.respond(file=file, embed=embed) + break + + else: + await ctx.respond("Your last message contains no code visible to me.", ephemeral=True) + break + else: + await ctx.respond("I was unable to find a message from you.", ephemeral=True) + + +def setup(bot): + bot.add_cog(Syntax(bot))
\ No newline at end of file diff --git a/cogs/utils.py b/cogs/utils.py index 0cefc94..e981360 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -5,38 +5,6 @@ from discord.ext import commands from discord.ext.commands import Context, MissingPermissions from main import config, randomMsg import re -from pygments.lexer import RegexLexer -from pygments.token import * -from PIL import Image -from pygments import highlight -from pygments.lexers import get_lexer_by_name -from pygments.styles.onedark import OneDarkStyle -from pygments.formatters import ImageFormatter -import io -import secrets - -class HolyCLexer(RegexLexer): - name = 'HolyC' - aliases = ['holyc'] - filenames = ['*.hc', '*.hh', '*.zc'] - tokens = { - 'root': [ - (r'\\b(break|case|continue|default|do|else|for|goto|if|return|switch|while|throw|try|catch|extern|MOV|CALL|PUSH|LEAVE|RET|SUB|SHR|ADD|RETF|CMP|JNE|BTS|INT|XOR|JC|JZ|LOOP|POP|TEST|SHL|ADC|SBB|JMP|INC|asm|const|extern|register|restrict|static|volatile|inline|_extern|_import|IMPORT|public)\b', Keyword), - (r'\b(U0|I8|U8|I16|U16|I32|U32|I64|U64|F64|Bool|class|union|DU8|DU16|DU32|DU64|RAX|RCX|RDX|RBX|RSP|RBP|RSI|RDI|EAX|ECX|EDX|EBX|ESP|EBP|ESI|EDI|AX|CX|DX|BX|SP|BP|SI|DI|SS|CS|DS|ES|FS|GS|CH)\b', Keyword.Type), - (r'"[^"]*"', String), - (r'//.*?\n', Comment.Single), - (r'/\*.*?\*/', Comment.Multiline), - (r'\d+', Number), - (r'\b(true|false)\b', Keyword.Constant), - (r'[A-Za-z_]\w*', Name), - (r'[~!%^&*+=|?:<>/-]', Operator), - (r'[(){}\[\],.;]', Punctuation), - (r'\s+', Text), - ] - } - - - class Utils(commands.Cog, name="utils"): def __init__(self, bot): @@ -128,46 +96,5 @@ class Utils(commands.Cog, name="utils"): text = "Sorry {}, you do not have permissions to do that!" await ctx.respond(text) - @discord.slash_command(guild_ids=config['main_guilds'], description="Converts your last message to HolyC Code") - async def holyc(self, ctx): - channel = ctx.channel - user: discord.Member = ctx.author - async for message in channel.history(limit=100): - if message.author == user: - if message.content.startswith("```") and message.content.endswith("```"): - code = message.content[3:-3] - - lexer = HolyCLexer() - formatter = ImageFormatter(font_name='JetBrainsMono-Medium.ttf', font_size=20, line_numbers=False, - style=OneDarkStyle) - - highlighted_code = highlight(code, lexer, formatter) - image_data = io.BytesIO(highlighted_code) - image = Image.open(image_data) - # Increase the image resolution - width, height = image.size - image = image.resize((width * 2, height * 2), resample=Image.LANCZOS) - - with io.BytesIO() as image_binary: - image.save(image_binary, 'PNG') - image_binary.seek(0) - image_data = image_binary.getvalue() - - file = discord.File(io.BytesIO(image_data), filename='image.png') - - # Create the embed and set the image to the file - embed = discord.Embed(title=f"HolyC Snippet", description=f"Requested by {user.mention}") - embed.set_footer(text="You can create your own HolyC snippets by sending code in chat and then typing /holyc.") - embed.set_image(url="attachment://image.png") - await message.delete() - await ctx.respond(file=file, embed=embed) - break - - else: - await ctx.respond("Your last message contains no code visible to me.", ephemeral=True) - break - else: - await ctx.respond("I was unable to find a message from you.", ephemeral=True) - def setup(bot): - bot.add_cog(Utils(bot)) + bot.add_cog(Utils(bot))
\ No newline at end of file diff --git a/config.json b/config.json index 70cdc7a..bc60ae5 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,8 @@ { "prefix": "c!", "token": "", - "application_id": "", "openai_key": "", + "application_id": "", "random_footer": [ "Run /credits to view Bot credits", "Pure Aryan Technology™ Inc©.", @@ -14,11 +14,11 @@ "around in TempleOS" ], "owners": [ - 1074110616895238204, - 1050544143102914692 + 922299510850486293, + 1007410169145212958 ], "main_guilds": [ 1076700215521837187 ], - "bot_color": "6eacba" + "bot_color": "6eacba" }
\ No newline at end of file @@ -27,6 +27,8 @@ def randomMsg(): d = requests.get('https://xkcd.com/info.0.json').json() totalComics = d['num'] + + @bot.event async def on_ready() -> None: print("cleaning up") @@ -35,14 +37,13 @@ async def on_ready() -> None: os.remove(file) else: pass - print(f"Logged in as {bot.user.name}") print(f"discord.py API version: {discord.__version__}") print(f"Python version: {platform.python_version()}") print(f"Running on: {platform.system()} {platform.release()} ({os.name})") print(f"Comics: {totalComics}") print("-------------------") - print(f"Invite to your server using: https://discord.com/api/oauth2/authorize?client_id={config['application_id']}&permissions=412387495489&scope=bot") + channel = bot.get_channel(1040394602710040677) status_task.start() @@ -65,7 +66,6 @@ async def on_command_error(ctx, error): if isinstance(error, commands.CommandNotFound): await ctx.send("wot") - def load_cogs() -> None: """ The code in this function is executed whenever the bot will start. diff --git a/requirements.txt b/requirements.txt index a0d338f..2597879 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ py-cord requests Pillow openai -pygments +pygments
\ No newline at end of file |