From 3f25d1121f6c5ba3bda426e58646c0777487657f Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 10 May 2018 15:18:28 -0400 Subject: [PATCH 1/5] CogLint initial commit Signed-off-by: Bobloy --- coglint/__init__.py | 5 +++++ coglint/coglint.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 coglint/__init__.py create mode 100644 coglint/coglint.py diff --git a/coglint/__init__.py b/coglint/__init__.py new file mode 100644 index 0000000..87f61bb --- /dev/null +++ b/coglint/__init__.py @@ -0,0 +1,5 @@ +from .coglint import CogLint + + +def setup(bot): + bot.add_cog(CogLint(bot)) diff --git a/coglint/coglint.py b/coglint/coglint.py new file mode 100644 index 0000000..20e2075 --- /dev/null +++ b/coglint/coglint.py @@ -0,0 +1,26 @@ +import discord +from discord.ext import commands +from redbot.core import Config, checks, RedContext + +from redbot.core.bot import Red + +import pylint + + +class CogLint: + """ + V3 Cog Template + """ + + def __init__(self, bot: Red): + self.bot = bot + self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True) + default_global = {} + default_guild = {} + + self.config.register_global(**default_global) + self.config.register_guild(**default_guild) + + @commands.command() + async def lint(self, ctx: RedContext): + await ctx.send("Hello World") From 85057f0b1e69b2f7781187458fa0fd835b23f084 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 10 May 2018 16:24:25 -0400 Subject: [PATCH 2/5] Forgot info file Signed-off-by: Bobloy --- coglint/info..json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 coglint/info..json diff --git a/coglint/info..json b/coglint/info..json new file mode 100644 index 0000000..709c9eb --- /dev/null +++ b/coglint/info..json @@ -0,0 +1,20 @@ +{ + "author": [ + "Bobloy" + ], + "bot_version": [ + 3, + 0, + 0 + ], + "description": "Lint python code posted in chat", + "hidden": true, + "install_msg": "Thank you for installing CogLint", + "requirements": [], + "short": "Python cog linter", + "tags": [ + "bobloy", + "utils", + "tools" + ] +} \ No newline at end of file From b724272380cb8357db2601a8c7349c3dfda1b35e Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 11 May 2018 10:31:24 -0400 Subject: [PATCH 3/5] Update README with coglint Signed-off-by: Bobloy --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 798b52b..2ff22d4 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Cog Function | --- | --- | --- | | ccrole | **Beta** |
Create custom commands that also assign rolesMay have some bugs, please create an issue if you find any
| | chatter | **Alpha** |
Chat-bot trained to talk like your guildMissing some key features, but currently functional
| +| coglint | **Incomplete** |
Error check code in python syntax posted to discordStill conceptual, no idea if it'll work
| | fight | **Incomplete** |
Organize bracket tournaments within discordStill in-progress, a massive project
| | flag | **Incomplete** |
Create temporary marks on users that expire after specified timeNot yet ported to v3
| | hangman | **Alpha** |
Play a game of hangmanSome visual glitches and needs more customization
| From d467c8b24f41ac9c6c8d9597096f4a7b630e4f6e Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 11 May 2018 12:08:09 -0400 Subject: [PATCH 4/5] Now functional Signed-off-by: Bobloy --- coglint/coglint.py | 55 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/coglint/coglint.py b/coglint/coglint.py index 20e2075..473817e 100644 --- a/coglint/coglint.py +++ b/coglint/coglint.py @@ -4,7 +4,8 @@ from redbot.core import Config, checks, RedContext from redbot.core.bot import Red -import pylint +from pylint import epylint as lint +from redbot.core.data_manager import cog_data_path class CogLint: @@ -15,12 +16,60 @@ class CogLint: def __init__(self, bot: Red): self.bot = bot self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True) - default_global = {} + default_global = { + "lint": True + } default_guild = {} + self.path = str(cog_data_path(self)).replace('\\', '/') + + self.counter = 0 + + # self.answer_path = self.path + "/tmpfile.py" + self.config.register_global(**default_global) self.config.register_guild(**default_guild) @commands.command() - async def lint(self, ctx: RedContext): + async def autolint(self, ctx: RedContext): + """Toggles automatically linting code""" + + @commands.command() + async def lint(self, ctx: RedContext, *, code): + """Lint python code""" + await self.lint_message(ctx.message) await ctx.send("Hello World") + + async def lint_code(self, code): + self.counter += 1 + path = self.path + "/{}.py".format(self.counter) + with open(path, 'w') as codefile: + codefile.write(code) + + future = await self.bot.loop.run_in_executor(None, lint.py_run, path, 'return_std=True') + + if future: + (pylint_stdout, pylint_stderr) = future + else: + (pylint_stdout, pylint_stderr) = None, None + + # print(pylint_stderr) + # print(pylint_stdout) + + return pylint_stdout, pylint_stderr + + async def lint_message(self, message): + code_blocks = message.content.split('```')[1::2] + + for c in code_blocks: + is_python, code = c.split(None, 1) + is_python = is_python.lower() == 'python' + if is_python: # Then we're in business + linted, errors = await self.lint_code(code) + linted = linted.getvalue() + errors = errors.getvalue() + await message.channel.send(linted) + # await message.channel.send(errors) + + async def on_message(self, message: discord.Message): + await self.lint_message(message) From 8cca379d378df79feedd5b05795742727a442756 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 11 May 2018 12:53:12 -0400 Subject: [PATCH 5/5] Toggle lint and Alpha release Signed-off-by: Bobloy --- README.md | 2 +- coglint/coglint.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ff22d4..f3837a3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Cog Function | --- | --- | --- | | ccrole | **Beta** |
Create custom commands that also assign rolesMay have some bugs, please create an issue if you find any
| | chatter | **Alpha** |
Chat-bot trained to talk like your guildMissing some key features, but currently functional
| -| coglint | **Incomplete** |
Error check code in python syntax posted to discordStill conceptual, no idea if it'll work
| +| coglint | **Alpha** |
Error check code in python syntax posted to discordWorks, but probably needs more turning to work for cogs
| | fight | **Incomplete** |
Organize bracket tournaments within discordStill in-progress, a massive project
| | flag | **Incomplete** |
Create temporary marks on users that expire after specified timeNot yet ported to v3
| | hangman | **Alpha** |
Play a game of hangmanSome visual glitches and needs more customization
| diff --git a/coglint/coglint.py b/coglint/coglint.py index 473817e..cf93402 100644 --- a/coglint/coglint.py +++ b/coglint/coglint.py @@ -23,6 +23,7 @@ class CogLint: self.path = str(cog_data_path(self)).replace('\\', '/') + self.do_lint = None self.counter = 0 # self.answer_path = self.path + "/tmpfile.py" @@ -33,10 +34,18 @@ class CogLint: @commands.command() async def autolint(self, ctx: RedContext): """Toggles automatically linting code""" + curr = await self.config.lint() + + self.do_lint = not curr + await self.config.lint.set(not curr) + await ctx.send("Autolinting is now set to {}".format(not curr)) @commands.command() async def lint(self, ctx: RedContext, *, code): - """Lint python code""" + """Lint python code + + Toggle autolinting with `[p]autolint` + """ await self.lint_message(ctx.message) await ctx.send("Hello World") @@ -59,6 +68,10 @@ class CogLint: return pylint_stdout, pylint_stderr async def lint_message(self, message): + if self.do_lint is None: + self.do_lint = await self.config.lint() + if not self.do_lint: + return code_blocks = message.content.split('```')[1::2] for c in code_blocks: